123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- name: Discord Issue Notification
- on:
- issues:
- types: [opened, closed, reopened]
- workflow_dispatch:
- inputs:
- issue_number:
- description: "Issue number to notify about"
- required: true
- type: string
- jobs:
- Discord:
- runs-on: ubuntu-latest
- name: Discord Issue Notifier
- steps:
- - uses: actions/checkout@v4
- - name: Get issue info
- id: issue-info
- run: |
- if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
- # For manual trigger, get the specific issue
- ISSUE_NUMBER="${{ github.event.inputs.issue_number }}"
-
- # Get issue info via GitHub CLI
- ISSUE_INFO=$(gh issue view "$ISSUE_NUMBER" --json number,title,body,url,author,state,createdAt,closedAt,labels)
-
- echo "number=$(echo "$ISSUE_INFO" | jq -r '.number')" >> $GITHUB_OUTPUT
- echo "title=$(echo "$ISSUE_INFO" | jq -r '.title')" >> $GITHUB_OUTPUT
-
- # Use EOF for the body which may contain special characters
- echo "body<<EOF" >> $GITHUB_OUTPUT
- echo "$ISSUE_INFO" | jq -r '.body' >> $GITHUB_OUTPUT
- echo "EOF" >> $GITHUB_OUTPUT
-
- echo "html_url=$(echo "$ISSUE_INFO" | jq -r '.url')" >> $GITHUB_OUTPUT
- echo "author_login=$(echo "$ISSUE_INFO" | jq -r '.author.login')" >> $GITHUB_OUTPUT
- echo "author_html_url=https://github.com/$(echo "$ISSUE_INFO" | jq -r '.author.login')" >> $GITHUB_OUTPUT
- echo "state=$(echo "$ISSUE_INFO" | jq -r '.state')" >> $GITHUB_OUTPUT
- echo "created_at=$(echo "$ISSUE_INFO" | jq -r '.createdAt')" >> $GITHUB_OUTPUT
- echo "closed_at=$(echo "$ISSUE_INFO" | jq -r '.closedAt // empty')" >> $GITHUB_OUTPUT
-
- # Format labels
- LABELS=$(echo "$ISSUE_INFO" | jq -r '.labels[]?.name' | tr '\n' ',' | sed 's/,$//')
- echo "labels=${LABELS}" >> $GITHUB_OUTPUT
-
- # Set action to "opened" for manual dispatch
- echo "action=opened" >> $GITHUB_OUTPUT
- else
- # For automatic trigger, use event data
- echo "number=${{ github.event.issue.number }}" >> $GITHUB_OUTPUT
- echo "title=${{ github.event.issue.title }}" >> $GITHUB_OUTPUT
-
- # Use EOF for the automatic issue body as well
- echo "body<<EOF" >> $GITHUB_OUTPUT
- echo "${{ github.event.issue.body }}" >> $GITHUB_OUTPUT
- echo "EOF" >> $GITHUB_OUTPUT
-
- echo "html_url=${{ github.event.issue.html_url }}" >> $GITHUB_OUTPUT
- echo "author_login=${{ github.event.issue.user.login }}" >> $GITHUB_OUTPUT
- echo "author_html_url=${{ github.event.issue.user.html_url }}" >> $GITHUB_OUTPUT
- echo "state=${{ github.event.issue.state }}" >> $GITHUB_OUTPUT
- echo "created_at=${{ github.event.issue.created_at }}" >> $GITHUB_OUTPUT
- echo "closed_at=${{ github.event.issue.closed_at }}" >> $GITHUB_OUTPUT
- echo "action=${{ github.event.action }}" >> $GITHUB_OUTPUT
-
- # Format labels from event
- LABELS="${{ join(github.event.issue.labels.*.name, ',') }}"
- echo "labels=${LABELS}" >> $GITHUB_OUTPUT
- fi
- env:
- GH_TOKEN: ${{ github.token }}
- - name: Set action emoji and color
- id: action-style
- run: |
- case "${{ steps.issue-info.outputs.action }}" in
- "opened")
- echo "emoji=🆕" >> $GITHUB_OUTPUT
- echo "color=5763719" >> $GITHUB_OUTPUT
- echo "action_text=New Issue" >> $GITHUB_OUTPUT
- ;;
- "closed")
- echo "emoji=✅" >> $GITHUB_OUTPUT
- echo "color=3066993" >> $GITHUB_OUTPUT
- echo "action_text=Issue Closed" >> $GITHUB_OUTPUT
- ;;
- "reopened")
- echo "emoji=🔄" >> $GITHUB_OUTPUT
- echo "color=15158332" >> $GITHUB_OUTPUT
- echo "action_text=Issue Reopened" >> $GITHUB_OUTPUT
- ;;
- *)
- echo "emoji=📝" >> $GITHUB_OUTPUT
- echo "color=5763719" >> $GITHUB_OUTPUT
- echo "action_text=Issue Updated" >> $GITHUB_OUTPUT
- ;;
- esac
- - name: Prepare issue content
- id: prepare-content
- run: |
- # Create a temp file for the body content
- cat > body_content.txt << 'BODY_EOF'
- ${{ steps.issue-info.outputs.body }}
- BODY_EOF
- # Truncate body if too long (Discord has limits) and escape for JSON
- BODY_LENGTH=$(wc -c < body_content.txt)
- if [ $BODY_LENGTH -gt 500 ]; then
- BODY=$(head -c 500 body_content.txt)
- BODY="${BODY}..."
- else
- BODY=$(cat body_content.txt)
- fi
- # Escape the body content for JSON
- BODY_ESCAPED=$(echo "$BODY" | jq -Rs .)
- echo "body_escaped=${BODY_ESCAPED}" >> $GITHUB_OUTPUT
- # Prepare labels field
- LABELS="${{ steps.issue-info.outputs.labels }}"
- if [ -n "$LABELS" ]; then
- LABELS_FORMATTED=$(echo "$LABELS" | sed 's/,/, /g')
- echo "labels_field={\"name\": \"🏷️ Labels\", \"value\": \"${LABELS_FORMATTED}\", \"inline\": true}," >> $GITHUB_OUTPUT
- else
- echo "labels_field=" >> $GITHUB_OUTPUT
- fi
- # Set timestamp based on action
- if [ "${{ steps.issue-info.outputs.action }}" = "closed" ] && [ -n "${{ steps.issue-info.outputs.closed_at }}" ]; then
- echo "timestamp=${{ steps.issue-info.outputs.closed_at }}" >> $GITHUB_OUTPUT
- else
- echo "timestamp=${{ steps.issue-info.outputs.created_at }}" >> $GITHUB_OUTPUT
- fi
- - name: Create Discord webhook payload
- run: |
- # Create the Discord payload using jq to properly escape everything
- jq -n \
- --arg avatar_url "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png" \
- --arg title "${{ steps.action-style.outputs.emoji }} ${{ steps.action-style.outputs.action_text }}: #${{ steps.issue-info.outputs.number }} - ${{ steps.issue-info.outputs.title }}" \
- --argjson description ${{ steps.prepare-content.outputs.body_escaped }} \
- --arg url "${{ steps.issue-info.outputs.html_url }}" \
- --argjson color ${{ steps.action-style.outputs.color }} \
- --arg thumbnail_url "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png" \
- --arg issue_number "#${{ steps.issue-info.outputs.number }}" \
- --arg author_name "${{ steps.issue-info.outputs.author_login }}" \
- --arg author_url "${{ steps.issue-info.outputs.author_html_url }}" \
- --arg repo_name "${{ github.event.repository.name }}" \
- --arg repo_url "${{ github.event.repository.html_url }}" \
- --arg issue_url "${{ steps.issue-info.outputs.html_url }}" \
- --arg timestamp "${{ steps.prepare-content.outputs.timestamp }}" \
- --arg labels "${{ steps.issue-info.outputs.labels }}" \
- '{
- "avatar_url": $avatar_url,
- "embeds": [
- {
- "title": $title,
- "description": $description,
- "url": $url,
- "color": $color,
- "thumbnail": {
- "url": $thumbnail_url
- },
- "fields": ([
- {
- "name": "📋 Issue #",
- "value": $issue_number,
- "inline": true
- },
- {
- "name": "👤 Author",
- "value": "[\($author_name)](\($author_url))",
- "inline": true
- },
- {
- "name": "📁 Repository",
- "value": "[\($repo_name)](\($repo_url))",
- "inline": true
- }
- ] + (if $labels != "" then [{
- "name": "🏷️ Labels",
- "value": ($labels | split(",") | join(", ")),
- "inline": true
- }] else [] end) + [{
- "name": "🔗 View Issue",
- "value": "[Open on GitHub](\($issue_url))",
- "inline": true
- }]),
- "timestamp": $timestamp,
- "footer": {
- "text": "Workout Cool • Issues",
- "icon_url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
- }
- }
- ]
- }' > discord_payload.json
- - name: Send Discord notification
- run: |
- curl -H "Content-Type: application/json" \
- -d @discord_payload.json \
- "${{ secrets.DISCORD_ISSUES_WEBHOOK }}"
|