fix: rewrite Discord webhook workflow for Gitea compatibility
- Remove jq dependency - Use file-based payload with cat > /tmp/discord.json instead of inline HEREDOC - Fix HEREDOC indentation issue - Escape JSON properly with sed - Use curl -d @file for reliable payload delivery Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,70 +1,63 @@
|
||||
name: Discord Notification
|
||||
|
||||
on:
|
||||
push:
|
||||
push:
|
||||
|
||||
jobs:
|
||||
notify:
|
||||
runs_on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 2
|
||||
notify:
|
||||
runs_on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 2
|
||||
|
||||
- name: Send Discord Embed
|
||||
run: |
|
||||
REPO_NAME="${{ gitea.repository }}"
|
||||
BRANCH="${{ gitea.ref_name }}"
|
||||
AUTHOR="${{ gitea.actor }}"
|
||||
COMMIT_SHA="${{ gitea.sha }}"
|
||||
SHORT_SHA="${COMMIT_SHA:0:7}"
|
||||
COMMIT_URL="${{ gitea.server_url }}/${REPO_NAME}/commit/${COMMIT_SHA}"
|
||||
REPO_URL="${{ gitea.server_url }}/${REPO_NAME}"
|
||||
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||
- name: Send Discord Embed
|
||||
run: |
|
||||
REPO_NAME="${{ gitea.repository }}"
|
||||
BRANCH="${{ gitea.ref_name }}"
|
||||
AUTHOR="${{ gitea.actor }}"
|
||||
COMMIT_SHA="${{ gitea.sha }}"
|
||||
SHORT_SHA="${COMMIT_SHA:0:7}"
|
||||
COMMIT_URL="${{ gitea.server_url }}/${REPO_NAME}/commit/${COMMIT_SHA}"
|
||||
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||
|
||||
COMMIT_MSG=$(git log -1 --pretty=format:'%s' | head -c 256)
|
||||
COMMIT_BODY=$(git log -1 --pretty=format:'%b' | head -c 1024)
|
||||
FILES_CHANGED=$(git diff --stat HEAD~1 HEAD 2>/dev/null | tail -1 || echo "N/A")
|
||||
FILES_LIST=$(git diff --name-only HEAD~1 HEAD 2>/dev/null | head -20 | sed 's/^/• /' || echo "N/A")
|
||||
FILE_COUNT=$(git diff --name-only HEAD~1 HEAD 2>/dev/null | wc -l || echo "0")
|
||||
COMMIT_MSG=$(git log -1 --pretty=format:'%s' | head -c 200)
|
||||
FILES_CHANGED=$(git diff --stat HEAD~1 HEAD 2>/dev/null | tail -1 || echo "premier commit")
|
||||
FILE_COUNT=$(git diff --name-only HEAD~1 HEAD 2>/dev/null | wc -l || echo "0")
|
||||
FILES_LIST=$(git diff --name-only HEAD~1 HEAD 2>/dev/null | head -15 | sed 's/^/• /' || echo "• premier commit")
|
||||
|
||||
if [ "$FILE_COUNT" -gt 20 ]; then
|
||||
REMAINING=$((FILE_COUNT - 20))
|
||||
FILES_LIST="${FILES_LIST}\n... et ${REMAINING} autres fichiers"
|
||||
fi
|
||||
if [ "$FILE_COUNT" -gt 15 ]; then
|
||||
REMAINING=$((FILE_COUNT - 15))
|
||||
FILES_LIST=$(printf '%s\n... et %s autres fichiers' "$FILES_LIST" "$REMAINING")
|
||||
fi
|
||||
|
||||
DESCRIPTION="${COMMIT_MSG}"
|
||||
if [ -n "$COMMIT_BODY" ]; then
|
||||
DESCRIPTION="${DESCRIPTION}\n\n${COMMIT_BODY}"
|
||||
fi
|
||||
# Escape for JSON
|
||||
COMMIT_MSG_ESC=$(echo "$COMMIT_MSG" | sed 's/\\/\\\\/g; s/"/\\"/g; s/\t/\\t/g')
|
||||
FILES_CHANGED_ESC=$(echo "$FILES_CHANGED" | sed 's/\\/\\\\/g; s/"/\\"/g; s/\t/\\t/g')
|
||||
FILES_LIST_ESC=$(echo "$FILES_LIST" | sed ':a;N;$!ba;s/\n/\\n/g' | sed 's/\\/\\\\/g; s/"/\\"/g' | sed 's/\\\\\\\\/\\\\/g')
|
||||
|
||||
FIELDS="["
|
||||
FIELDS="${FIELDS}{\"name\":\"Branche\",\"value\":\"\`${BRANCH}\`\",\"inline\":true},"
|
||||
FIELDS="${FIELDS}{\"name\":\"Commit\",\"value\":\"[\`${SHORT_SHA}\`](${COMMIT_URL})\",\"inline\":true},"
|
||||
FIELDS="${FIELDS}{\"name\":\"Auteur\",\"value\":\"${AUTHOR}\",\"inline\":true},"
|
||||
FIELDS="${FIELDS}{\"name\":\"Statistiques\",\"value\":\"\`\`\`${FILES_CHANGED}\`\`\`\",\"inline\":false},"
|
||||
FIELDS="${FIELDS}{\"name\":\"Fichiers modifies\",\"value\":\"\`\`\`${FILES_LIST}\`\`\`\",\"inline\":false}"
|
||||
FIELDS="${FIELDS}]"
|
||||
cat > /tmp/discord.json <<DEOF
|
||||
{
|
||||
"embeds": [{
|
||||
"title": "DEPOT CRM SITECONSEIL",
|
||||
"description": "${COMMIT_MSG_ESC}",
|
||||
"url": "${COMMIT_URL}",
|
||||
"color": 16156748,
|
||||
"fields": [
|
||||
{"name": "Branche", "value": "\`${BRANCH}\`", "inline": true},
|
||||
{"name": "Commit", "value": "[\`${SHORT_SHA}\`](${COMMIT_URL})", "inline": true},
|
||||
{"name": "Auteur", "value": "${AUTHOR}", "inline": true},
|
||||
{"name": "Statistiques", "value": "\`\`\`${FILES_CHANGED_ESC}\`\`\`", "inline": false},
|
||||
{"name": "Fichiers modifies (${FILE_COUNT})", "value": "\`\`\`${FILES_LIST_ESC}\`\`\`", "inline": false}
|
||||
],
|
||||
"footer": {"text": "${REPO_NAME}"},
|
||||
"timestamp": "${TIMESTAMP}"
|
||||
}]
|
||||
}
|
||||
DEOF
|
||||
|
||||
PAYLOAD=$(cat <<EOFPAYLOAD
|
||||
{
|
||||
"embeds": [{
|
||||
"title": "DEPOT CRM SITECONSEIL",
|
||||
"description": $(echo -e "$DESCRIPTION" | jq -Rs .),
|
||||
"url": "${COMMIT_URL}",
|
||||
"color": 16244476,
|
||||
"fields": ${FIELDS},
|
||||
"footer": {
|
||||
"text": "${REPO_NAME}"
|
||||
},
|
||||
"timestamp": "${TIMESTAMP}"
|
||||
}]
|
||||
}
|
||||
EOFPAYLOAD
|
||||
)
|
||||
|
||||
curl -s -X POST \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "${PAYLOAD}" \
|
||||
"https://discord.com/api/webhooks/1419573620602044518/ikAdxWxsrrTqMTb5Gh_8ylcoJHlOnq7aJZvR5udoS_fCK56Jk3qpEnJHVKdD8fwuNJF3"
|
||||
curl -sf -X POST \
|
||||
-H "Content-Type: application/json" \
|
||||
-d @/tmp/discord.json \
|
||||
"https://discord.com/api/webhooks/1419573620602044518/ikAdxWxsrrTqMTb5Gh_8ylcoJHlOnq7aJZvR5udoS_fCK56Jk3qpEnJHVKdD8fwuNJF3"
|
||||
|
||||
Reference in New Issue
Block a user