-
Notifications
You must be signed in to change notification settings - Fork 360
66 lines (52 loc) · 2.02 KB
/
update-pr-leaderboard.yml
File metadata and controls
66 lines (52 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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}"