|
| 1 | +# .github/workflows/connector-discord.yml |
| 2 | +# Discord connector — posts to Discord on git events |
| 3 | +# Set org secret: DISCORD_WEBHOOK_URL |
| 4 | + |
| 5 | +name: "Connector: Discord" |
| 6 | + |
| 7 | +on: |
| 8 | + push: |
| 9 | + branches: [main] |
| 10 | + pull_request: |
| 11 | + types: [opened, closed] |
| 12 | + issues: |
| 13 | + types: [opened, closed] |
| 14 | + release: |
| 15 | + types: [published] |
| 16 | + |
| 17 | +permissions: |
| 18 | + contents: read |
| 19 | + |
| 20 | +jobs: |
| 21 | + notify: |
| 22 | + name: "Discord Notify" |
| 23 | + runs-on: ubuntu-latest |
| 24 | + if: secrets.DISCORD_WEBHOOK_URL != '' |
| 25 | + |
| 26 | + steps: |
| 27 | + - name: Send Discord notification |
| 28 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 |
| 29 | + env: |
| 30 | + DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_URL }} |
| 31 | + with: |
| 32 | + script: | |
| 33 | + const webhook = process.env.DISCORD_WEBHOOK; |
| 34 | + if (!webhook) return; |
| 35 | +
|
| 36 | + const event = context.eventName; |
| 37 | + const repo = `${context.repo.owner}/${context.repo.repo}`; |
| 38 | + let embed = { color: 0xFF2255, footer: { text: 'BlackRoad OS' }, timestamp: new Date().toISOString() }; |
| 39 | +
|
| 40 | + if (event === 'push') { |
| 41 | + const commits = context.payload.commits || []; |
| 42 | + embed.title = `Push to ${repo}`; |
| 43 | + embed.description = commits.map(c => `[\`${c.id.substring(0,7)}\`](${c.url}) ${c.message.split('\n')[0].substring(0, 80)}`).join('\n'); |
| 44 | + embed.color = 0x4488FF; |
| 45 | + } |
| 46 | + else if (event === 'pull_request') { |
| 47 | + const pr = context.payload.pull_request; |
| 48 | + const merged = context.payload.action === 'closed' && pr.merged; |
| 49 | + embed.title = `PR ${merged ? 'merged' : context.payload.action}: ${pr.title}`; |
| 50 | + embed.url = pr.html_url; |
| 51 | + embed.description = `${repo} • +${pr.additions} -${pr.deletions} • ${pr.changed_files} files`; |
| 52 | + embed.color = merged ? 0x22C55E : context.payload.action === 'opened' ? 0x8844FF : 0xFF6B2B; |
| 53 | + } |
| 54 | + else if (event === 'issues') { |
| 55 | + const issue = context.payload.issue; |
| 56 | + embed.title = `Issue ${context.payload.action}: ${issue.title}`; |
| 57 | + embed.url = issue.html_url; |
| 58 | + embed.description = `${repo} • ${issue.labels.map(l => l.name).join(', ') || 'no labels'}`; |
| 59 | + embed.color = context.payload.action === 'opened' ? 0xFF2255 : 0x22C55E; |
| 60 | + } |
| 61 | + else if (event === 'release') { |
| 62 | + const release = context.payload.release; |
| 63 | + embed.title = `Release: ${release.tag_name}`; |
| 64 | + embed.url = release.html_url; |
| 65 | + embed.description = release.body?.substring(0, 500) || repo; |
| 66 | + embed.color = 0x00D4FF; |
| 67 | + } |
| 68 | +
|
| 69 | + await fetch(webhook, { |
| 70 | + method: 'POST', |
| 71 | + headers: { 'Content-Type': 'application/json' }, |
| 72 | + body: JSON.stringify({ embeds: [embed] }) |
| 73 | + }); |
| 74 | + console.log('Discord notification sent'); |
0 commit comments