Repository navigation
146 lines (129 loc) · 5.63 KB
/
Copy pathcommit-notifications.yml
File metadata and controls
146 lines (129 loc) · 5.63 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
name: Commit Notification
on:
push:
workflow_dispatch:
inputs:
sha:
description: "Commit SHA to (re-)send a notification for"
required: true
jobs:
notify:
if: github.repository == 'DadaCap/Right-Shulker-Box'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Send commit notification to Discord
env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
REPO: ${{ github.repository }}
REF_NAME: ${{ github.ref_name }}
ACTOR: ${{ github.actor }}
COMPARE: ${{ github.event.compare }}
HEAD_MESSAGE: ${{ github.event.head_commit.message }}
BEFORE: ${{ github.event.before }}
AFTER: ${{ github.sha }}
COMMITS: ${{ toJson(github.event.commits) }}
DISPATCH_SHA: ${{ github.event.inputs.sha }}
run: |
set -euo pipefail
if [ -z "$DISCORD_WEBHOOK" ]; then
echo "Error: DISCORD_WEBHOOK secret not set"
exit 1
fi
if [ -n "$DISPATCH_SHA" ]; then
AFTER=$(git rev-parse "$DISPATCH_SHA")
BEFORE=$(git rev-parse "$AFTER~1")
COMPARE="https://github.com/$REPO/commit/$AFTER"
HEAD_MESSAGE=$(git log -1 --format=%B "$AFTER")
COMMITS=$(jq -n \
--arg id "$AFTER" \
--arg message "$HEAD_MESSAGE" \
--arg url "https://github.com/$REPO/commit/$AFTER" \
--arg author "$(git log -1 --format=%an "$AFTER")" \
'[{id: $id, message: $message, url: $url, author: {name: $author}}]')
fi
FILE_LIST=$(git diff --name-status "$BEFORE" "$AFTER" | sort -u)
FILES_CHANGED=$(printf '%s\n' "$FILE_LIST" | sed '/^$/d' | wc -l | tr -d ' ')
STATS=$(git diff --shortstat "$BEFORE" "$AFTER")
INSERTIONS=$(printf '%s\n' "$STATS" | grep -oE '[0-9]+ insertion' | grep -oE '[0-9]+' || true)
DELETIONS=$(printf '%s\n' "$STATS" | grep -oE '[0-9]+ deletion' | grep -oE '[0-9]+' || true)
INSERTIONS=${INSERTIONS:-0}
DELETIONS=${DELETIONS:-0}
# Get per-file stats using numstat for accurate insertion/deletion counts
NUMSTAT_OUTPUT=$(git diff --numstat "$BEFORE" "$AFTER")
if [ -z "$FILE_LIST" ]; then
FILE_BLOCK='```text
No files changed
```'
else
FILE_BLOCK_CONTENT=$(while IFS=$'\t' read -r status file; do
# Extract stats for this file from NUMSTAT_OUTPUT
FILE_STATS=$(printf '%s\n' "$NUMSTAT_OUTPUT" | grep "$file$")
FILE_INSERTIONS=$(printf '%s\n' "$FILE_STATS" | awk '{print $1}' || true)
FILE_DELETIONS=$(printf '%s\n' "$FILE_STATS" | awk '{print $2}' || true)
FILE_INSERTIONS=${FILE_INSERTIONS:-0}
FILE_DELETIONS=${FILE_DELETIONS:-0}
case "$status" in
M) echo "--- $file [+$FILE_INSERTIONS/-$FILE_DELETIONS]" ;;
A) echo "+ $file [+$FILE_INSERTIONS]" ;;
D) echo "- $file [-$FILE_DELETIONS]" ;;
esac
done <<< "$FILE_LIST")
FILE_BLOCK=$(printf '```diff\n%s\n```' "$FILE_BLOCK_CONTENT")
fi
HEAD_BLOCK=$(printf '```fix\n%s\n```' "$HEAD_MESSAGE")
COMMITS_JSON=$(printf '%s' "$COMMITS" | jq -c '
map({
id: .id[0:7],
message: (.message | split("\n")[0]),
url: .url,
author: (.author.name // "unknown")
})
')
EMBED_DESCRIPTION=$(printf '%s' "$COMMITS_JSON" | jq -r '
.[:10]
| map("- [`" + .id + "`](" + .url + ") " + .message + " — **" + .author + "**")
| join("\n")
')
TOTAL_COMMITS=$(printf '%s' "$COMMITS_JSON" | jq 'length')
if [ "$TOTAL_COMMITS" -gt 10 ]; then
EMBED_DESCRIPTION="$EMBED_DESCRIPTION\n\n...and $((TOTAL_COMMITS - 10)) more commit(s)."
fi
jq -n \
--arg repo "$REPO" \
--arg branch "$REF_NAME" \
--arg actor "$ACTOR" \
--arg compare "$COMPARE" \
--arg files_changed "$FILES_CHANGED" \
--arg insertions "$INSERTIONS" \
--arg deletions "$DELETIONS" \
--arg file_block "$FILE_BLOCK" \
--arg head_block "$HEAD_BLOCK" \
--arg desc "$EMBED_DESCRIPTION" \
'{
username: "GitHub",
avatar_url: "https://avatars.githubusercontent.com/u/37024673",
embeds: [
{
title: ("Push to " + $repo),
url: $compare,
color: 8323327,
fields: [
{ name: "Repo", value: $repo, inline: true },
{ name: "Branch", value: $branch, inline: true },
{ name: "Pushed By", value: $actor, inline: true },
{ name: "Files changed", value: $files_changed, inline: true },
{ name: "Lines inserted / deleted", value: ("+" + $insertions + " / -" + $deletions), inline: true },
{ name: "\u200b", value: "\u200b", inline: true },
{ name: "Changed files", value: $file_block, inline: false },
{ name: "Head commit", value: $head_block, inline: false }
],
description: $desc
}
]
}' \
| curl -sS -X POST "$DISCORD_WEBHOOK" \
-H "Content-Type: application/json" \
-d @-