Parcourir la source

chore(notify-discord): refactor Discord notification workflow to improve readability and maintainability by simplifying JSON construction and separating message preparation from sending logic

Mathias il y a 1 mois
Parent
commit
cbc381193d
1 fichiers modifiés avec 28 ajouts et 61 suppressions
  1. 28 61
      .github/workflows/notify-discord.yml

+ 28 - 61
.github/workflows/notify-discord.yml

@@ -6,7 +6,7 @@ on:
   workflow_dispatch:
     inputs:
       tag_name:
-        description: 'Tag name (leave empty for latest release)'
+        description: "Tag name (leave empty for latest release)"
         required: false
         type: string
 
@@ -18,7 +18,7 @@ jobs:
       - uses: actions/checkout@v4
         with:
           fetch-depth: 0
-          
+
       - name: Get release info
         id: release-info
         run: |
@@ -62,78 +62,45 @@ jobs:
           fi
         env:
           GH_TOKEN: ${{ github.token }}
-          
+
       - name: Get previous release
         id: previous-release
         run: |
           PREV_TAG=$(git tag --sort=-version:refname | grep -v '${{ steps.release-info.outputs.tag_name }}' | head -n1)
           echo "previous_tag=${PREV_TAG}" >> $GITHUB_OUTPUT
-          
+
       - name: Get changed files since last release
         id: changed-files
         uses: tj-actions/changed-files@v44
         with:
           base_sha: ${{ steps.previous-release.outputs.previous_tag }}
           separator: "\n• "
-          
-      - name: Create Discord webhook payload
-        run: |
-          # Créer un fichier JSON temporaire
-          cat > discord_payload.json << 'EOF'
-          {
-            "avatar_url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png",
-            "embeds": [
-              {
-                "title": "🚀 New Release: ${{ steps.release-info.outputs.tag_name }}",
-                "description": "${{ steps.release-info.outputs.name }}\n\n${{ steps.release-info.outputs.body }}",
-                "url": "${{ steps.release-info.outputs.html_url }}",
-                "color": 5763719,
-                "thumbnail": {
-                  "url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
-                },
-                "fields": [
-                  {
-                    "name": "📦 Version",
-                    "value": "`${{ steps.release-info.outputs.tag_name }}`",
-                    "inline": true
-                  },
-                  {
-                    "name": "👤 Released by",
-                    "value": "[${{ steps.release-info.outputs.author_login }}](${{ steps.release-info.outputs.author_html_url }})",
-                    "inline": true
-                  },
-                  {
-                    "name": "📁 Repository",
-                    "value": "[${{ github.event.repository.name }}](${{ github.event.repository.html_url }})",
-                    "inline": true
-                  },
-                  {
-                    "name": "🔗 Download",
-                    "value": "[Release Page](${{ steps.release-info.outputs.html_url }})",
-                    "inline": true
-                  },
-                  {
-                    "name": "📝 Files Changed",
-                    "value": "• ${{ steps.changed-files.outputs.all_changed_files }}",
-                    "inline": false
-                  }
-                ],
-                "timestamp": "${{ steps.release-info.outputs.published_at }}",
-                "footer": {
-                  "text": "Workout Cool • Release",
-                  "icon_url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
-                }
-              }
-            ]
-          }
-          EOF
 
-      - name: Send Discord notification
+      - name: Prepare Discord message
+        id: discord-message
         run: |
-          curl -H "Content-Type: application/json" \
-               -d @discord_payload.json \
-               "${{ secrets.DISCORD_RELEASE_WEBHOOK }}"
+          # Échapper les caractères spéciaux dans le JSON
+          TITLE="🚀 New Release: ${{ steps.release-info.outputs.tag_name }}"
+          DESCRIPTION="${{ steps.release-info.outputs.name }}"
+          BODY="${{ steps.release-info.outputs.body }}"
 
+          # Échapper les guillemets et les sauts de ligne pour JSON
+          BODY_ESCAPED=$(echo "$BODY" | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g')
+          DESCRIPTION_FULL="$DESCRIPTION\\n\\n$BODY_ESCAPED"
+
+          # Construire le JSON
+          DISCORD_EMBEDS="[{\"title\": \"$TITLE\", \"description\": \"$DESCRIPTION_FULL\", \"url\": \"${{ steps.release-info.outputs.html_url }}\", \"color\": 5763719, \"thumbnail\": {\"url\": \"https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png\"}, \"fields\": [{\"name\": \"📦 Version\", \"value\": \"\`${{ steps.release-info.outputs.tag_name }}\`\", \"inline\": true}, {\"name\": \"👤 Released by\", \"value\": \"[${{ steps.release-info.outputs.author_login }}](${{ steps.release-info.outputs.author_html_url }})\", \"inline\": true}, {\"name\": \"📁 Repository\", \"value\": \"[${{ github.event.repository.name }}](${{ github.event.repository.html_url }})\", \"inline\": true}, {\"name\": \"🔗 Download\", \"value\": \"[Release Page](${{ steps.release-info.outputs.html_url }})\", \"inline\": true}, {\"name\": \"📝 Files Changed\", \"value\": \"• ${{ steps.changed-files.outputs.all_changed_files }}\", \"inline\": false}], \"timestamp\": \"${{ steps.release-info.outputs.published_at }}\", \"footer\": {\"text\": \"Workout Cool • Release\", \"icon_url\": \"https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png\"}}]"
+
+          echo "discord_embeds<<EOF" >> $GITHUB_OUTPUT
+          echo "$DISCORD_EMBEDS" >> $GITHUB_OUTPUT
+          echo "EOF" >> $GITHUB_OUTPUT
+
+      - name: Discord notification
+        env:
+          DISCORD_WEBHOOK: ${{ secrets.DISCORD_RELEASE_WEBHOOK }}
+          DISCORD_AVATAR: "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
+          DISCORD_EMBEDS: ${{ steps.discord-message.outputs.discord_embeds }}
+        uses: Ilshidur/action-discord@master
 # https://stackoverflow.com/a/68068674/19395252
 # https://birdie0.github.io/discord-webhooks-guide/structure/embeds.html
-# https://github.com/marketplace/actions/changed-files
+# https://github.com/marketplace/actions/changed-files