Skip to content

Commit 4d33255

Browse files
committed
feat: update webhook
1 parent 8894399 commit 4d33255

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

.github/workflows/webhook.yml

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
name: Discord Commit Notification
2+
3+
on:
4+
push:
5+
branches: main
6+
7+
jobs:
8+
notify-discord:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
contents: read
12+
steps:
13+
- name: Checkout repository
14+
uses: actions/checkout@v4
15+
with:
16+
fetch-depth: 0
17+
18+
- name: Send Discord notification (push only)
19+
uses: actions/github-script@v6
20+
env:
21+
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
22+
with:
23+
script: |
24+
const webhookUrl = process.env.DISCORD_WEBHOOK_URL;
25+
if (!webhookUrl) core.setFailed('DISCORD_WEBHOOK_URL secret is missing');
26+
27+
const head = github.context.payload.head_commit;
28+
if (!head) core.setFailed('No head commit found in the push event');
29+
30+
const commitSha = head.id.slice(0, 7);
31+
const commitMsg = head.message.split('\n')[0] || '(no message)';
32+
const commitBody = head.message.split('\n').slice(1).join('\n');
33+
const author = head.author?.name || github.context.actor || '(unknown)';
34+
const fileCount = (head.added?.length || 0) + (head.removed?.length || 0) + (head.modified?.length || 0);
35+
const timestamp = head.timestamp || new Date().toISOString();
36+
37+
const color = 5763719;
38+
39+
const fields = [
40+
{ name: 'Commit Message', value: commitMsg, inline: false },
41+
];
42+
if (commitBody && commitBody.trim() !== '') {
43+
fields.push({ name: 'Details', value: commitBody, inline: false });
44+
}
45+
fields.push(
46+
{ name: 'Files Updated', value: `${fileCount} file(s)`, inline: true },
47+
{ name: 'By', value: author, inline: true }
48+
);
49+
50+
const embed = {
51+
title: 'Update',
52+
description: `TDS Statistics Editor has been updated to version ${commitSha}`,
53+
color,
54+
fields,
55+
footer: { text: 'TDS Stats Editor' },
56+
timestamp
57+
};
58+
59+
const payload = { embeds: [embed] };
60+
61+
// Post to Discord
62+
const { request } = require('https');
63+
const u = new URL(webhookUrl);
64+
const data = JSON.stringify(payload);
65+
66+
const options = {
67+
hostname: u.hostname,
68+
path: u.pathname + u.search,
69+
method: 'POST',
70+
headers: {
71+
'Content-Type': 'application/json',
72+
'Content-Length': Buffer.byteLength(data),
73+
'User-Agent': 'github-actions/discord-notify'
74+
}
75+
};
76+
77+
const req = request(options, res => {
78+
let body = '';
79+
res.on('data', d => body += d);
80+
res.on('end', () => {
81+
if (res.statusCode < 200 || res.statusCode >= 300) {
82+
core.setFailed(`Failed to post to Discord: HTTP ${res.statusCode} ${body}`);
83+
} else {
84+
core.info(`Discord webhook posted successfully`);
85+
}
86+
});
87+
});
88+
89+
req.on('error', err => core.setFailed(`Failed to post to Discord: ${err.message}`));
90+
req.write(data);
91+
req.end();

0 commit comments

Comments
 (0)