notify-discord-pr.yml 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. name: Discord PR Notification
  2. on:
  3. pull_request:
  4. types: [opened]
  5. workflow_dispatch:
  6. inputs:
  7. pr_number:
  8. description: "Pull Request number"
  9. required: true
  10. type: string
  11. jobs:
  12. Discord:
  13. runs-on: ubuntu-latest
  14. name: Discord PR Notifier
  15. steps:
  16. - uses: actions/checkout@v4
  17. if: github.event_name == 'workflow_dispatch'
  18. - name: Get PR info for manual trigger
  19. id: pr-info
  20. if: github.event_name == 'workflow_dispatch'
  21. run: |
  22. PR_INFO=$(gh pr view ${{ github.event.inputs.pr_number }} --json number,title,url,author,state,labels,createdAt,headRefName,baseRefName,isDraft,mergeable)
  23. echo "number=$(echo "$PR_INFO" | jq -r '.number')" >> $GITHUB_OUTPUT
  24. echo "title=$(echo "$PR_INFO" | jq -r '.title')" >> $GITHUB_OUTPUT
  25. echo "html_url=$(echo "$PR_INFO" | jq -r '.url')" >> $GITHUB_OUTPUT
  26. echo "author_login=$(echo "$PR_INFO" | jq -r '.author.login')" >> $GITHUB_OUTPUT
  27. echo "author_html_url=https://github.com/$(echo "$PR_INFO" | jq -r '.author.login')" >> $GITHUB_OUTPUT
  28. echo "state=$(echo "$PR_INFO" | jq -r '.state')" >> $GITHUB_OUTPUT
  29. echo "created_at=$(echo "$PR_INFO" | jq -r '.createdAt')" >> $GITHUB_OUTPUT
  30. echo "labels=$(echo "$PR_INFO" | jq -r '.labels | map(.name) | join(", ") // "None"')" >> $GITHUB_OUTPUT
  31. echo "head_ref=$(echo "$PR_INFO" | jq -r '.headRefName')" >> $GITHUB_OUTPUT
  32. echo "base_ref=$(echo "$PR_INFO" | jq -r '.baseRefName')" >> $GITHUB_OUTPUT
  33. echo "is_draft=$(echo "$PR_INFO" | jq -r '.isDraft')" >> $GITHUB_OUTPUT
  34. echo "mergeable=$(echo "$PR_INFO" | jq -r '.mergeable // "UNKNOWN"')" >> $GITHUB_OUTPUT
  35. env:
  36. GH_TOKEN: ${{ github.token }}
  37. - name: Determine action color and emoji
  38. id: action-info
  39. run: |
  40. if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
  41. # For manual trigger, use the current state
  42. case "${{ steps.pr-info.outputs.state }}" in
  43. "OPEN")
  44. if [ "${{ steps.pr-info.outputs.is_draft }}" = "true" ]; then
  45. echo "color=8421504" >> $GITHUB_OUTPUT # Gray
  46. echo "emoji=📝" >> $GITHUB_OUTPUT
  47. echo "action_text=Draft" >> $GITHUB_OUTPUT
  48. else
  49. echo "color=5763719" >> $GITHUB_OUTPUT # Green
  50. echo "emoji=🔄" >> $GITHUB_OUTPUT
  51. echo "action_text=Open" >> $GITHUB_OUTPUT
  52. fi
  53. ;;
  54. "CLOSED")
  55. echo "color=15158332" >> $GITHUB_OUTPUT # Red
  56. echo "emoji=❌" >> $GITHUB_OUTPUT
  57. echo "action_text=Closed" >> $GITHUB_OUTPUT
  58. ;;
  59. "MERGED")
  60. echo "color=6559689" >> $GITHUB_OUTPUT # Purple
  61. echo "emoji=🎉" >> $GITHUB_OUTPUT
  62. echo "action_text=Merged" >> $GITHUB_OUTPUT
  63. ;;
  64. esac
  65. else
  66. # For automatic trigger, use the action
  67. case "${{ github.event.action }}" in
  68. "opened")
  69. echo "color=5763719" >> $GITHUB_OUTPUT # Green
  70. echo "emoji=🔄" >> $GITHUB_OUTPUT
  71. echo "action_text=Opened" >> $GITHUB_OUTPUT
  72. ;;
  73. "reopened")
  74. echo "color=16776960" >> $GITHUB_OUTPUT # Yellow
  75. echo "emoji=🔄" >> $GITHUB_OUTPUT
  76. echo "action_text=Reopened" >> $GITHUB_OUTPUT
  77. ;;
  78. "closed")
  79. if [ "${{ github.event.pull_request.merged }}" = "true" ]; then
  80. echo "color=6559689" >> $GITHUB_OUTPUT # Purple
  81. echo "emoji=🎉" >> $GITHUB_OUTPUT
  82. echo "action_text=Merged" >> $GITHUB_OUTPUT
  83. else
  84. echo "color=15158332" >> $GITHUB_OUTPUT # Red
  85. echo "emoji=❌" >> $GITHUB_OUTPUT
  86. echo "action_text=Closed" >> $GITHUB_OUTPUT
  87. fi
  88. ;;
  89. "ready_for_review")
  90. echo "color=5763719" >> $GITHUB_OUTPUT # Green
  91. echo "emoji=👀" >> $GITHUB_OUTPUT
  92. echo "action_text=Ready for Review" >> $GITHUB_OUTPUT
  93. ;;
  94. "converted_to_draft")
  95. echo "color=8421504" >> $GITHUB_OUTPUT # Gray
  96. echo "emoji=📝" >> $GITHUB_OUTPUT
  97. echo "action_text=Converted to Draft" >> $GITHUB_OUTPUT
  98. ;;
  99. esac
  100. fi
  101. - name: Create Discord webhook payload
  102. run: |
  103. # Determine data source based on trigger type
  104. if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
  105. PR_NUMBER="${{ steps.pr-info.outputs.number }}"
  106. PR_TITLE="${{ steps.pr-info.outputs.title }}"
  107. PR_URL="${{ steps.pr-info.outputs.html_url }}"
  108. AUTHOR_LOGIN="${{ steps.pr-info.outputs.author_login }}"
  109. AUTHOR_URL="${{ steps.pr-info.outputs.author_html_url }}"
  110. PR_STATE="${{ steps.pr-info.outputs.state }}"
  111. PR_LABELS="${{ steps.pr-info.outputs.labels }}"
  112. CREATED_AT="${{ steps.pr-info.outputs.created_at }}"
  113. HEAD_REF="${{ steps.pr-info.outputs.head_ref }}"
  114. BASE_REF="${{ steps.pr-info.outputs.base_ref }}"
  115. IS_DRAFT="${{ steps.pr-info.outputs.is_draft }}"
  116. MERGEABLE="${{ steps.pr-info.outputs.mergeable }}"
  117. else
  118. PR_NUMBER="${{ github.event.pull_request.number }}"
  119. PR_TITLE="${{ github.event.pull_request.title }}"
  120. PR_URL="${{ github.event.pull_request.html_url }}"
  121. AUTHOR_LOGIN="${{ github.event.pull_request.user.login }}"
  122. AUTHOR_URL="${{ github.event.pull_request.user.html_url }}"
  123. PR_STATE="${{ github.event.pull_request.state }}"
  124. PR_LABELS="${{ github.event.pull_request.labels[0].name && join(github.event.pull_request.labels.*.name, ', ') || 'None' }}"
  125. CREATED_AT="${{ github.event.pull_request.created_at }}"
  126. HEAD_REF="${{ github.event.pull_request.head.ref }}"
  127. BASE_REF="${{ github.event.pull_request.base.ref }}"
  128. IS_DRAFT="${{ github.event.pull_request.draft }}"
  129. MERGEABLE="${{ github.event.pull_request.mergeable || 'UNKNOWN' }}"
  130. fi
  131. # Create a temporary JSON file
  132. cat > discord_payload.json << EOF
  133. {
  134. "avatar_url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png",
  135. "embeds": [
  136. {
  137. "title": "${{ steps.action-info.outputs.emoji }} Pull Request ${{ steps.action-info.outputs.action_text }}: #${PR_NUMBER}",
  138. "description": "${PR_TITLE}",
  139. "url": "${PR_URL}",
  140. "color": ${{ steps.action-info.outputs.color }},
  141. "thumbnail": {
  142. "url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
  143. },
  144. "fields": [
  145. {
  146. "name": "📋 PR #",
  147. "value": "\`#${PR_NUMBER}\`",
  148. "inline": true
  149. },
  150. {
  151. "name": "👤 Author",
  152. "value": "[${AUTHOR_LOGIN}](${AUTHOR_URL})",
  153. "inline": true
  154. },
  155. {
  156. "name": "📁 Repository",
  157. "value": "[${{ github.event.repository.name }}](${{ github.event.repository.html_url }})",
  158. "inline": true
  159. },
  160. {
  161. "name": "🌿 Branch",
  162. "value": "\`${HEAD_REF}\` → \`${BASE_REF}\`",
  163. "inline": true
  164. },
  165. {
  166. "name": "🏷️ Labels",
  167. "value": "${PR_LABELS}",
  168. "inline": true
  169. },
  170. {
  171. "name": "📊 Status",
  172. "value": "\`${PR_STATE}\`${IS_DRAFT:+\" (Draft)\"}",
  173. "inline": true
  174. },
  175. {
  176. "name": "🔗 View PR",
  177. "value": "[Pull Request](${PR_URL})",
  178. "inline": true
  179. },
  180. {
  181. "name": "✅ Mergeable",
  182. "value": "\`${MERGEABLE}\`",
  183. "inline": true
  184. }
  185. ],
  186. "timestamp": "${CREATED_AT}",
  187. "footer": {
  188. "text": "Workout Cool • PR ${{ steps.action-info.outputs.action_text }}",
  189. "icon_url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
  190. }
  191. }
  192. ]
  193. }
  194. EOF
  195. - name: Send Discord notification
  196. run: |
  197. curl -H "Content-Type: application/json" \
  198. -d @discord_payload.json \
  199. "${{ secrets.DISCORD_PR_WEBHOOK }}"