Skip to content

Commit f2e6eaa

Browse files
committed
refactor: overall better webhook system
also testing webhook
1 parent 803ba93 commit f2e6eaa

File tree

3 files changed

+52
-39
lines changed

3 files changed

+52
-39
lines changed

.github/scripts/sendwebhook.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const fs = require('fs');
2+
const { sendDiscordNotification } = require('./webhook.js');
3+
4+
async function main() {
5+
const eventPath = process.env.GITHUB_EVENT_PATH;
6+
const eventData = fs.readFileSync(eventPath, 'utf8');
7+
const event = JSON.parse(eventData);
8+
9+
if (!event.commits || event.commits.length === 0) {
10+
console.log('No commits in this push event');
11+
return;
12+
}
13+
14+
const commits = event.commits.map(commit => ({
15+
sha: commit.id,
16+
message: commit.message.split('\n')[0],
17+
full_message: commit.message,
18+
author: commit.author.name
19+
}));
20+
21+
const filesChanged = new Set();
22+
event.commits.forEach(commit => {
23+
commit.added.forEach(file => filesChanged.add(file));
24+
commit.removed.forEach(file => filesChanged.add(file));
25+
commit.modified.forEach(file => filesChanged.add(file));
26+
});
27+
28+
const fileCount = filesChanged.size;
29+
30+
const webhookUrl = process.env.DISCORD_WEBHOOK_URL;
31+
if (!webhookUrl) {
32+
throw new Error('DISCORD_WEBHOOK_URL is not set');
33+
}
34+
35+
await sendDiscordNotification(webhookUrl, commits, fileCount);
36+
console.log('Notification sent successfully');
37+
}
38+
39+
main().catch(error => {
40+
console.error(error);
41+
process.exit(1);
42+
});

.github/scripts/webhook.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,4 @@ async function sendDiscordNotification(webhookUrl, commits, fileCount) {
8181
});
8282
}
8383

84-
module.exports = { sendDiscordNotification };
84+
module.exports = { sendDiscordNotification };

.github/workflows/webhook.yml

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,23 @@
1-
name: Discord Commit Notification
1+
name: Discord Notification on Push
22

33
on:
44
push:
5-
branches: main
5+
branches:
6+
- main
67

78
jobs:
8-
notify-discord:
9+
notify:
910
runs-on: ubuntu-latest
10-
permissions:
11-
contents: read
1211
steps:
1312
- name: Checkout repository
1413
uses: actions/checkout@v5
15-
with:
16-
fetch-depth: 0
17-
18-
- name: Get commit and file details
19-
id: commit
20-
run: |
21-
COMMITS_JSON=$(git log --oneline ${{ github.event.before }}..${{ github.event.after }} --format='%h%x00%an%x00%s%x00%B%x00' | jq -R -s 'split("") | map(select(. != "")) as $lines | [range(0; $lines | length; 4) as $i | $lines[$i:$i+4]] | map({sha: .[0], author: .[1], message: .[2], full_message: .[3]}) | .')
22-
23-
echo "commits_json<<EOF" >> $GITHUB_OUTPUT
24-
echo "$COMMITS_JSON" >> $GITHUB_OUTPUT
25-
echo "EOF" >> $GITHUB_OUTPUT
26-
27-
- name: Get changed files count
28-
id: changed-files
29-
uses: tj-actions/changed-files@v44
30-
with:
31-
since_last_remote_commit: true
3214

33-
- name: Send Discord notification
34-
uses: actions/github-script@v7
15+
- name: Set up Node.js
16+
uses: actions/setup-node@v4
3517
with:
36-
script: |
37-
const { sendDiscordNotification } = require('./.github/scripts/webhook.js');
38-
39-
const commits = JSON.parse(process.env.COMMITS_JSON || '[]');
40-
const fileCount = parseInt(process.env.FILE_COUNT || '0');
41-
42-
await sendDiscordNotification(
43-
process.env.DISCORD_WEBHOOK_URL,
44-
commits,
45-
fileCount
46-
);
18+
node-version: '22'
4719

48-
console.log('Discord webhook posted successfully');
20+
- name: Send Discord Notification
4921
env:
5022
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
51-
COMMITS_JSON: ${{ steps.commit.outputs.commits_json }}
52-
FILE_COUNT: ${{ steps.changed-files.outputs.all_changed_files_count }}
23+
run: node .github/scripts/sendwebhook.js

0 commit comments

Comments
 (0)