Repository navigation
[NoQA] Prevent GH actions from running on auto-closed PRs #84884
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
Changes from all commits
c1aab90
62b1ea5
8c0db25
4f03d36
d8ec215
e019584
8c4ecfb
c9f1388
87eb918
6bc591a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,8 +13,19 @@ concurrency: | |
| cancel-in-progress: true | ||
|
|
||
| jobs: | ||
| validate: | ||
| uses: ./.github/workflows/contributorValidationGate.yml | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Calling Useful? React with 👍 / 👎. |
||
| with: | ||
| PR_NUMBER: ${{ github.event.pull_request.number }} | ||
| PR_AUTHOR: ${{ github.event.pull_request.user.login }} | ||
| AUTHOR_ASSOCIATION: ${{ github.event.pull_request.author_association }} | ||
|
|
||
| review: | ||
| if: github.event.pull_request.draft != true && !contains(github.event.pull_request.title, 'Revert') | ||
| needs: [validate] | ||
| if: | | ||
| needs.validate.outputs.IS_AUTHORIZED == 'true' | ||
| && github.event.pull_request.draft != true | ||
| && !contains(github.event.pull_request.title, 'Revert') | ||
| runs-on: blacksmith-2vcpu-ubuntu-2404 | ||
| env: | ||
| PR_NUMBER: ${{ github.event.pull_request.number }} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| # Reusable workflow that checks whether a PR author is authorized to contribute. | ||
| # Used by expensive PR workflows to skip CI for unauthorized contributors. | ||
| # The actual close/lock/comment logic lives in validateContributorPR.yml. | ||
|
|
||
| name: Contributor Validation Gate | ||
|
|
||
| on: | ||
| workflow_call: | ||
| inputs: | ||
| PR_NUMBER: | ||
| description: Pull request number | ||
| required: true | ||
| type: number | ||
| PR_AUTHOR: | ||
| description: Pull request author login | ||
| required: true | ||
| type: string | ||
| AUTHOR_ASSOCIATION: | ||
| description: "Author's association with the repository (MEMBER, OWNER, CONTRIBUTOR, etc.)" | ||
| required: true | ||
| type: string | ||
| outputs: | ||
| IS_AUTHORIZED: | ||
| description: "'true' if the contributor is authorized, 'false' otherwise" | ||
| value: ${{ jobs.check.outputs.IS_AUTHORIZED }} | ||
|
|
||
| jobs: | ||
| check: | ||
| runs-on: blacksmith-2vcpu-ubuntu-2404 | ||
| outputs: | ||
| IS_AUTHORIZED: ${{ steps.gate.outputs.IS_AUTHORIZED }} | ||
| steps: | ||
| - name: Check contributor authorization | ||
| id: gate | ||
| uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd | ||
| with: | ||
| script: | | ||
| const prNumber = ${{ inputs.PR_NUMBER }}; | ||
| const prAuthor = '${{ inputs.PR_AUTHOR }}'; | ||
| const authorAssociation = '${{ inputs.AUTHOR_ASSOCIATION }}'; | ||
|
|
||
| if (['MEMBER', 'OWNER', 'CONTRIBUTOR'].includes(authorAssociation)) { | ||
| console.log(`${prAuthor} is ${authorAssociation}. Authorized.`); | ||
| core.setOutput('IS_AUTHORIZED', 'true'); | ||
| return; | ||
| } | ||
|
|
||
| console.log(`${prAuthor} has association "${authorAssociation}". Checking linked issues/PRs...`); | ||
|
|
||
| const {data: pr} = await github.rest.pulls.get({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| pull_number: prNumber, | ||
| }); | ||
|
|
||
| const prBody = pr.body || ''; | ||
| const cleanBody = prBody.replace(/<!--[\s\S]*?-->/g, ''); | ||
|
|
||
| const issuePattern = /https:\/\/github\.com\/(Expensify\/[^/]+)\/issues\/(\d+)/g; | ||
| let match; | ||
| while ((match = issuePattern.exec(cleanBody)) !== null) { | ||
| const [, repo, num] = match; | ||
| const issueNumber = parseInt(num); | ||
| try { | ||
| const [owner, repoName] = repo.split('/'); | ||
| const {data: issue} = await github.rest.issues.get({ | ||
| owner, | ||
| repo: repoName, | ||
| issue_number: issueNumber, | ||
| }); | ||
| if (issue.assignees.some(a => a.login.toLowerCase() === prAuthor.toLowerCase())) { | ||
| console.log(`${prAuthor} is assigned to ${repo}#${issueNumber}. Authorized.`); | ||
| core.setOutput('IS_AUTHORIZED', 'true'); | ||
| return; | ||
| } | ||
| console.log(`${prAuthor} is NOT assigned to ${repo}#${issueNumber}.`); | ||
| } catch (e) { | ||
| console.log(`Could not verify ${repo}#${issueNumber}: ${e.message}`); | ||
| } | ||
| } | ||
|
|
||
| const prUrlPattern = /https:\/\/github\.com\/(Expensify\/[^/]+)\/pull\/(\d+)/g; | ||
| while ((match = prUrlPattern.exec(cleanBody)) !== null) { | ||
| const [, repo, num] = match; | ||
| const linkedPRNumber = parseInt(num); | ||
| try { | ||
| const [owner, repoName] = repo.split('/'); | ||
|
|
||
| const {data: linkedPR} = await github.rest.pulls.get({ | ||
| owner, | ||
| repo: repoName, | ||
| pull_number: linkedPRNumber, | ||
| }); | ||
| if (linkedPR.user.login.toLowerCase() === prAuthor.toLowerCase()) { | ||
| console.log(`${prAuthor} is the author of ${repo}#${linkedPRNumber}. Authorized.`); | ||
| core.setOutput('IS_AUTHORIZED', 'true'); | ||
| return; | ||
| } | ||
|
|
||
| const {data: reviews} = await github.rest.pulls.listReviews({ | ||
| owner, | ||
| repo: repoName, | ||
| pull_number: linkedPRNumber, | ||
| }); | ||
| if (reviews.some(r => r.user?.login?.toLowerCase() === prAuthor.toLowerCase())) { | ||
| console.log(`${prAuthor} is a reviewer of ${repo}#${linkedPRNumber}. Authorized.`); | ||
| core.setOutput('IS_AUTHORIZED', 'true'); | ||
| return; | ||
| } | ||
|
|
||
| const {data: requestedReviewers} = await github.rest.pulls.listRequestedReviewers({ | ||
| owner, | ||
| repo: repoName, | ||
| pull_number: linkedPRNumber, | ||
| }); | ||
| if (requestedReviewers.users.some(u => u.login.toLowerCase() === prAuthor.toLowerCase())) { | ||
| console.log(`${prAuthor} is a requested reviewer of ${repo}#${linkedPRNumber}. Authorized.`); | ||
| core.setOutput('IS_AUTHORIZED', 'true'); | ||
| return; | ||
| } | ||
|
|
||
| console.log(`${prAuthor} is not author or reviewer of ${repo}#${linkedPRNumber}.`); | ||
| } catch (e) { | ||
| console.log(`Could not verify ${repo}#${linkedPRNumber}: ${e.message}`); | ||
| } | ||
| } | ||
|
|
||
| console.log(`No valid authorization found for ${prAuthor}.`); | ||
| core.setOutput('IS_AUTHORIZED', 'false'); |
Uh oh!
There was an error while loading. Please reload this page.