1- name : Discord Notification on Push
1+ name : Discord Commit Notification
22
33on :
44 push :
5- branches :
6- - main
5+ branches : main
76
87jobs :
9- notify :
8+ notify-discord :
109 runs-on : ubuntu-latest
10+ permissions :
11+ contents : read
1112 steps :
1213 - name : Checkout repository
1314 uses : actions/checkout@v5
14-
15- - name : Set up Node.js
16- uses : actions/setup-node@v4
1715 with :
18- node-version : ' 22 '
16+ fetch-depth : 0
1917
20- - name : Send Discord Notification
18+ - name : Send Discord notification (all commits in push)
19+ uses : actions/github-script@v6
2120 env :
2221 DISCORD_WEBHOOK_URL : ${{ secrets.DISCORD_WEBHOOK_URL }}
23- run : node .github/scripts/sendwebhook.js
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 commits = context.payload?.commits;
28+ if (!commits || commits.length === 0) core.setFailed('No commits found in this push event');
29+
30+ const fields = commits.map(commit => {
31+ const sha = commit.id?.slice(0,7) || '(unknown)';
32+ const msg = commit.message?.split('\n')[0] || '(no message)';
33+ const body = commit.message?.split('\n').slice(1).join('\n') || '';
34+ const author = commit.author?.name || commit.author?.username || '(unknown)';
35+
36+ const commitFields = [
37+ { name: `Commit ${sha}`, value: msg, inline: false }
38+ ];
39+ if (body.trim() !== '') commitFields.push({ name: 'Details', value: body, inline: false });
40+ commitFields.push(
41+ { name: 'By', value: author, inline: true }
42+ );
43+
44+ return commitFields;
45+ }).flat();
46+
47+ const embed = {
48+ title: 'TDS Statistics Editor Update',
49+ description: `Push contains ${commits.length} commit(s)`,
50+ color: 5763719,
51+ fields,
52+ footer: { text: 'Thank you for using the Statisics Editor!' },
53+ timestamp: new Date().toISOString()
54+ };
55+
56+ const payload = { embeds: [embed] };
57+
58+ const { request } = require('https');
59+ const u = new URL(webhookUrl);
60+ const data = JSON.stringify(payload);
61+
62+ const options = {
63+ hostname: u.hostname,
64+ path: u.pathname + u.search,
65+ method: 'POST',
66+ headers: {
67+ 'Content-Type': 'application/json',
68+ 'Content-Length': Buffer.byteLength(data),
69+ 'User-Agent': 'github-actions/discord-notify'
70+ }
71+ };
72+
73+ const req = request(options, res => {
74+ let body = '';
75+ res.on('data', d => body += d);
76+ res.on('end', () => {
77+ if (res.statusCode < 200 || res.statusCode >= 300) {
78+ core.setFailed(`Failed to post to Discord: HTTP ${res.statusCode} ${body}`);
79+ } else {
80+ core.info('Discord webhook posted successfully');
81+ }
82+ });
83+ });
84+
85+ req.on('error', err => core.setFailed(`Failed to post to Discord: ${err.message}`));
86+ req.write(data);
87+ req.end();
0 commit comments