Add Discord commit notifications workflow #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Discord Commit Updates | ||
| on: | ||
| push: | ||
| branches: [ "main" ] | ||
| workflow_dispatch: | ||
| inputs: | ||
| count: | ||
| description: "How many commits to backfill (last N commits)" | ||
| required: false | ||
| default: "50" | ||
| delay_ms: | ||
| description: "Delay between messages (ms) for backfill" | ||
| required: false | ||
| default: "900" | ||
| jobs: | ||
| notify: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout (full history so backfill works) | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
| - name: Post to Discord | ||
| env: | ||
| DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} | ||
| REPO: ${{ github.repository }} | ||
| BRANCH: ${{ github.ref_name }} | ||
| ACTOR: ${{ github.actor }} | ||
| EVENT_NAME: ${{ github.event_name }} | ||
| COUNT: ${{ inputs.count }} | ||
| DELAY_MS: ${{ inputs.delay_ms }} | ||
| BEFORE: ${{ github.event.before }} | ||
| AFTER: ${{ github.sha }} | ||
| run: | | ||
| set -euo pipefail | ||
| if [ -z "${DISCORD_WEBHOOK_URL:-}" ]; then | ||
| echo "Missing DISCORD_WEBHOOK_URL secret." | ||
| exit 1 | ||
| fi | ||
| post() { | ||
| python3 - <<'PY' | ||
| import os, json, urllib.request, sys | ||
| url = os.environ["DISCORD_WEBHOOK_URL"] | ||
| content = os.environ["CONTENT"] | ||
| data = json.dumps({"content": content}).encode("utf-8") | ||
| req = urllib.request.Request(url, data=data, headers={"Content-Type":"application/json"}) | ||
| with urllib.request.urlopen(req) as r: | ||
| r.read() | ||
| PY | ||
| } | ||
| sleep_ms() { | ||
| python3 - <<'PY' | ||
| import os, time | ||
| time.sleep(int(os.environ.get("DELAY_MS","900"))/1000.0) | ||
| PY | ||
| } | ||
| header=f"🛠️ **{os.environ['REPO']}** (`{os.environ['BRANCH']}`)" | ||
| export HEADER="$(python3 - <<'PY' | ||
| import os | ||
| print(f"🛠️ **{os.environ['REPO']}** (`{os.environ['BRANCH']}`)") | ||
| PY | ||
| )" | ||
| if [ "${EVENT_NAME}" = "push" ]; then | ||
| # One summary message for the push | ||
| if git rev-parse "${BEFORE}" >/dev/null 2>&1; then | ||
| range="${BEFORE}..${AFTER}" | ||
| commits="$(git log --pretty=format:'- `%h` **%an**: %s' "$range")" | ||
| else | ||
| commits="$(git log -n 20 --pretty=format:'- `%h` **%an**: %s')" | ||
| fi | ||
| msg="${HEADER}\n✅ **New push by ${ACTOR}**\n${commits}" | ||
| # Discord limit ~2000 chars; truncate | ||
| export CONTENT="$(python3 - <<'PY' | ||
| import os | ||
| m=os.environ["MSG"] | ||
| print(m[:1900] + ("\n…(truncated)" if len(m)>1900 else "")) | ||
| PY | ||
| )" | ||
| export MSG="$msg" | ||
| export CONTENT="$(python3 - <<'PY' | ||
| import os | ||
| m=os.environ["MSG"] | ||
| print(m[:1900] + ("\n…(truncated)" if len(m)>1900 else "")) | ||
| PY | ||
| )" | ||
| post | ||
| else | ||
| # Backfill last N commits oldest -> newest | ||
| n="${COUNT:-50}" | ||
| mapfile -t lines < <(git log -n "$n" --pretty=format:'%h|%an|%ad|%s' --date=short) | ||
| for ((i=${#lines[@]}-1; i>=0; i--)); do | ||
| IFS='|' read -r h a d s <<< "${lines[i]}" | ||
| export CONTENT="${HEADER}\n📌 **Backfill** — ${d}\n- \`${h}\` **${a}**: ${s}" | ||
| post | ||
| sleep_ms | ||
| done | ||
| export CONTENT="${HEADER}\n✅ Backfill complete. (${n} commits)" | ||
| post | ||
| fi | ||