Skip to content

Commit 2fede59

Browse files
committed
tools: add workflow to add label when there are 2+ approvals
And make the CQ use it to support adding the label on PRs with a single approval which are not 7 days old yet. Signed-off-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 8410a91 commit 2fede59

3 files changed

Lines changed: 93 additions & 3 deletions

File tree

‎.github/workflows/commit-queue.yml‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,21 @@ jobs:
3333
- name: Get Pull Requests
3434
id: get_mergeable_prs
3535
run: |
36+
set -x
3637
prs=$(gh pr list \
3738
--repo "$GITHUB_REPOSITORY" \
3839
--base "$GITHUB_REF_NAME" \
3940
--label 'commit-queue' \
4041
--json 'number' \
42+
--search "created:<=$(date --date="7 days ago" +"%Y-%m-%dT%H:%M:%S%z") -label:blocked -label:\"Approvals: 2+\"" \
43+
-t '{{ range . }}{{ .number }} {{ end }}' \
44+
--limit 100)
45+
prs_2_approvals=$(gh pr list \
46+
--repo "$GITHUB_REPOSITORY" \
47+
--base "$GITHUB_REF_NAME" \
48+
--label 'commit-queue' \
49+
--label 'Approvals: 2+' \
50+
--json 'number' \
4151
--search "created:<=$(date --date="2 days ago" +"%Y-%m-%dT%H:%M:%S%z") -label:blocked" \
4252
-t '{{ range . }}{{ .number }} {{ end }}' \
4353
--limit 100)
@@ -46,11 +56,12 @@ jobs:
4656
--base "$GITHUB_REF_NAME" \
4757
--label 'commit-queue' \
4858
--label 'fast-track' \
59+
--label 'Approvals: 2+' \
4960
--search "-label:blocked" \
5061
--json 'number' \
5162
-t '{{ range . }}{{ .number }} {{ end }}' \
5263
--limit 100)
53-
numbers=$(echo $prs' '$fast_track_prs | jq -r -s 'unique | join(" ")')
64+
numbers=$(echo "$prs $prs_2_approvals $fast_track_prs" | jq -r -s 'unique | join(" ")')
5465
echo "numbers=$numbers" >> "$GITHUB_OUTPUT"
5566
env:
5667
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

‎.github/workflows/label-pr.yml‎

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,76 @@ name: Label PRs
33
on:
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

1012
jobs:
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+
# Using privileged token to be able to download file from a different workflow
35+
github-token: ${{ secrets.GH_USER_TOKEN }}
36+
- name: Fetch PR data
37+
id: pr-data
38+
run: |
39+
set -x
40+
PR_NUMBER="$(grep -E '^[0-9]+$' pr_number.txt)"
41+
42+
[ -n "$PR_NUMBER" ] && {
43+
echo "NUMBER=$PR_NUMBER"
44+
echo "DATA=$(
45+
gh --repo "$GITHUB_REPOSITORY" pr view "$PR_NUMBER" \
46+
--json reviewDecision,headRefOid,reviews,labels \
47+
--jq 'if .reviewDecision != "APPROVED" then .reviewDecision | halt_error end' \
48+
|| true
49+
)"
50+
} >> "$GITHUB_OUTPUT"
51+
env:
52+
# Using unprivileged token as PR_NUMBER is a user-provided value
53+
GH_TOKEN: ${{ github.token }}
54+
- name: 'Add/remove "Approvals: 2+" label'
55+
run: |
56+
set -x
57+
[ -n "$PR_DATA" ] && {
58+
TEAM_NAME=$(echo "$PR_DATA" | jq -r 'if (.labels | any(.name == "semver-major")) then "tsc" else "collaborators" end')
59+
TEAM_MEMBERS=$(gh api "orgs/nodejs/teams/$TEAM_NAME/members" --paginate --jq 'map(.login)')
60+
61+
echo "$PR_DATA" | jq --argjson team "$TEAM_MEMBERS" -r '
62+
.headRefOid as $head
63+
| .reviews
64+
| map(select(
65+
.state == "APPROVED"
66+
and .commit.oid == $head
67+
and (.author.login as $login | $team | any(. == $login))
68+
))
69+
| unique_by(.author.login)
70+
| if length < 2 then halt_error end' \
71+
&& gh --repo "$GITHUB_REPOSITORY" pr edit "$PR_NUMBER" --add-label 'Approvals: 2+'
72+
} \
73+
|| gh --repo "$GITHUB_REPOSITORY" pr edit "$PR_NUMBER" --remove-label 'Approvals: 2+'
74+
env:
75+
# Using privileged token to be able to fetch the team list
76+
GH_TOKEN: ${{ secrets.GH_USER_TOKEN }}
77+
PR_NUMBER: ${{ steps.pr-data.outputs.NUMBER }}
78+
PR_DATA: ${{ steps.pr-data.outputs.DATA }}

‎.github/workflows/pr-revieweed.yml‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: PR Reviewed
2+
3+
on:
4+
pull_request_review:
5+
types: [submitted, dismissed]
6+
7+
permissions: {}
8+
9+
jobs:
10+
store-pr-number:
11+
runs-on: ubuntu-slim
12+
steps:
13+
- name: Store PR number
14+
run: echo "${{ github.event.pull_request.number }}" > pr_number.txt
15+
- name: Upload PR number
16+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
17+
with:
18+
name: pr-number
19+
path: pr_number.txt

0 commit comments

Comments
 (0)