Skip to content

Commit 96c82c6

Browse files
committed
chore: move telegram notify script to .github/scripts
1 parent 371b016 commit 96c82c6

3 files changed

Lines changed: 66 additions & 41 deletions

File tree

.github/scripts/telegram-notify.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import fetch from 'node-fetch'
2+
import fs from 'fs'
3+
4+
const {
5+
TELEGRAM_BOT_TOKEN,
6+
TELEGRAM_CHAT_ID,
7+
GITHUB_EVENT_NAME,
8+
GITHUB_EVENT_PATH,
9+
GITHUB_REPOSITORY,
10+
GITHUB_ACTOR,
11+
GITHUB_REF,
12+
} = process.env
13+
14+
if (!TELEGRAM_BOT_TOKEN || !TELEGRAM_CHAT_ID) {
15+
console.error('Telegram env vars are missing')
16+
process.exit(1)
17+
}
18+
19+
let message = `*GitHub Notification*\n\n`
20+
message += `Repo: ${GITHUB_REPOSITORY}\n`
21+
message += `Actor: ${GITHUB_ACTOR}\n`
22+
message += `Event: ${GITHUB_EVENT_NAME}\n`
23+
24+
if (GITHUB_EVENT_PATH && fs.existsSync(GITHUB_EVENT_PATH)) {
25+
const payload = JSON.parse(fs.readFileSync(GITHUB_EVENT_PATH, 'utf8'))
26+
27+
if (GITHUB_EVENT_NAME === 'push') {
28+
message += `\n*Push*\n`
29+
message += `Branch: ${GITHUB_REF?.replace('refs/heads/', '')}\n`
30+
31+
payload.commits?.slice(0, 3).forEach((c, i) => {
32+
message += `${i + 1}. ${c.message.split('\n')[0]}\n`
33+
})
34+
}
35+
36+
if (GITHUB_EVENT_NAME === 'pull_request' || GITHUB_EVENT_NAME === 'pull_request_target') {
37+
const pr = payload.pull_request
38+
message += `\n*Pull Request*\n`
39+
message += `Title: ${pr.title}\n`
40+
message += `Action: ${payload.action}\n`
41+
message += `URL: ${pr.html_url}\n`
42+
}
43+
44+
if (GITHUB_EVENT_NAME === 'issues') {
45+
const issue = payload.issue
46+
message += `\n*Issue*\n`
47+
message += `Title: ${issue.title}\n`
48+
message += `Action: ${payload.action}\n`
49+
message += `URL: ${issue.html_url}\n`
50+
}
51+
}
52+
53+
await fetch(`https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage`, {
54+
method: 'POST',
55+
headers: {
56+
'Content-Type': 'application/json'
57+
},
58+
body: JSON.stringify({
59+
chat_id: TELEGRAM_CHAT_ID,
60+
text: message,
61+
parse_mode: 'Markdown'
62+
})
63+
})

.github/workflows/notify.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ on:
55
branches:
66
- main
77
- develop
8-
pull_request:
9-
types: [opened, closed, reopened, merged]
8+
pull_request_target:
9+
types: [opened, reopened, closed]
1010
issues:
1111
types: [opened, closed, reopened]
1212

@@ -41,4 +41,4 @@ jobs:
4141
run: npm install node-fetch
4242

4343
- name: Send Telegram notification
44-
run: node scripts/telegram-notify.js
44+
run: node .github/scripts/telegram-notify.js

scripts/telegram-notify.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)