|
| 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 | +}) |
0 commit comments