Skip to content

Change footer text from 'πŸ”€ Merged' to 'β†’ Merged' #116

Change footer text from 'πŸ”€ Merged' to 'β†’ Merged'

Change footer text from 'πŸ”€ Merged' to 'β†’ Merged' #116

name: Discord Commit
on:
push:
branches:
- '**'
pull_request:
types: [closed]
jobs:
# ──────────────────────────────────────────────────────────────
# Job 1: Per-commit notifications β€” only for direct pushes,
# NOT for pushes that belong to an open pull request.
# ──────────────────────────────────────────────────────────────
discord_commit_notify:
if: github.event_name == 'push' && github.event.pull_request == null
runs-on: ubuntu-latest
env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
REPO: ${{ github.repository }}
GITHUB_EVENT_PATH: ${{ github.event_path }}
REF: ${{ github.ref }}
steps:
- name: Checkout (optional)
uses: actions/checkout@v4
- name: Send a Discord message per commit
shell: bash
run: |
set -euo pipefail
if ! command -v jq >/dev/null 2>&1; then
echo "Installing jq..."
sudo apt-get update -y
sudo apt-get install -y jq
fi
if [ -z "${GITHUB_EVENT_PATH:-}" ] || [ ! -s "$GITHUB_EVENT_PATH" ]; then
echo "GITHUB_EVENT_PATH is missing or empty, nothing to do."
exit 0
fi
commits_count=$(jq '.commits | length' "$GITHUB_EVENT_PATH")
if [ "$commits_count" -eq 0 ]; then
echo "No commits found in the push event."
exit 0
fi
BRANCH=${REF#refs/heads/}
cat > /tmp/discord_payload.py <<'PY'
import os, json, datetime, time
sha = os.environ.get('SHA', '')
msg = os.environ.get('COMMIT_MSG', '').strip().replace('\r', '')
author = os.environ.get('AUTHOR', 'unknown')
timestamp = os.environ.get('TIMESTAMP', '')
repo = os.environ.get('REPO', '')
branch = os.environ.get('BRANCH', '')
short_sha = sha[:7] if sha else "(no sha)"
commit_url = f"https://github.com/{repo}/commit/{sha}" if sha else ""
try:
if timestamp:
dt = datetime.datetime.fromisoformat(timestamp.replace('Z', '+00:00'))
unix_time = int(dt.timestamp())
else:
unix_time = int(time.time())
except Exception:
unix_time = int(time.time())
lines = msg.split("\n", 1)
title = lines[0].strip() if lines and lines[0].strip() != "" else "(no title)"
body = lines[1].strip() if len(lines) > 1 else ""
embed = {
"author": {
"name": author,
"icon_url": f"https://github.com/{author}.png?size=64"
},
"title": title or "(no title)",
**({"description": body} if body else {}),
"fields": [
{
"name": "",
"value": f"[`{short_sha}`]({commit_url}) - {branch} - <t:{unix_time}:f> (<t:{unix_time}:R>)",
"inline": True
}
],
"color": 7506394
}
print(json.dumps({"embeds": [embed]}, ensure_ascii=False))
PY
jq -c '.commits[]' "$GITHUB_EVENT_PATH" | while read -r commit_json; do
sha=$(echo "$commit_json" | jq -r '.id // empty')
msg=$(echo "$commit_json" | jq -r '.message // empty')
author_username=$(echo "$commit_json" | jq -r '.author.username // empty')
author_name=$(echo "$commit_json" | jq -r '.author.name // empty')
author_email=$(echo "$commit_json" | jq -r '.author.email // empty')
timestamp=$(echo "$commit_json" | jq -r '.timestamp // empty')
if [ -n "$author_username" ]; then
author="$author_username"
elif [ -n "$author_name" ]; then
author="$author_name"
elif [ -n "$author_email" ]; then
author="$author_email"
else
author="unknown"
fi
export SHA="$sha" COMMIT_MSG="$msg" AUTHOR="$author" TIMESTAMP="$timestamp" REPO="$REPO" BRANCH="$BRANCH"
PAYLOAD=$(python3 /tmp/discord_payload.py)
curl -s -H "Content-Type: application/json" -d "$PAYLOAD" "$DISCORD_WEBHOOK"
sleep 0.5
done
# ──────────────────────────────────────────────────────────────
# Job 2: Single PR notification β€” only when merged
# ──────────────────────────────────────────────────────────────
discord_pr_notify:
if: github.event_name == 'pull_request' && github.event.pull_request.merged == true
runs-on: ubuntu-latest
env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
steps:
- name: Send Discord PR merged notification
shell: bash
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
PR_TITLE: ${{ github.event.pull_request.title }}
PR_BODY: ${{ github.event.pull_request.body }}
PR_URL: ${{ github.event.pull_request.html_url }}
PR_AUTHOR: ${{ github.event.pull_request.user.login }}
PR_HEAD: ${{ github.event.pull_request.head.ref }}
PR_BASE: ${{ github.event.pull_request.base.ref }}
PR_COMMITS: ${{ github.event.pull_request.commits }}
PR_ADDITIONS: ${{ github.event.pull_request.additions }}
PR_DELETIONS: ${{ github.event.pull_request.deletions }}
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
run: |
cat > /tmp/discord_pr_payload.py <<'PY'
import os, json, urllib.request
pr_number = os.environ.get('PR_NUMBER', '')
pr_title = os.environ.get('PR_TITLE', '(no title)')
pr_body = (os.environ.get('PR_BODY') or '').strip()[:500]
pr_url = os.environ.get('PR_URL', '')
pr_author = os.environ.get('PR_AUTHOR', 'unknown')
head = os.environ.get('PR_HEAD', '')
base = os.environ.get('PR_BASE', '')
commits = os.environ.get('PR_COMMITS', '?')
additions = os.environ.get('PR_ADDITIONS', '?')
deletions = os.environ.get('PR_DELETIONS', '?')
webhook_url = os.environ.get('DISCORD_WEBHOOK', '')
embed = {
"author": {
"name": pr_author,
"icon_url": f"https://github.com/{pr_author}.png?size=64"
},
"title": f"[PR #{pr_number}] {pr_title}",
"url": pr_url,
"fields": [
{"name": "Branch", "value": f"`{head}` β†’ `{base}`", "inline": True},
{"name": "Commits", "value": str(commits), "inline": True},
{"name": "Changes", "value": f"+{additions} / -{deletions}", "inline": True},
],
"footer": {"text": "β†’ Merged"},
"color": 6559412,
}
if pr_body:
embed["description"] = pr_body
payload = json.dumps({"embeds": [embed]}, ensure_ascii=False)
req = urllib.request.Request(
webhook_url,
data=payload.encode('utf-8'),
headers={'Content-Type': 'application/json'},
method='POST'
)
with urllib.request.urlopen(req) as resp:
print(f"Discord response: {resp.status}")
PY
python3 /tmp/discord_pr_payload.py