Merge pull request #44 from Valorless/main #134
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 | |
| 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 }} | |
| GITHUB_TOKEN: ${{ github.token }} | |
| 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') | |
| # Skip merge commits | |
| if echo "$msg" | grep -qE '^Merge pull request|^Merge branch'; then | |
| echo "Skipping merge commit: $msg" | |
| continue | |
| fi | |
| # Skip commits that are associated with a PR | |
| pr_count=$(curl -s \ | |
| -H "Authorization: Bearer $GITHUB_TOKEN" \ | |
| -H "Accept: application/vnd.github+json" \ | |
| "https://api.github.com/repos/$REPO/commits/$sha/pulls" \ | |
| | jq 'if type == "array" then length else 0 end') | |
| if [ "$pr_count" -gt 0 ]; then | |
| echo "Skipping commit $sha — associated with a PR" | |
| continue | |
| fi | |
| 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 }} | |
| PR_MERGED_AT: ${{ github.event.pull_request.merged_at }} | |
| DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }} | |
| run: | | |
| cat > /tmp/discord_pr_payload.py <<'PY' | |
| import os, json, datetime, time | |
| 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', '?') | |
| merged_at = os.environ.get('PR_MERGED_AT', '') | |
| try: | |
| if merged_at: | |
| dt = datetime.datetime.fromisoformat(merged_at.replace('Z', '+00:00')) | |
| unix_time = int(dt.timestamp()) | |
| else: | |
| unix_time = int(time.time()) | |
| except Exception: | |
| unix_time = int(time.time()) | |
| 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}, | |
| { | |
| "name": "", | |
| "value": f"🔀 Merged • <t:{unix_time}:f> (<t:{unix_time}:R>)", | |
| "inline": False | |
| } | |
| ], | |
| "color": 6559412, | |
| } | |
| if pr_body: | |
| embed["description"] = pr_body | |
| print(json.dumps({"embeds": [embed]}, ensure_ascii=False)) | |
| PY | |
| PAYLOAD=$(python3 /tmp/discord_pr_payload.py) | |
| curl -s -H "Content-Type: application/json" -d "$PAYLOAD" "$DISCORD_WEBHOOK" |