Skip to content

Build actionable notifications feed #1460

Build actionable notifications feed

Build actionable notifications feed #1460

name: Update PR leaderboard
on:
pull_request_target:
types:
- opened
permissions:
contents: write
pull-requests: read
concurrency:
group: leaderboard-json-update
cancel-in-progress: false
jobs:
update-leaderboard:
runs-on: ubuntu-latest
steps:
- name: Checkout default branch (trusted code only)
uses: actions/checkout@v4
with:
ref: ${{ github.event.repository.default_branch }}
fetch-depth: 0
persist-credentials: false
- name: Increment PR count in leaderboard.json
env:
GH_TOKEN: ${{ github.token }}
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
PR_USER: ${{ github.event.pull_request.user.login }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
set -euo pipefail
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git remote set-url origin "https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
git checkout "${DEFAULT_BRANCH}"
git pull --ff-only origin "${DEFAULT_BRANCH}"
if git --no-pager log --format=%s --grep="^chore: update leaderboard for PR #${PR_NUMBER}$" -n 1 | grep -q .; then
echo "PR #${PR_NUMBER} already counted."
exit 0
fi
if [ ! -f leaderboard.json ]; then
printf '{}\n' > leaderboard.json
fi
tmp_file="$(mktemp)"
jq --arg user "${PR_USER}" '.[$user] = ((.[$user] // 0) + 1)' leaderboard.json > "${tmp_file}"
mv "${tmp_file}" leaderboard.json
git add leaderboard.json
if git diff --cached --quiet -- leaderboard.json; then
echo "No leaderboard changes to commit."
exit 0
fi
git commit -m "chore: update leaderboard for PR #${PR_NUMBER}"
git pull --rebase origin "${DEFAULT_BRANCH}"
git push origin "HEAD:${DEFAULT_BRANCH}"