|
| 1 | +name: Discord Notify |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + username: |
| 7 | + description: Discord webhook username |
| 8 | + required: false |
| 9 | + default: DEVtheOPS Bot |
| 10 | + type: string |
| 11 | + avatar_url: |
| 12 | + description: Discord webhook avatar URL |
| 13 | + required: false |
| 14 | + default: https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png |
| 15 | + type: string |
| 16 | + color: |
| 17 | + description: Decimal embed color override |
| 18 | + required: false |
| 19 | + default: "5793266" |
| 20 | + type: string |
| 21 | + title_prefix: |
| 22 | + description: Optional prefix added to the embed title |
| 23 | + required: false |
| 24 | + default: "" |
| 25 | + type: string |
| 26 | + include_body: |
| 27 | + description: Include issue, PR, or release body in the embed description |
| 28 | + required: false |
| 29 | + default: true |
| 30 | + type: boolean |
| 31 | + secrets: |
| 32 | + discord_webhook: |
| 33 | + description: Discord webhook URL |
| 34 | + required: true |
| 35 | + |
| 36 | +jobs: |
| 37 | + notify: |
| 38 | + name: Send Discord notification |
| 39 | + runs-on: ubuntu-latest |
| 40 | + permissions: |
| 41 | + contents: read |
| 42 | + issues: read |
| 43 | + pull-requests: read |
| 44 | + steps: |
| 45 | + - name: Build payload |
| 46 | + id: payload |
| 47 | + uses: actions/github-script@v7 |
| 48 | + env: |
| 49 | + DISCORD_USERNAME: ${{ inputs.username }} |
| 50 | + DISCORD_AVATAR_URL: ${{ inputs.avatar_url }} |
| 51 | + DISCORD_COLOR: ${{ inputs.color }} |
| 52 | + DISCORD_TITLE_PREFIX: ${{ inputs.title_prefix }} |
| 53 | + DISCORD_INCLUDE_BODY: ${{ inputs.include_body }} |
| 54 | + with: |
| 55 | + script: | |
| 56 | + const eventName = context.eventName; |
| 57 | + const action = context.payload.action || ""; |
| 58 | + const repo = context.repo; |
| 59 | + const prefix = process.env.DISCORD_TITLE_PREFIX || ""; |
| 60 | + const defaultColor = Number.parseInt(process.env.DISCORD_COLOR || "5793266", 10) || 5793266; |
| 61 | + const includeBody = (process.env.DISCORD_INCLUDE_BODY || "true") === "true"; |
| 62 | +
|
| 63 | + const truncate = (value, limit) => { |
| 64 | + if (!value) return ""; |
| 65 | + const normalized = String(value).replace(/\r\n/g, "\n").trim(); |
| 66 | + if (!normalized) return ""; |
| 67 | + return normalized.length > limit ? `${normalized.slice(0, limit - 1)}…` : normalized; |
| 68 | + }; |
| 69 | +
|
| 70 | + const author = { |
| 71 | + name: `${repo.owner}/${repo.repo}`, |
| 72 | + url: `https://github.com/${repo.owner}/${repo.repo}`, |
| 73 | + icon_url: `https://github.com/${repo.owner}.png` |
| 74 | + }; |
| 75 | +
|
| 76 | + const fields = [ |
| 77 | + { name: "Repository", value: `[${repo.owner}/${repo.repo}](https://github.com/${repo.owner}/${repo.repo})`, inline: true }, |
| 78 | + { name: "Actor", value: `[${context.actor}](https://github.com/${context.actor})`, inline: true }, |
| 79 | + { name: "Event", value: `${eventName}${action ? `.${action}` : ""}`, inline: true } |
| 80 | + ]; |
| 81 | +
|
| 82 | + let title = `GitHub event in ${repo.repo}`; |
| 83 | + let url = `https://github.com/${repo.owner}/${repo.repo}`; |
| 84 | + let description = ""; |
| 85 | + let color = defaultColor; |
| 86 | +
|
| 87 | + if (eventName === "issues" && context.payload.issue) { |
| 88 | + const issue = context.payload.issue; |
| 89 | + title = `Issue #${issue.number} opened: ${issue.title}`; |
| 90 | + url = issue.html_url; |
| 91 | + description = includeBody ? truncate(issue.body, 4000) : ""; |
| 92 | + color = 16098851; |
| 93 | + fields.push( |
| 94 | + { name: "Author", value: `[${issue.user.login}](${issue.user.html_url})`, inline: true }, |
| 95 | + { name: "Labels", value: issue.labels.length ? issue.labels.map((label) => label.name).join(", ") : "None", inline: true } |
| 96 | + ); |
| 97 | + } |
| 98 | +
|
| 99 | + if (eventName === "pull_request" && context.payload.pull_request) { |
| 100 | + const pr = context.payload.pull_request; |
| 101 | + title = `PR #${pr.number} opened: ${pr.title}`; |
| 102 | + url = pr.html_url; |
| 103 | + description = includeBody ? truncate(pr.body, 4000) : ""; |
| 104 | + color = 3447003; |
| 105 | + fields.push( |
| 106 | + { name: "Author", value: `[${pr.user.login}](${pr.user.html_url})`, inline: true }, |
| 107 | + { name: "Branch", value: `\`${pr.head.ref}\` -> \`${pr.base.ref}\``, inline: true } |
| 108 | + ); |
| 109 | + } |
| 110 | +
|
| 111 | + if (eventName === "release" && context.payload.release) { |
| 112 | + const release = context.payload.release; |
| 113 | + title = `Release published: ${release.name || release.tag_name}`; |
| 114 | + url = release.html_url; |
| 115 | + description = includeBody ? truncate(release.body, 4000) : ""; |
| 116 | + color = 10181046; |
| 117 | + fields.push( |
| 118 | + { name: "Tag", value: `\`${release.tag_name}\``, inline: true }, |
| 119 | + { name: "Prerelease", value: release.prerelease ? "Yes" : "No", inline: true } |
| 120 | + ); |
| 121 | + } |
| 122 | +
|
| 123 | + if (prefix) { |
| 124 | + title = `${prefix} ${title}`; |
| 125 | + } |
| 126 | +
|
| 127 | + const payload = { |
| 128 | + username: process.env.DISCORD_USERNAME || "DEVtheOPS Bot", |
| 129 | + avatar_url: process.env.DISCORD_AVATAR_URL || "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png", |
| 130 | + embeds: [ |
| 131 | + { |
| 132 | + title, |
| 133 | + url, |
| 134 | + description, |
| 135 | + color, |
| 136 | + author, |
| 137 | + fields, |
| 138 | + timestamp: new Date().toISOString(), |
| 139 | + footer: { |
| 140 | + text: "GitHub Actions" |
| 141 | + } |
| 142 | + } |
| 143 | + ] |
| 144 | + }; |
| 145 | +
|
| 146 | + core.setOutput("json", JSON.stringify(payload)); |
| 147 | +
|
| 148 | + - name: Post to Discord |
| 149 | + env: |
| 150 | + DISCORD_WEBHOOK: ${{ secrets.discord_webhook }} |
| 151 | + DISCORD_PAYLOAD: ${{ steps.payload.outputs.json }} |
| 152 | + run: | |
| 153 | + curl --fail --silent --show-error \ |
| 154 | + --connect-timeout 10 \ |
| 155 | + --max-time 30 \ |
| 156 | + --retry 3 \ |
| 157 | + --retry-delay 2 \ |
| 158 | + -H "Content-Type: application/json" \ |
| 159 | + -d "$DISCORD_PAYLOAD" \ |
| 160 | + "$DISCORD_WEBHOOK" |
0 commit comments