notify-discord-pr.yml 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. else
  117. PR_NUMBER="${{ github.event.pull_request.number }}"
  118. PR_TITLE="${{ github.event.pull_request.title }}"
  119. PR_URL="${{ github.event.pull_request.html_url }}"
  120. AUTHOR_LOGIN="${{ github.event.pull_request.user.login }}"
  121. AUTHOR_URL="${{ github.event.pull_request.user.html_url }}"
  122. PR_STATE="${{ github.event.pull_request.state }}"
  123. PR_LABELS="${{ github.event.pull_request.labels[0].name && join(github.event.pull_request.labels.*.name, ', ') || 'None' }}"
  124. CREATED_AT="${{ github.event.pull_request.created_at }}"
  125. HEAD_REF="${{ github.event.pull_request.head.ref }}"
  126. BASE_REF="${{ github.event.pull_request.base.ref }}"
  127. IS_DRAFT="${{ github.event.pull_request.draft }}"
  128. fi
  129. # Escape special characters for JSON
  130. PR_TITLE_ESCAPED=$(echo "$PR_TITLE" | sed 's/"/\\"/g' | sed "s/'/\\'/g")
  131. # Create a temporary JSON file using jq to ensure valid JSON
  132. jq -n \
  133. --arg avatar_url "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png" \
  134. --arg title "${{ steps.action-info.outputs.emoji }} Pull Request ${{ steps.action-info.outputs.action_text }}: #${PR_NUMBER}" \
  135. --arg description "$PR_TITLE_ESCAPED" \
  136. --arg url "$PR_URL" \
  137. --argjson color ${{ steps.action-info.outputs.color }} \
  138. --arg thumbnail_url "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png" \
  139. --arg pr_number "#${PR_NUMBER}" \
  140. --arg author_name "$AUTHOR_LOGIN" \
  141. --arg author_url "$AUTHOR_URL" \
  142. --arg repo_name "${{ github.event.repository.name }}" \
  143. --arg repo_url "${{ github.event.repository.html_url }}" \
  144. --arg branch "${HEAD_REF} → ${BASE_REF}" \
  145. --arg labels "$PR_LABELS" \
  146. --arg status "$PR_STATE" \
  147. --arg pr_url "$PR_URL" \
  148. --arg timestamp "$CREATED_AT" \
  149. --arg footer_text "Workout Cool • PR ${{ steps.action-info.outputs.action_text }}" \
  150. --arg footer_icon "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png" \
  151. '{
  152. "avatar_url": $avatar_url,
  153. "embeds": [
  154. {
  155. "title": $title,
  156. "description": $description,
  157. "url": $url,
  158. "color": $color,
  159. "thumbnail": {
  160. "url": $thumbnail_url
  161. },
  162. "fields": [
  163. {
  164. "name": "📋 PR #",
  165. "value": $pr_number,
  166. "inline": true
  167. },
  168. {
  169. "name": "👤 Author",
  170. "value": ("[\($author_name)](\($author_url))"),
  171. "inline": true
  172. },
  173. {
  174. "name": "📁 Repository",
  175. "value": ("[\($repo_name)](\($repo_url))"),
  176. "inline": true
  177. },
  178. {
  179. "name": "🌿 Branch",
  180. "value": $branch,
  181. "inline": true
  182. },
  183. {
  184. "name": "🏷️ Labels",
  185. "value": $labels,
  186. "inline": true
  187. },
  188. {
  189. "name": "📊 Status",
  190. "value": $status,
  191. "inline": true
  192. },
  193. {
  194. "name": "🔗 View PR",
  195. "value": ("[Pull Request](\($pr_url))"),
  196. "inline": true
  197. }
  198. ],
  199. "timestamp": $timestamp,
  200. "footer": {
  201. "text": $footer_text,
  202. "icon_url": $footer_icon
  203. }
  204. }
  205. ]
  206. }' > discord_payload.json
  207. - name: Send Discord notification
  208. run: |
  209. curl -H "Content-Type: application/json" \
  210. -d @discord_payload.json \
  211. "${{ secrets.DISCORD_PR_WEBHOOK }}"