Skip to content

Commit c7110fd

Browse files
committed
Discord Pushs
1 parent a2a8efc commit c7110fd

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

.github/scripts/discord_push.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
if [ -z "${DISCORD_WEBHOOK_URL:-}" ]; then
5+
echo "Missing DISCORD_WEBHOOK_URL secret."
6+
exit 1
7+
fi
8+
9+
REPO="${GITHUB_REPOSITORY}"
10+
BRANCH="${GITHUB_REF_NAME}"
11+
ACTOR="${GITHUB_ACTOR}"
12+
13+
# On push events, GitHub gives BEFORE. On manual runs, it may not.
14+
BEFORE="${GITHUB_EVENT_BEFORE:-}"
15+
AFTER="${GITHUB_SHA}"
16+
17+
if [ -n "${BEFORE}" ] && git rev-parse "${BEFORE}" >/dev/null 2>&1; then
18+
range="${BEFORE}..${AFTER}"
19+
commits="$(git log --pretty=format:'- `%h` **%an**: %s' "$range")"
20+
else
21+
commits="$(git log -n 20 --pretty=format:'- `%h` **%an**: %s')"
22+
fi
23+
24+
msg="🛠️ **${REPO}** (\`${BRANCH}\`)
25+
✅ **New push by ${ACTOR}**
26+
${commits}"
27+
28+
python3 - "$msg" <<'PY'
29+
import json, os, sys, urllib.request, urllib.error
30+
31+
url = os.environ["DISCORD_WEBHOOK_URL"]
32+
content = sys.argv[1]
33+
34+
if len(content) > 1900:
35+
content = content[:1900] + "\n…(truncated)"
36+
37+
data = json.dumps({"content": content}).encode("utf-8")
38+
req = urllib.request.Request(url, data=data, headers={"Content-Type":"application/json"})
39+
40+
try:
41+
with urllib.request.urlopen(req) as r:
42+
print("Discord OK:", r.status)
43+
r.read()
44+
except urllib.error.HTTPError as e:
45+
body = e.read().decode("utf-8", errors="replace")
46+
print("Discord ERROR:", e.code, e.reason)
47+
print("Discord body:", body)
48+
raise
49+
PY
50+
51+

.github/workflows/discord-push.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Discord Push Updates
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
workflow_dispatch: {}
8+
9+
jobs:
10+
notify:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
with:
15+
fetch-depth: 50
16+
17+
- name: Post commits to Discord
18+
env:
19+
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
20+
run: |
21+
chmod +x .github/scripts/discord_push.sh
22+
.github/scripts/discord_push.sh

0 commit comments

Comments
 (0)