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<> $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<> $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) 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 # Save the body content safely echo "body_content<> $GITHUB_OUTPUT echo "$BODY" >> $GITHUB_OUTPUT echo "BODY_CONTENT_EOF" >> $GITHUB_OUTPUT # 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 with --arg for all values 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 }}" \ --arg description "${{ steps.prepare-content.outputs.body_content }}" \ --arg url "${{ steps.issue-info.outputs.html_url }}" \ --arg 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 | tonumber), "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 }}"