@@ -3,16 +3,61 @@ name: Label PRs
33on :
44 pull_request_target :
55 types : [opened]
6+ workflow_run :
7+ workflows : [PR Reviewed]
8+ types : [completed]
69
7- permissions :
8- contents : read
10+ permissions : {}
911
1012jobs :
1113 label :
1214 runs-on : ubuntu-slim
15+ if : github.event_name != 'workflow_run'
1316
1417 steps :
1518 - uses : nodejs/node-pr-labeler@d4cf1b8b9f23189c37917000e5e17e796c770a6b # v1
1619 with :
1720 repo-token : ${{ secrets.GH_USER_TOKEN }}
1821 configuration-path : .github/label-pr-config.yml
22+
23+ count_approvals :
24+ runs-on : ubuntu-latest
25+ if : github.event_name == 'workflow_run'
26+ permissions :
27+ pull-requests : read
28+ steps :
29+ - name : Download PR number
30+ uses : actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
31+ with :
32+ name : pr-number
33+ run-id : ${{ github.event.workflow_run.id }}
34+ github-token : ${{ secrets.GH_USER_TOKEN }}
35+ - name : Fetch PR data
36+ id : pr-data
37+ run : |
38+ PR_NUMBER="$(grep '^[0-9]+$' pr_number.txt)"
39+ echo "NUMBER=$PR_NUMBER" >> "$GITHUB_OUTPUT"
40+
41+ [ -n "$PR_NUMBER" ] && echo "DATA=$(
42+ gh --repo "$GITHUB_REPOSITORY" pr view "$PR_NUMBER" \
43+ --json reviewDecision,reviews,labels \
44+ --jq 'if .reviewDecision != "APPROVED" then .reviewDecision | halt_error end' \
45+ || true
46+ )" >> "$GITHUB_OUTPUT"
47+ env :
48+ GH_TOKEN : ${{ github.token }}
49+ - name : ' Add/remove "Approvals: 2+" label'
50+ run : |
51+ set -x
52+ [ -n "$PR_DATA" ] && {
53+ TEAM_NAME=$(echo "$PR_DATA" | jq -r 'if (.labels | any(.name == "semver-major")) then "tsc" else "collaborators" end')
54+ TEAM_MEMBERS=$(gh api "orgs/nodejs/teams/$TEAM_NAME/members" --paginate --jq 'map(.login)')
55+
56+ echo "$PR_DATA" | jq --argjson team "$TEAM_MEMBERS" -r '.reviews | map(select(.state == "APPROVED" and (.author.login as $login | $team | any(. == $login)))) | if length < 2 then halt_error end' \
57+ && gh --repo "$GITHUB_REPOSITORY" pr edit "$PR_NUMBER" --add-label 'Approvals: 2+'
58+ } \
59+ || gh --repo "$GITHUB_REPOSITORY" pr edit "$PR_NUMBER" --remove-label 'Approvals: 2+'
60+ env :
61+ GH_TOKEN : ${{ secrets.GH_USER_TOKEN }}
62+ PR_NUMBER : ${{ steps.pr-data.outputs.NUMBER }}
63+ PR_DATA : ${{ steps.pr-data.outputs.DATA }}
0 commit comments