@@ -11,79 +11,62 @@ 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();
18+ - name : Get commit and file details
19+ id : commit
20+ run : |
21+ BEFORE_SHA="${{ github.event.before }}"
22+ AFTER_SHA="${{ github.event.after }}"
23+
24+ if [ "$BEFORE_SHA" = "0000000000000000000000000000000000000000" ] || ! git cat-file -e "$BEFORE_SHA" 2>/dev/null; then
25+ COMMITS_JSON=$(git log --oneline -10 --pretty=format:'{"sha":"%h","message":"%s","author":"%an"}' |
26+ while IFS= read -r line; do
27+ sha=$(echo "$line" | jq -r '.sha')
28+ full_message=$(git show --no-patch --format="%B" $sha | jq -R -s .)
29+ echo "$line" | jq --arg full_message "$full_message" '. + {full_message: $full_message}'
30+ done | jq -s -c .)
31+
32+ CHANGED_FILES=$(git show --name-only --pretty="" $AFTER_SHA)
33+ FILE_COUNT=$(echo "$CHANGED_FILES" | wc -l)
34+ else
35+ COMMITS_JSON=$(git log --oneline "$BEFORE_SHA..$AFTER_SHA" --pretty=format:'{"sha":"%h","message":"%s","author":"%an"}' |
36+ while IFS= read -r line; do
37+ sha=$(echo "$line" | jq -r '.sha')
38+ full_message=$(git show --no-patch --format="%B" $sha | jq -R -s .)
39+ echo "$line" | jq --arg full_message "$full_message" '. + {full_message: $full_message}'
40+ done | jq -s -c .)
41+
42+ CHANGED_FILES=$(git diff --name-only "$BEFORE_SHA" "$AFTER_SHA")
43+ FILE_COUNT=$(echo "$CHANGED_FILES" | wc -l)
44+ fi
4845
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- };
46+ ESCAPED_COMMITS_JSON=$(echo "$COMMITS_JSON" | jq -R -s .)
47+
48+ echo "commits_json=$ESCAPED_COMMITS_JSON" >> $GITHUB_OUTPUT
49+ echo "file_count=$FILE_COUNT" >> $GITHUB_OUTPUT
5750
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- });
86-
87- req.on('error', err => core.setFailed(`Failed to post to Discord: ${err.message}`));
88- req.write(data);
89- req.end();
51+ - name : Send Discord notification
52+ uses : actions/github-script@v7
53+ with :
54+ script : |
55+ const { sendDiscordNotification } = require('./.github/scripts/webhook.js');
56+
57+ // Parse the escaped JSON
58+ const commitsJsonString = process.env.COMMITS_JSON;
59+ const commits = JSON.parse(commitsJsonString);
60+ const fileCount = parseInt(process.env.FILE_COUNT || '0');
61+
62+ await sendDiscordNotification(
63+ process.env.DISCORD_WEBHOOK_URL,
64+ commits,
65+ fileCount
66+ );
67+
68+ console.log('Discord webhook posted successfully');
69+ env :
70+ DISCORD_WEBHOOK_URL : ${{ secrets.DISCORD_WEBHOOK_URL }}
71+ COMMITS_JSON : ${{ steps.commit.outputs.commits_json }}
72+ FILE_COUNT : ${{ steps.commit.outputs.file_count }}
0 commit comments