-
-
Notifications
You must be signed in to change notification settings - Fork 238
68 lines (62 loc) · 2.42 KB
/
Copy pathcommit-log.yml
File metadata and controls
68 lines (62 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
name: Commit Log to Discord
on:
push:
branches: [main, master]
jobs:
discord:
runs-on: ubuntu-latest
steps:
- name: Send embed to Discord
env:
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_GITHUB }}
REPO_NAME: ${{ github.event.repository.name }}
REPOSITORY: ${{ github.repository }}
COMMIT_SHA: ${{ github.sha }}
COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
REF_NAME: ${{ github.ref_name }}
run: |
node - <<'EOF'
const https = require('https')
const webhook = process.env.DISCORD_WEBHOOK_URL
if (!webhook) {
console.log('⚠️ DISCORD_WEBHOOK_GITHUB not set — skipping Discord commit notification')
process.exit(0)
}
// 📖 Commit messages can contain quotes, apostrophes, backticks, and
// 📖 multi-line bodies. Build JSON in Node instead of interpolating
// 📖 directly into shell heredocs so any commit text is safe.
const repo = process.env.REPO_NAME || 'repository'
const repository = process.env.REPOSITORY || ''
const sha = process.env.COMMIT_SHA || ''
const shaShort = sha.slice(0, 7)
const ref = process.env.REF_NAME || 'main'
const firstLine = String(process.env.COMMIT_MESSAGE || 'No commit message')
.split(/\r?\n/)[0]
.slice(0, 240)
const body = JSON.stringify({
embeds: [{
title: `[${repo}] ${firstLine}`,
url: `https://github.com/${repository}/commit/${sha}`,
description: `Commit \`${shaShort}\` on \`${ref}\``,
color: 5814783,
footer: { text: repo },
}],
})
const url = new URL(webhook)
const req = https.request({
hostname: url.hostname,
path: url.pathname + url.search,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body),
},
}, (res) => {
console.log('Discord response status:', res.statusCode)
res.resume()
process.exit(res.statusCode >= 200 && res.statusCode < 300 ? 0 : 1)
})
req.on('error', (err) => { console.error(err); process.exit(1) })
req.write(body)
req.end()
EOF