Browse Source

feat(workflow): add Discord PR notification workflow to notify on PR events for better collaboration and visibility

Mathias 1 tháng trước cách đây
mục cha
commit
d16c05e7ea
1 tập tin đã thay đổi với 206 bổ sung0 xóa
  1. 206 0
      .github/workflows/notify-discord-pr.yml

+ 206 - 0
.github/workflows/notify-discord-pr.yml

@@ -0,0 +1,206 @@
+name: Discord PR Notification
+
+on:
+  pull_request:
+    types: [opened]
+  workflow_dispatch:
+    inputs:
+      pr_number:
+        description: "Pull Request number"
+        required: true
+        type: string
+
+jobs:
+  Discord:
+    runs-on: ubuntu-latest
+    name: Discord PR Notifier
+    steps:
+      - uses: actions/checkout@v4
+        if: github.event_name == 'workflow_dispatch'
+
+      - name: Get PR info for manual trigger
+        id: pr-info
+        if: github.event_name == 'workflow_dispatch'
+        run: |
+          PR_INFO=$(gh pr view ${{ github.event.inputs.pr_number }} --json number,title,url,author,state,labels,createdAt,headRefName,baseRefName,isDraft,mergeable)
+          echo "number=$(echo "$PR_INFO" | jq -r '.number')" >> $GITHUB_OUTPUT
+          echo "title=$(echo "$PR_INFO" | jq -r '.title')" >> $GITHUB_OUTPUT
+          echo "html_url=$(echo "$PR_INFO" | jq -r '.url')" >> $GITHUB_OUTPUT
+          echo "author_login=$(echo "$PR_INFO" | jq -r '.author.login')" >> $GITHUB_OUTPUT
+          echo "author_html_url=https://github.com/$(echo "$PR_INFO" | jq -r '.author.login')" >> $GITHUB_OUTPUT
+          echo "state=$(echo "$PR_INFO" | jq -r '.state')" >> $GITHUB_OUTPUT
+          echo "created_at=$(echo "$PR_INFO" | jq -r '.createdAt')" >> $GITHUB_OUTPUT
+          echo "labels=$(echo "$PR_INFO" | jq -r '.labels | map(.name) | join(", ") // "None"')" >> $GITHUB_OUTPUT
+          echo "head_ref=$(echo "$PR_INFO" | jq -r '.headRefName')" >> $GITHUB_OUTPUT
+          echo "base_ref=$(echo "$PR_INFO" | jq -r '.baseRefName')" >> $GITHUB_OUTPUT
+          echo "is_draft=$(echo "$PR_INFO" | jq -r '.isDraft')" >> $GITHUB_OUTPUT
+          echo "mergeable=$(echo "$PR_INFO" | jq -r '.mergeable // "UNKNOWN"')" >> $GITHUB_OUTPUT
+        env:
+          GH_TOKEN: ${{ github.token }}
+
+      - name: Determine action color and emoji
+        id: action-info
+        run: |
+          if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
+            # For manual trigger, use the current state
+            case "${{ steps.pr-info.outputs.state }}" in
+              "OPEN")
+                if [ "${{ steps.pr-info.outputs.is_draft }}" = "true" ]; then
+                  echo "color=8421504" >> $GITHUB_OUTPUT   # Gray
+                  echo "emoji=📝" >> $GITHUB_OUTPUT
+                  echo "action_text=Draft" >> $GITHUB_OUTPUT
+                else
+                  echo "color=5763719" >> $GITHUB_OUTPUT   # Green
+                  echo "emoji=🔄" >> $GITHUB_OUTPUT
+                  echo "action_text=Open" >> $GITHUB_OUTPUT
+                fi
+                ;;
+              "CLOSED")
+                echo "color=15158332" >> $GITHUB_OUTPUT    # Red
+                echo "emoji=❌" >> $GITHUB_OUTPUT
+                echo "action_text=Closed" >> $GITHUB_OUTPUT
+                ;;
+              "MERGED")
+                echo "color=6559689" >> $GITHUB_OUTPUT     # Purple
+                echo "emoji=🎉" >> $GITHUB_OUTPUT
+                echo "action_text=Merged" >> $GITHUB_OUTPUT
+                ;;
+            esac
+          else
+            # For automatic trigger, use the action
+            case "${{ github.event.action }}" in
+              "opened")
+                echo "color=5763719" >> $GITHUB_OUTPUT     # Green
+                echo "emoji=🔄" >> $GITHUB_OUTPUT
+                echo "action_text=Opened" >> $GITHUB_OUTPUT
+                ;;
+              "reopened")
+                echo "color=16776960" >> $GITHUB_OUTPUT    # Yellow
+                echo "emoji=🔄" >> $GITHUB_OUTPUT
+                echo "action_text=Reopened" >> $GITHUB_OUTPUT
+                ;;
+              "closed")
+                if [ "${{ github.event.pull_request.merged }}" = "true" ]; then
+                  echo "color=6559689" >> $GITHUB_OUTPUT   # Purple
+                  echo "emoji=🎉" >> $GITHUB_OUTPUT
+                  echo "action_text=Merged" >> $GITHUB_OUTPUT
+                else
+                  echo "color=15158332" >> $GITHUB_OUTPUT  # Red
+                  echo "emoji=❌" >> $GITHUB_OUTPUT
+                  echo "action_text=Closed" >> $GITHUB_OUTPUT
+                fi
+                ;;
+              "ready_for_review")
+                echo "color=5763719" >> $GITHUB_OUTPUT     # Green
+                echo "emoji=👀" >> $GITHUB_OUTPUT
+                echo "action_text=Ready for Review" >> $GITHUB_OUTPUT
+                ;;
+              "converted_to_draft")
+                echo "color=8421504" >> $GITHUB_OUTPUT     # Gray
+                echo "emoji=📝" >> $GITHUB_OUTPUT
+                echo "action_text=Converted to Draft" >> $GITHUB_OUTPUT
+                ;;
+            esac
+          fi
+
+      - name: Create Discord webhook payload
+        run: |
+          # Determine data source based on trigger type
+          if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
+            PR_NUMBER="${{ steps.pr-info.outputs.number }}"
+            PR_TITLE="${{ steps.pr-info.outputs.title }}"
+            PR_URL="${{ steps.pr-info.outputs.html_url }}"
+            AUTHOR_LOGIN="${{ steps.pr-info.outputs.author_login }}"
+            AUTHOR_URL="${{ steps.pr-info.outputs.author_html_url }}"
+            PR_STATE="${{ steps.pr-info.outputs.state }}"
+            PR_LABELS="${{ steps.pr-info.outputs.labels }}"
+            CREATED_AT="${{ steps.pr-info.outputs.created_at }}"
+            HEAD_REF="${{ steps.pr-info.outputs.head_ref }}"
+            BASE_REF="${{ steps.pr-info.outputs.base_ref }}"
+            IS_DRAFT="${{ steps.pr-info.outputs.is_draft }}"
+            MERGEABLE="${{ steps.pr-info.outputs.mergeable }}"
+          else
+            PR_NUMBER="${{ github.event.pull_request.number }}"
+            PR_TITLE="${{ github.event.pull_request.title }}"
+            PR_URL="${{ github.event.pull_request.html_url }}"
+            AUTHOR_LOGIN="${{ github.event.pull_request.user.login }}"
+            AUTHOR_URL="${{ github.event.pull_request.user.html_url }}"
+            PR_STATE="${{ github.event.pull_request.state }}"
+            PR_LABELS="${{ github.event.pull_request.labels[0].name && join(github.event.pull_request.labels.*.name, ', ') || 'None' }}"
+            CREATED_AT="${{ github.event.pull_request.created_at }}"
+            HEAD_REF="${{ github.event.pull_request.head.ref }}"
+            BASE_REF="${{ github.event.pull_request.base.ref }}"
+            IS_DRAFT="${{ github.event.pull_request.draft }}"
+            MERGEABLE="${{ github.event.pull_request.mergeable || 'UNKNOWN' }}"
+          fi
+
+          # Create a temporary JSON file
+          cat > discord_payload.json << EOF
+          {
+            "avatar_url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png",
+            "embeds": [
+              {
+                "title": "${{ steps.action-info.outputs.emoji }} Pull Request ${{ steps.action-info.outputs.action_text }}: #${PR_NUMBER}",
+                "description": "${PR_TITLE}",
+                "url": "${PR_URL}",
+                "color": ${{ steps.action-info.outputs.color }},
+                "thumbnail": {
+                  "url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
+                },
+                "fields": [
+                  {
+                    "name": "📋 PR #",
+                    "value": "\`#${PR_NUMBER}\`",
+                    "inline": true
+                  },
+                  {
+                    "name": "👤 Author",
+                    "value": "[${AUTHOR_LOGIN}](${AUTHOR_URL})",
+                    "inline": true
+                  },
+                  {
+                    "name": "📁 Repository",
+                    "value": "[${{ github.event.repository.name }}](${{ github.event.repository.html_url }})",
+                    "inline": true
+                  },
+                  {
+                    "name": "🌿 Branch",
+                    "value": "\`${HEAD_REF}\` → \`${BASE_REF}\`",
+                    "inline": true
+                  },
+                  {
+                    "name": "🏷️ Labels",
+                    "value": "${PR_LABELS}",
+                    "inline": true
+                  },
+                  {
+                    "name": "📊 Status",
+                    "value": "\`${PR_STATE}\`${IS_DRAFT:+\" (Draft)\"}",
+                    "inline": true
+                  },
+                  {
+                    "name": "🔗 View PR",
+                    "value": "[Pull Request](${PR_URL})",
+                    "inline": true
+                  },
+                  {
+                    "name": "✅ Mergeable",
+                    "value": "\`${MERGEABLE}\`",
+                    "inline": true
+                  }
+                ],
+                "timestamp": "${CREATED_AT}",
+                "footer": {
+                  "text": "Workout Cool • PR ${{ steps.action-info.outputs.action_text }}",
+                  "icon_url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
+                }
+              }
+            ]
+          }
+          EOF
+
+      - name: Send Discord notification
+        run: |
+          curl -H "Content-Type: application/json" \
+               -d @discord_payload.json \
+               "${{ secrets.DISCORD_PR_WEBHOOK }}"