Skip to content

Commit e998df7

Browse files
committed
tools: add workflow to add label when there are 2+ approvals
Signed-off-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: nodejs/node#60554
1 parent 8410a91 commit e998df7

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

.github/workflows/label-pr.yml

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,46 @@ 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+
steps:
27+
- name: Download PR number
28+
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
29+
with:
30+
name: pr-number
31+
run-id: ${{ github.event.workflow_run.id }}
32+
github-token: ${{ secrets.GH_USER_TOKEN }}
33+
- name: 'Add/remove "Approvals: 2+" label'
34+
run: |
35+
set -x
36+
PR_DATA=$(gh --repo "$GITHUB_REPOSITORY" pr view "$(< pr_number.txt)" --json reviewDecision,reviews,labels --jq 'if .reviewDecision != "APPROVED" then .reviewDecision | halt_error end' || true)
37+
38+
[ -n "$PR_DATA" ] && {
39+
TEAM_NAME=$(echo "$PR_DATA" | jq -r 'if (.labels | any(.name == "semver-major")) then "tsc" else "collaborators" end')
40+
TEAM_MEMBERS=$(gh api "orgs/nodejs/teams/$TEAM_NAME/members" --paginate --jq 'map(.login)')
41+
42+
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' \
43+
&& gh --repo "$GITHUB_REPOSITORY" pr edit "$(< pr_number.txt)" --add-label 'Approvals: 2+'
44+
} \
45+
|| gh --repo "$GITHUB_REPOSITORY" pr edit "$(< pr_number.txt)" --remove-label 'Approvals: 2+'
46+
env:
47+
GH_TOKEN: ${{ secrets.GH_USER_TOKEN }}
48+
PR_URL: ${{ github.event.pull_request.html_url }}

.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)