Label PRs #9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Label PRs | |
| on: | |
| pull_request_target: | |
| types: [opened] | |
| workflow_run: | |
| workflows: [PR Reviewed] | |
| types: [completed] | |
| permissions: {} | |
| jobs: | |
| label: | |
| runs-on: ubuntu-slim | |
| if: github.event_name != 'workflow_run' | |
| steps: | |
| - uses: nodejs/node-pr-labeler@d4cf1b8b9f23189c37917000e5e17e796c770a6b # v1 | |
| with: | |
| repo-token: ${{ secrets.GH_USER_TOKEN }} | |
| configuration-path: .github/label-pr-config.yml | |
| count_approvals: | |
| runs-on: ubuntu-latest | |
| if: github.event_name == 'workflow_run' | |
| steps: | |
| - name: Download PR number | |
| uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 | |
| with: | |
| name: pr-number | |
| run-id: ${{ github.event.workflow_run.id }} | |
| github-token: ${{ secrets.GH_USER_TOKEN }} | |
| - name: 'Add/remove "Approvals: 2+" label' | |
| run: | | |
| set -x | |
| 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) | |
| [ -n "$PR_DATA" ] && { | |
| TEAM_NAME=$(echo "$PR_DATA" | jq -r 'if (.labels | any(.name == "semver-major")) then "tsc" else "collaborators" end') | |
| TEAM_MEMBERS=$(gh api "orgs/nodejs/teams/$TEAM_NAME/members" --paginate --jq 'map(.login)') | |
| 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' \ | |
| && gh --repo "$GITHUB_REPOSITORY" pr edit "$(< pr_number.txt)" --add-label 'Approvals: 2+' | |
| } \ | |
| || gh --repo "$GITHUB_REPOSITORY" pr edit "$(< pr_number.txt)" --remove-label 'Approvals: 2+' | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| PR_URL: ${{ github.event.pull_request.html_url }} |