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