-
Notifications
You must be signed in to change notification settings - Fork 884
CI - Simplify PR approval check #4578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e9089c6
[bfops/tweak-pr-approval-check]: Simplify PR approval check
bfops 5539b40
[bfops/tweak-pr-approval-check]: fix merge queue
bfops 4311fef
[bfops/tweak-pr-approval-check]: simplify
bfops d467fda
[bfops/tweak-pr-approval-check]: review
bfops 03b40c0
[bfops/tweak-pr-approval-check]: review
bfops File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,45 +1,106 @@ | ||
| name: PR Approval Check | ||
| name: Review Checks | ||
|
|
||
| on: | ||
| pull_request: | ||
| types: [opened, synchronize, reopened, ready_for_review] | ||
| types: [opened, synchronize, reopened] | ||
| pull_request_review: | ||
| types: [submitted] | ||
| types: [submitted, dismissed] | ||
| merge_group: | ||
|
|
||
| permissions: | ||
| contents: read | ||
| pull-requests: read | ||
| statuses: write | ||
|
|
||
| concurrency: | ||
| group: pr-approval-check-${{ github.event.pull_request.number || github.sha }} | ||
| cancel-in-progress: true | ||
|
|
||
| jobs: | ||
| check-approvals: | ||
| publish-approval-status: | ||
| name: Set approval status | ||
| runs-on: ubuntu-latest | ||
| # Skip this check if PR is in draft state | ||
| if: github.event.pull_request.draft == false | ||
bfops marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v3 | ||
| - name: Evaluate and publish approval status | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Check PR approvals | ||
| env: | ||
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| run: | | ||
| # Check if PR author is clockwork-labs-bot | ||
| if [[ "${{ github.event.pull_request.user.login }}" != "clockwork-labs-bot" ]]; then | ||
| echo "PR opened by ${{ github.event.pull_request.user.login }}, not clockwork-labs-bot. Skipping check." | ||
| exit 0 | ||
| fi | ||
|
|
||
| PR_NUMBER="${{ github.event.pull_request.number }}" | ||
| # Get approval count | ||
| APPROVALS=$(gh pr view $PR_NUMBER --json reviews -q '.reviews | map(select(.state == "APPROVED")) | length') | ||
|
|
||
| echo "PR has $APPROVALS approvals" | ||
|
|
||
| if [[ $APPROVALS -lt 2 ]]; then | ||
| echo "Error: PRs from clockwork-labs-bot require at least 2 approvals" | ||
| exit 1 | ||
| else | ||
| echo "PR has the required number of approvals" | ||
| fi | ||
| github-token: ${{ secrets.GITHUB_TOKEN }} | ||
| script: | | ||
| const contextName = "PR approval check"; | ||
|
|
||
| let targetSha; | ||
| let state; | ||
| let description; | ||
|
|
||
| if (context.eventName === "merge_group") { | ||
| targetSha = process.env.GITHUB_SHA; | ||
| state = "success"; | ||
| description = "Merge group entry; approvals already satisfied"; | ||
| } else { | ||
| const pr = context.payload.pull_request; | ||
| targetSha = pr.head.sha; | ||
|
|
||
| if (pr.user.login !== "clockwork-labs-bot") { | ||
| state = "success"; | ||
| description = "PR author is not clockwork-labs-bot"; | ||
| } else { | ||
| const result = await github.graphql( | ||
| ` | ||
| query($owner: String!, $repo: String!, $number: Int!) { | ||
| repository(owner: $owner, name: $repo) { | ||
| pullRequest(number: $number) { | ||
| latestOpinionatedReviews(first: 100, writersOnly: true) { | ||
| nodes { | ||
| state | ||
| author { | ||
| login | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| `, | ||
| { | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| number: pr.number, | ||
| } | ||
| ); | ||
|
|
||
| const effectiveApprovers = | ||
| result.repository.pullRequest.latestOpinionatedReviews.nodes | ||
| .filter((review) => review.state === "APPROVED") | ||
| .map((review) => review.author?.login) | ||
| .filter(Boolean); | ||
|
|
||
| core.info( | ||
| `Latest effective approvers (${effectiveApprovers.length}): ${effectiveApprovers.join(", ")}` | ||
| ); | ||
|
|
||
| if (effectiveApprovers.length < 2) { | ||
| state = "failure"; | ||
| description = "PRs from clockwork-labs-bot require at least 2 approvals"; | ||
| } else { | ||
| state = "success"; | ||
| description = "PR has the required number of approvals"; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| core.info(`Publishing status ${state} for ${targetSha}: ${description}`); | ||
|
|
||
| // We need to set a separate commit status for this, because it runs on both | ||
| // pull_request and pull_request_review events. If we don't set an explicit context, | ||
| // what happens is that there are sometimes two separate statuses on the same commit - | ||
| // one from each event type. This leads to weird cases where one copy of the check is failed, | ||
| // and the other is successful, and the failed one blocks the PR from merging. | ||
| await github.rest.repos.createCommitStatus({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| sha: targetSha, | ||
| state, | ||
| context: contextName, | ||
| description, | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.