Skip to content

Commit 03f93fb

Browse files
committed
chore: move telegram notify to scripts
1 parent 2c6d718 commit 03f93fb

2 files changed

Lines changed: 40 additions & 34 deletions

File tree

.github/workflows/notify.yml

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ env:
2121
GITHUB_REPOSITORY: ${{ github.repository }}
2222
GITHUB_REF: ${{ github.ref }}
2323
GITHUB_SHA: ${{ github.sha }}
24+
GITHUB_EVENT_PATH: ${{ github.event_path }}
2425

2526
jobs:
2627
telegram-notify:
@@ -40,37 +41,4 @@ jobs:
4041
run: npm install node-fetch
4142

4243
- name: Send Telegram notification
43-
run: |
44-
node -e "
45-
import fetch from 'node-fetch';
46-
47-
const token = process.env.TELEGRAM_BOT_TOKEN;
48-
const chatId = process.env.TELEGRAM_CHAT_ID;
49-
const actor = process.env.GITHUB_ACTOR;
50-
const event = process.env.GITHUB_EVENT_NAME;
51-
const repo = process.env.GITHUB_REPOSITORY;
52-
const ref = process.env.GITHUB_REF;
53-
const sha = process.env.GITHUB_SHA;
54-
55-
let message = `*GitHub Notification*\nRepository: ${repo}\nActor: ${actor}\nEvent: ${event}\nRef: ${ref}\nSHA: ${sha}`;
56-
57-
if (event === 'pull_request') {
58-
const pr = JSON.parse(process.env.GITHUB_EVENT_PATH);
59-
message += `\nPR Title: ${pr.pull_request.title}\nAction: ${pr.action}\nURL: ${pr.pull_request.html_url}`;
60-
}
61-
62-
if (event === 'issues') {
63-
const issue = JSON.parse(process.env.GITHUB_EVENT_PATH);
64-
message += `\nIssue Title: ${issue.issue.title}\nAction: ${issue.action}\nURL: ${issue.issue.html_url}`;
65-
}
66-
67-
fetch(`https://api.telegram.org/bot${token}/sendMessage`, {
68-
method: 'POST',
69-
headers: { 'Content-Type': 'application/json' },
70-
body: JSON.stringify({
71-
chat_id: chatId,
72-
text: message,
73-
parse_mode: 'Markdown'
74-
})
75-
});
76-
"
44+
run: node scripts/telegram-notify.js

scripts/telegram-notify.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import fetch from 'node-fetch';
2+
import fs from 'fs';
3+
4+
const token = process.env.TELEGRAM_BOT_TOKEN;
5+
const chatId = process.env.TELEGRAM_CHAT_ID;
6+
const actor = process.env.GITHUB_ACTOR;
7+
const event = process.env.GITHUB_EVENT_NAME;
8+
const repo = process.env.GITHUB_REPOSITORY;
9+
const ref = process.env.GITHUB_REF;
10+
const sha = process.env.GITHUB_SHA;
11+
const payloadPath = process.env.GITHUB_EVENT_PATH;
12+
13+
let message = `*GitHub Notification*\nRepository: ${repo}\nActor: ${actor}\nEvent: ${event}\nRef: ${ref}\nSHA: ${sha}`;
14+
15+
if (payloadPath && fs.existsSync(payloadPath)) {
16+
const payload = JSON.parse(fs.readFileSync(payloadPath, 'utf8'));
17+
18+
if (event === 'pull_request') {
19+
const pr = payload.pull_request;
20+
message += `\nPR Title: ${pr.title}\nAction: ${payload.action}\nURL: ${pr.html_url}`;
21+
}
22+
23+
if (event === 'issues') {
24+
const issue = payload.issue;
25+
message += `\nIssue Title: ${issue.title}\nAction: ${payload.action}\nURL: ${issue.html_url}`;
26+
}
27+
}
28+
29+
await fetch(`https://api.telegram.org/bot${token}/sendMessage`, {
30+
method: 'POST',
31+
headers: { 'Content-Type': 'application/json' },
32+
body: JSON.stringify({
33+
chat_id: chatId,
34+
text: message,
35+
parse_mode: 'Markdown'
36+
})
37+
});
38+

0 commit comments

Comments
 (0)