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 (all commits in push)
19+ uses : actions/github-script@v6
20+ env :
21+ DISCORD_WEBHOOK_URL : ${{ secrets.DISCORD_WEBHOOK_URL }}
22+ with :
23+ script : |
24+ const core = require('@actions/core');
25+
26+ const webhookUrl = process.env.DISCORD_WEBHOOK_URL;
27+ if (!webhookUrl) core.setFailed('DISCORD_WEBHOOK_URL secret is missing');
28+
29+ const commits = context.payload?.commits;
30+ if (!commits || commits.length === 0) core.setFailed('No commits found in this push event');
31+
32+ const fields = commits.map(commit => {
33+ const sha = commit.id?.slice(0,7) || '(unknown)';
34+ const msg = commit.message?.split('\n')[0] || '(no message)';
35+ const body = commit.message?.split('\n').slice(1).join('\n') || '';
36+ const author = commit.author?.name || commit.author?.username || '(unknown)';
37+ const fileCount = (commit.added?.length || 0) + (commit.removed?.length || 0) + (commit.modified?.length || 0);
38+
39+ const commitFields = [
40+ { name: `Commit ${sha}`, value: msg, inline: false }
41+ ];
42+ if (body.trim() !== '') commitFields.push({ name: 'Details', value: body, inline: false });
43+ commitFields.push(
44+ { name: 'Files Updated', value: `${fileCount} file(s)`, inline: true },
45+ { name: 'By', value: author, inline: true }
46+ );
47+
48+ return commitFields;
49+ }).flat();
50+
51+ const embed = {
52+ title: 'TDS Statistics Editor Update',
53+ description: `Push contains ${commits.length} commit(s)`,
54+ color: 5763719,
55+ fields,
56+ footer: { text: 'TDS Stats Editor' },
57+ timestamp: new Date().toISOString()
58+ };
59+
60+ const payload = { embeds: [embed] };
61+
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