notify-discord-issues.yml 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. name: Discord Issue Notification
  2. on:
  3. issues:
  4. types: [opened, closed, reopened]
  5. workflow_dispatch:
  6. inputs:
  7. issue_number:
  8. description: "Issue number to notify about"
  9. required: true
  10. type: string
  11. jobs:
  12. Discord:
  13. runs-on: ubuntu-latest
  14. name: Discord Issue Notifier
  15. steps:
  16. - uses: actions/checkout@v4
  17. - name: Get issue info
  18. id: issue-info
  19. run: |
  20. if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
  21. # For manual trigger, get the specific issue
  22. ISSUE_NUMBER="${{ github.event.inputs.issue_number }}"
  23. # Get issue info via GitHub CLI
  24. ISSUE_INFO=$(gh issue view "$ISSUE_NUMBER" --json number,title,body,url,author,state,createdAt,closedAt,labels)
  25. echo "number=$(echo "$ISSUE_INFO" | jq -r '.number')" >> $GITHUB_OUTPUT
  26. echo "title=$(echo "$ISSUE_INFO" | jq -r '.title')" >> $GITHUB_OUTPUT
  27. # Use EOF for the body which may contain special characters
  28. echo "body<<EOF" >> $GITHUB_OUTPUT
  29. echo "$ISSUE_INFO" | jq -r '.body' >> $GITHUB_OUTPUT
  30. echo "EOF" >> $GITHUB_OUTPUT
  31. echo "html_url=$(echo "$ISSUE_INFO" | jq -r '.url')" >> $GITHUB_OUTPUT
  32. echo "author_login=$(echo "$ISSUE_INFO" | jq -r '.author.login')" >> $GITHUB_OUTPUT
  33. echo "author_html_url=https://github.com/$(echo "$ISSUE_INFO" | jq -r '.author.login')" >> $GITHUB_OUTPUT
  34. echo "state=$(echo "$ISSUE_INFO" | jq -r '.state')" >> $GITHUB_OUTPUT
  35. echo "created_at=$(echo "$ISSUE_INFO" | jq -r '.createdAt')" >> $GITHUB_OUTPUT
  36. echo "closed_at=$(echo "$ISSUE_INFO" | jq -r '.closedAt // empty')" >> $GITHUB_OUTPUT
  37. # Format labels
  38. LABELS=$(echo "$ISSUE_INFO" | jq -r '.labels[]?.name' | tr '\n' ',' | sed 's/,$//')
  39. echo "labels=${LABELS}" >> $GITHUB_OUTPUT
  40. # Set action to "opened" for manual dispatch
  41. echo "action=opened" >> $GITHUB_OUTPUT
  42. else
  43. # For automatic trigger, use event data
  44. echo "number=${{ github.event.issue.number }}" >> $GITHUB_OUTPUT
  45. echo "title=${{ github.event.issue.title }}" >> $GITHUB_OUTPUT
  46. # Use EOF for the automatic issue body as well
  47. echo "body<<EOF" >> $GITHUB_OUTPUT
  48. echo "${{ github.event.issue.body }}" >> $GITHUB_OUTPUT
  49. echo "EOF" >> $GITHUB_OUTPUT
  50. echo "html_url=${{ github.event.issue.html_url }}" >> $GITHUB_OUTPUT
  51. echo "author_login=${{ github.event.issue.user.login }}" >> $GITHUB_OUTPUT
  52. echo "author_html_url=${{ github.event.issue.user.html_url }}" >> $GITHUB_OUTPUT
  53. echo "state=${{ github.event.issue.state }}" >> $GITHUB_OUTPUT
  54. echo "created_at=${{ github.event.issue.created_at }}" >> $GITHUB_OUTPUT
  55. echo "closed_at=${{ github.event.issue.closed_at }}" >> $GITHUB_OUTPUT
  56. echo "action=${{ github.event.action }}" >> $GITHUB_OUTPUT
  57. # Format labels from event
  58. LABELS="${{ join(github.event.issue.labels.*.name, ',') }}"
  59. echo "labels=${LABELS}" >> $GITHUB_OUTPUT
  60. fi
  61. env:
  62. GH_TOKEN: ${{ github.token }}
  63. - name: Set action emoji and color
  64. id: action-style
  65. run: |
  66. case "${{ steps.issue-info.outputs.action }}" in
  67. "opened")
  68. echo "emoji=🆕" >> $GITHUB_OUTPUT
  69. echo "color=5763719" >> $GITHUB_OUTPUT
  70. echo "action_text=New Issue" >> $GITHUB_OUTPUT
  71. ;;
  72. "closed")
  73. echo "emoji=✅" >> $GITHUB_OUTPUT
  74. echo "color=3066993" >> $GITHUB_OUTPUT
  75. echo "action_text=Issue Closed" >> $GITHUB_OUTPUT
  76. ;;
  77. "reopened")
  78. echo "emoji=🔄" >> $GITHUB_OUTPUT
  79. echo "color=15158332" >> $GITHUB_OUTPUT
  80. echo "action_text=Issue Reopened" >> $GITHUB_OUTPUT
  81. ;;
  82. *)
  83. echo "emoji=📝" >> $GITHUB_OUTPUT
  84. echo "color=5763719" >> $GITHUB_OUTPUT
  85. echo "action_text=Issue Updated" >> $GITHUB_OUTPUT
  86. ;;
  87. esac
  88. - name: Create Discord webhook payload
  89. run: |
  90. # Truncate body if too long (Discord has limits)
  91. BODY="${{ steps.issue-info.outputs.body }}"
  92. if [ ${#BODY} -gt 500 ]; then
  93. BODY="${BODY:0:500}..."
  94. fi
  95. # Prepare labels field
  96. LABELS="${{ steps.issue-info.outputs.labels }}"
  97. if [ -n "$LABELS" ]; then
  98. LABELS_FIELD="{\"name\": \"🏷️ Labels\", \"value\": \"$(echo $LABELS | sed 's/,/, /g')\", \"inline\": true},"
  99. else
  100. LABELS_FIELD=""
  101. fi
  102. # Set timestamp based on action
  103. if [ "${{ steps.issue-info.outputs.action }}" = "closed" ] && [ -n "${{ steps.issue-info.outputs.closed_at }}" ]; then
  104. TIMESTAMP="${{ steps.issue-info.outputs.closed_at }}"
  105. else
  106. TIMESTAMP="${{ steps.issue-info.outputs.created_at }}"
  107. fi
  108. # Create a temporary JSON file
  109. cat > discord_payload.json << EOF
  110. {
  111. "avatar_url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png",
  112. "embeds": [
  113. {
  114. "title": "${{ steps.action-style.outputs.emoji }} ${{ steps.action-style.outputs.action_text }}: #${{ steps.issue-info.outputs.number }} - ${{ steps.issue-info.outputs.title }}",
  115. "description": "${BODY}",
  116. "url": "${{ steps.issue-info.outputs.html_url }}",
  117. "color": ${{ steps.action-style.outputs.color }},
  118. "thumbnail": {
  119. "url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
  120. },
  121. "fields": [
  122. {
  123. "name": "📋 Issue #",
  124. "value": "#${{ steps.issue-info.outputs.number }}",
  125. "inline": true
  126. },
  127. {
  128. "name": "👤 Author",
  129. "value": "[${{ steps.issue-info.outputs.author_login }}](${{ steps.issue-info.outputs.author_html_url }})",
  130. "inline": true
  131. },
  132. {
  133. "name": "📁 Repository",
  134. "value": "[${{ github.event.repository.name }}](${{ github.event.repository.html_url }})",
  135. "inline": true
  136. },
  137. ${LABELS_FIELD}
  138. {
  139. "name": "🔗 View Issue",
  140. "value": "[Open on GitHub](${{ steps.issue-info.outputs.html_url }})",
  141. "inline": true
  142. }
  143. ],
  144. "timestamp": "${TIMESTAMP}",
  145. "footer": {
  146. "text": "Workout Cool • Issues",
  147. "icon_url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
  148. }
  149. }
  150. ]
  151. }
  152. EOF
  153. - name: Send Discord notification
  154. run: |
  155. curl -H "Content-Type: application/json" \
  156. -d @discord_payload.json \
  157. "${{ secrets.DISCORD_ISSUES_WEBHOOK }}"