@@ -11,79 +11,40 @@ jobs:
1111 contents : read
1212 steps :
1313 - name : Checkout repository
14- uses : actions/checkout@v4
14+ uses : actions/checkout@v5
1515 with :
1616 fetch-depth : 0
1717
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 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- const fileCount = (commit.added?.length || 0) + (commit.removed?.length || 0) + (commit.modified?.length || 0);
36-
37- const commitFields = [
38- { name: `Commit ${sha}`, value: msg, inline: false }
39- ];
40- if (body.trim() !== '') commitFields.push({ name: 'Details', value: body, inline: false });
41- commitFields.push(
42- { name: 'Files Updated', value: `${fileCount} file(s)`, inline: true },
43- { name: 'By', value: author, inline: true }
44- );
45-
46- return commitFields;
47- }).flat();
48-
49- const embed = {
50- title: 'TDS Statistics Editor Update',
51- description: `Push contains ${commits.length} commit(s)`,
52- color: 5763719,
53- fields,
54- footer: { text: 'Thank you for using the Statisics Editor!' },
55- timestamp: new Date().toISOString()
56- };
18+ - name : Get commit and file details
19+ id : commit
20+ run : |
21+ COMMITS_JSON=$(git log --oneline ${{ github.event.before }}..${{ github.event.after }} --pretty=format:'{"sha":"%h","message":"%s","author":"%an","full_message":"%B"}' | jq -s .)
22+
23+ echo "commits_json=$COMMITS_JSON" >> $GITHUB_OUTPUT
5724
58- const payload = { embeds: [embed] };
59-
60- const { request } = require('https');
61- const u = new URL(webhookUrl);
62- const data = JSON.stringify(payload);
63-
64- const options = {
65- hostname: u.hostname,
66- path: u.pathname + u.search,
67- method: 'POST',
68- headers: {
69- 'Content-Type': 'application/json',
70- 'Content-Length': Buffer.byteLength(data),
71- 'User-Agent': 'github-actions/discord-notify'
72- }
73- };
74-
75- const req = request(options, res => {
76- let body = '';
77- res.on('data', d => body += d);
78- res.on('end', () => {
79- if (res.statusCode < 200 || res.statusCode >= 300) {
80- core.setFailed(`Failed to post to Discord: HTTP ${res.statusCode} ${body}`);
81- } else {
82- core.info('Discord webhook posted successfully');
83- }
84- });
85- });
25+ - name : Get changed files count
26+ id : changed-files
27+ uses : tj-actions/changed-files@v44
28+ with :
29+ since_last_remote_commit : true
8630
87- req.on('error', err => core.setFailed(`Failed to post to Discord: ${err.message}`));
88- req.write(data);
89- req.end();
31+ - name : Send Discord notification
32+ uses : actions/github-script@v7
33+ with :
34+ script : |
35+ const { sendDiscordNotification } = require('./.github/scripts/webhook.js');
36+
37+ const commits = JSON.parse(process.env.COMMITS_JSON || '[]');
38+ const fileCount = parseInt(process.env.FILE_COUNT || '0');
39+
40+ await sendDiscordNotification(
41+ process.env.DISCORD_WEBHOOK_URL,
42+ commits,
43+ fileCount
44+ );
45+
46+ console.log('Discord webhook posted successfully');
47+ env :
48+ DISCORD_WEBHOOK_URL : ${{ secrets.DISCORD_WEBHOOK_URL }}
49+ COMMITS_JSON : ${{ steps.commit.outputs.commits_json }}
50+ FILE_COUNT : ${{ steps.changed-files.outputs.changed_files_count }}
0 commit comments