Repository navigation
chore(ci): require approved issues or vouchers for PRs #1917
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 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
796d66f
chore(ci): require approved issues or vouchers for PRs
tinsever 77dd9b6
docs(ci): clarify eligibility checks during GitHub outages
tinsever 37ce548
fix(ci): recheck contribution eligibility when PRs close
tinsever 21f2123
fix(ci): batch linked issue eligibility lookups
tinsever 0f9c23a
fix(ci): exclude skipped events from eligibility concurrency
tinsever 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
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 |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| { | ||
| "vouchedContributors": [ | ||
| { | ||
| "id": 44305048, | ||
| "login": "andrejsshell", | ||
| "reason": "Core team (usekaneo/developers)." | ||
| }, | ||
| { | ||
| "id": 127273550, | ||
| "login": "randoneering", | ||
| "reason": "Core team (usekaneo/developers)." | ||
| }, | ||
| { | ||
| "id": 176929823, | ||
| "login": "tinsever", | ||
| "reason": "Core team (usekaneo/developers)." | ||
| } | ||
| ], | ||
| "exemptBots": [ | ||
| { | ||
| "id": 49699333, | ||
| "login": "dependabot[bot]", | ||
| "reason": "Repository-managed dependency updates." | ||
| } | ||
| ] | ||
| } |
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
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 |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| name: Contribution eligibility | ||
|
|
||
| on: | ||
| # zizmor: ignore[dangerous-triggers] Only executes policy code from main; | ||
| # never checks out or executes pull-request content. | ||
| pull_request_target: | ||
| types: [opened, edited, synchronize, reopened, ready_for_review, closed] | ||
| issues: | ||
| types: [labeled, unlabeled, closed, reopened, deleted, transferred] | ||
| label: | ||
| types: [edited, deleted] | ||
| # zizmor: ignore[dangerous-triggers] Reads GitHub metadata and trusted main | ||
| # code only; does not consume artifacts or code from the triggering run. | ||
| workflow_run: | ||
| workflows: [CI] | ||
| types: [completed] | ||
| push: | ||
| branches: [main] | ||
| paths: | ||
| - .github/contribution-policy.json | ||
| - .github/workflows/contribution-eligibility.yml | ||
| - scripts/contribution-eligibility/** | ||
| # Manual sidebar links and permission changes have no matching Actions event. | ||
| schedule: | ||
| - cron: '17 * * * *' | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: read | ||
| pull-requests: read | ||
| issues: read | ||
| checks: write | ||
|
|
||
| concurrency: | ||
| group: contribution-eligibility | ||
| cancel-in-progress: false | ||
|
|
||
| jobs: | ||
| reconcile: | ||
| # Dependabot-triggered runs may receive a read-only token. CI completion | ||
| # rechecks its PR using a token issued in the trusted workflow context. | ||
| if: >- | ||
| (github.event_name != 'pull_request_target' || github.actor != 'dependabot[bot]') && | ||
| (github.event_name != 'workflow_run' || github.event.workflow_run.actor.login == 'dependabot[bot]') | ||
| runs-on: blacksmith-4vcpu-ubuntu-2404 | ||
| timeout-minutes: 15 | ||
| steps: | ||
| - name: Check out current trusted policy | ||
| uses: useblacksmith/checkout@25227e61ff9dafe400e22fa487b673eac4e4409a # v1 | ||
| with: | ||
| # Always read the latest policy, including when an older run is queued. | ||
| ref: main | ||
| persist-credentials: false | ||
|
|
||
| - name: Set up Node.js | ||
| uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7 | ||
| with: | ||
| node-version: 24.19.0 | ||
| package-manager-cache: false | ||
|
|
||
| - name: Check open pull requests | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| run: node scripts/contribution-eligibility/check.mjs | ||
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
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
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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { statusContext } from "./policy.mjs"; | ||
|
|
||
| export function checkPublisher(github) { | ||
| const ids = new Map(); | ||
| return async (sha, result) => { | ||
| const body = { | ||
| name: statusContext, | ||
| status: result.state === "pending" ? "in_progress" : "completed", | ||
| ...(result.state === "pending" | ||
| ? {} | ||
| : { conclusion: result.state === "success" ? "success" : "failure" }), | ||
| details_url: `https://github.com/${github.repository}/blob/main/CONTRIBUTING.md#contribution-eligibility`, | ||
| output: { | ||
| title: result.description, | ||
| summary: `${result.description}\n\n[Contribution rules](https://github.com/${github.repository}/blob/main/CONTRIBUTING.md#contribution-eligibility).`, | ||
| }, | ||
| }; | ||
| const id = ids.get(sha); | ||
| const check = await github.request( | ||
| `repos/${github.repository}/check-runs${id ? `/${id}` : ""}`, | ||
| { | ||
| method: id ? "PATCH" : "POST", | ||
| body: id ? body : { ...body, head_sha: sha }, | ||
| }, | ||
| ); | ||
| if (!Number.isSafeInteger(check.id) || check.id <= 0) { | ||
| throw new Error("GitHub did not return a check run ID."); | ||
| } | ||
| ids.set(sha, check.id); | ||
| }; | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| import { readFile } from "node:fs/promises"; | ||
| import { pathToFileURL } from "node:url"; | ||
| import { GitHub } from "./github.mjs"; | ||
| import { checkPublisher } from "./check-runs.mjs"; | ||
| import { assessIssues, authorExemption, validatePolicy } from "./policy.mjs"; | ||
|
|
||
| async function checkPullRequest(github, pull, policy, policyError) { | ||
| if (policyError) throw policyError; | ||
| let exemption = authorExemption(pull.user, null, policy); | ||
| if (!exemption && pull.user.type !== "Bot") { | ||
| const permission = await github.request( | ||
| `repos/${github.repository}/collaborators/${encodeURIComponent(pull.user.login)}/permission`, | ||
| { allowNotFound: true }, | ||
| ); | ||
| exemption = authorExemption(pull.user, permission, policy); | ||
| } | ||
| let result; | ||
| if (exemption) { | ||
| result = { state: "success", description: exemption }; | ||
| } else { | ||
| result = assessIssues(await github.linkedIssues(pull.number)); | ||
| } | ||
| const current = await github.request( | ||
| `repos/${github.repository}/pulls/${pull.number}`, | ||
| ); | ||
| if (current.state !== "open") return null; | ||
| if ( | ||
| current.head.sha !== pull.head.sha || | ||
| current.body !== pull.body || | ||
| current.base.ref !== pull.base.ref | ||
| ) { | ||
| return { | ||
| state: "pending", | ||
| description: "Pull request changed; waiting for a fresh check.", | ||
| }; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| export async function reconcile(github, loadPolicy) { | ||
| let policy; | ||
| let policyError; | ||
| try { | ||
| policy = validatePolicy(await loadPolicy()); | ||
| } catch (error) { | ||
| policyError = error; | ||
| } | ||
| const pulls = await github.openPullRequests(); | ||
| const heads = new Map(); | ||
| const publish = checkPublisher(github); | ||
| for (const pull of pulls) { | ||
| if (!/^[a-f0-9]{40}$/.test(pull.head?.sha ?? "")) { | ||
| throw new Error("Missing pull request head commit."); | ||
| } | ||
| if (!heads.has(pull.head.sha)) { | ||
| // Clear earlier successes before reading mutable approval and trust data. | ||
| await publish(pull.head.sha, { | ||
| state: "pending", | ||
| description: "Checking contribution eligibility.", | ||
| }); | ||
| heads.set(pull.head.sha, []); | ||
| } | ||
| } | ||
| const results = []; | ||
| const errors = []; | ||
| // Every event refreshes all open PRs: GitHub concurrency may replace a queued | ||
| // run, so an event scoped to one PR could otherwise leave another check stale. | ||
| for (const pull of pulls) { | ||
| let result; | ||
| try { | ||
| result = await checkPullRequest(github, pull, policy, policyError); | ||
| } catch (error) { | ||
| errors.push(error); | ||
| result = { | ||
| state: "error", | ||
| description: | ||
| "Eligibility could not be checked. A maintainer must rerun the workflow.", | ||
| }; | ||
| } | ||
| if (result) { | ||
| const entry = { number: pull.number, ...result }; | ||
| results.push(entry); | ||
| heads.get(pull.head.sha).push(entry); | ||
| } | ||
| } | ||
| // Checks belong to commits, not PRs. A vouched author's PR must never make | ||
| // a different, ineligible PR sharing the same commit eligible to merge. | ||
| const priority = ["error", "failure", "pending", "success"]; | ||
| for (const [sha, entries] of heads) { | ||
| const worst = entries.toSorted( | ||
| (a, b) => priority.indexOf(a.state) - priority.indexOf(b.state), | ||
| )[0]; | ||
| if (worst) { | ||
| await publish(sha, { | ||
| state: worst.state, | ||
| description: `PR #${worst.number}: ${worst.description}`, | ||
| }); | ||
| } | ||
| } | ||
| if (policyError) errors.push(policyError); | ||
| if (errors.length) | ||
| throw new AggregateError(errors, "Contribution eligibility checks failed."); | ||
| return results; | ||
| } | ||
|
|
||
| if ( | ||
| process.argv[1] && | ||
| import.meta.url === pathToFileURL(process.argv[1]).href | ||
| ) { | ||
| const github = new GitHub(process.env); | ||
| const results = await reconcile(github, async () => | ||
| JSON.parse( | ||
| await readFile( | ||
| new URL("../../.github/contribution-policy.json", import.meta.url), | ||
| "utf8", | ||
| ), | ||
| ), | ||
| ); | ||
| for (const result of results) { | ||
| console.log(`PR #${result.number}: ${result.state}. ${result.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.