|
| 1 | +const core = require('@actions/core'); |
| 2 | +const github = require('@actions/github'); |
| 3 | + |
| 4 | +async function sendToDiscord(webhookUrl, embed) { |
| 5 | + const res = await fetch(webhookUrl, { |
| 6 | + method: 'POST', |
| 7 | + headers: { 'Content-Type': 'application/json' }, |
| 8 | + body: JSON.stringify({ embeds: [embed] }) |
| 9 | + }); |
| 10 | + if (!res.ok) { |
| 11 | + throw new Error(`Discord responded with ${res.status}: ${await res.text()}`); |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +function buildPushEmbed(payload) { |
| 16 | + const repo = payload.repository.full_name; |
| 17 | + const branch = payload.ref.replace('refs/heads/', ''); |
| 18 | + const commits = (payload.commits || []).slice(0, 5); |
| 19 | + |
| 20 | + const description = commits.map(c => { |
| 21 | + const hash = c.id.substring(0, 7); |
| 22 | + const msg = c.message.split('\n')[0].substring(0, 72); |
| 23 | + return `[\`${hash}\`](${c.url}) ${msg}`; |
| 24 | + }).join('\n') || 'No commits'; |
| 25 | + |
| 26 | + return { |
| 27 | + title: `Push to \`${branch}\``, |
| 28 | + url: `https://github.com/${repo}/commits/${branch}`, |
| 29 | + color: 0x28a745, |
| 30 | + description, |
| 31 | + author: { name: payload.pusher.name }, |
| 32 | + footer: { text: repo }, |
| 33 | + timestamp: new Date().toISOString() |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +function buildPREmbed(payload) { |
| 38 | + const pr = payload.pull_request; |
| 39 | + const action = payload.action; |
| 40 | + const repo = payload.repository.full_name; |
| 41 | + |
| 42 | + let color = 0x0075ca; |
| 43 | + if (action === 'closed' && pr.merged) color = 0x6f42c1; |
| 44 | + else if (action === 'closed') color = 0xcb2431; |
| 45 | + |
| 46 | + const actionLabel = action === 'closed' && pr.merged ? 'merged' : action; |
| 47 | + |
| 48 | + return { |
| 49 | + title: `PR ${actionLabel}: ${pr.title}`, |
| 50 | + url: pr.html_url, |
| 51 | + color, |
| 52 | + description: (pr.body || '').substring(0, 300) || null, |
| 53 | + author: { name: pr.user.login, icon_url: pr.user.avatar_url }, |
| 54 | + fields: [{ name: 'Branch', value: `\`${pr.head.ref}\` → \`${pr.base.ref}\``, inline: true }], |
| 55 | + footer: { text: repo }, |
| 56 | + timestamp: pr.updated_at |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +function buildIssueEmbed(payload) { |
| 61 | + const issue = payload.issue; |
| 62 | + const action = payload.action; |
| 63 | + const repo = payload.repository.full_name; |
| 64 | + |
| 65 | + return { |
| 66 | + title: `Issue ${action}: ${issue.title}`, |
| 67 | + url: issue.html_url, |
| 68 | + color: action === 'closed' ? 0xcb2431 : 0x28a745, |
| 69 | + description: (issue.body || '').substring(0, 300) || null, |
| 70 | + author: { name: issue.user.login, icon_url: issue.user.avatar_url }, |
| 71 | + footer: { text: repo }, |
| 72 | + timestamp: issue.updated_at |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +async function run() { |
| 77 | + try { |
| 78 | + const webhookUrl = core.getInput('discord-webhook-url', { required: true }); |
| 79 | + const { eventName, payload } = github.context; |
| 80 | + |
| 81 | + core.info(`Processing event: ${eventName}`); |
| 82 | + |
| 83 | + let embed; |
| 84 | + |
| 85 | + if (eventName === 'push') { |
| 86 | + embed = buildPushEmbed(payload); |
| 87 | + } else if (eventName === 'pull_request') { |
| 88 | + if (!['opened', 'closed', 'reopened'].includes(payload.action)) { |
| 89 | + core.info(`Skipping pull_request action: ${payload.action}`); |
| 90 | + return; |
| 91 | + } |
| 92 | + embed = buildPREmbed(payload); |
| 93 | + } else if (eventName === 'issues') { |
| 94 | + if (!['opened', 'closed', 'reopened'].includes(payload.action)) { |
| 95 | + core.info(`Skipping issues action: ${payload.action}`); |
| 96 | + return; |
| 97 | + } |
| 98 | + embed = buildIssueEmbed(payload); |
| 99 | + } else { |
| 100 | + core.info(`Unsupported event: ${eventName}`); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + await sendToDiscord(webhookUrl, embed); |
| 105 | + core.info('Discord notification sent'); |
| 106 | + } catch (err) { |
| 107 | + core.setFailed(err.message); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +run(); |
0 commit comments