Skip to content

Commit d0b6beb

Browse files
committed
fix(ci): keep read-only fork review runs out of PR concurrency groups
Fork pull_request_review runs receive a read-only token and write nothing (the reconcile script's isReadOnlyRun guard skips every mutation), but they still took PR-keyed concurrency groups at both the workflow and job level. Under newest-pending-wins, a newer read-only run could supersede a queued mutating run for the same PR — the #1707 starvation pattern in a narrower form. Scope a fork-review guard (head repo != base repo) to pull_request_review at both levels: read-only runs take a unique per-run group (job-level uses a non-numeric -ro- infix so it can never collide with a mutating reconcile-<pr> group), while mutating fork pull_request_target runs keep PR grouping. Missing head.repo metadata fails toward the run-scoped, read-only-safe key. Fixes #1707 Co-authored-by: Roomote <[email protected]>
1 parent 425f654 commit d0b6beb

2 files changed

Lines changed: 96 additions & 8 deletions

File tree

‎.github/workflows/label-pr-review-state.yml‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ permissions:
4444
# workflow_run events key by the triggering run's id instead of a PR number: a
4545
# single run can reconcile several associated PRs (and fork runs backfill
4646
# none), so a PR-number key would let an unrelated event for the first PR
47-
# supersede reconciliation of the rest. Sweep triggers (hourly schedule,
48-
# pushes to main) share one group, and anything unresolved falls back to a
49-
# unique per-run group.
47+
# supersede reconciliation of the rest. Fork pull_request_review runs are
48+
# read-only (see isReadOnlyRun in the reconcile script) and write nothing, so
49+
# they take a unique per-run group rather than a PR-keyed group whose
50+
# newest-pending-wins could supersede a queued mutating run for that PR.
51+
# Sweep triggers (hourly schedule, pushes to main) share one group, and
52+
# anything unresolved falls back to a unique per-run group.
5053
#
5154
# Job level (on the reconcile job): every mutating shard keys per PR in one
5255
# shared group namespace, so direct-event shards, sweep-resolved shards, and
@@ -57,6 +60,8 @@ permissions:
5760
concurrency:
5861
group: >-
5962
label-pr-review-state-${{
63+
((github.event_name == 'pull_request_review' &&
64+
github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) && github.run_id) ||
6065
github.event.pull_request.number ||
6166
github.event.issue.number ||
6267
github.event.inputs.pull_request_number ||
@@ -164,9 +169,21 @@ jobs:
164169
# workflow and job levels, so a shard group equal to the workflow-level
165170
# group its own run holds (e.g. label-pr-review-state-<pr> for direct
166171
# triggers, or a workflow_run.id that numerically matches a PR number)
167-
# fails the job at startup.
172+
# fails the job at startup. Read-only fork pull_request_review shards
173+
# write nothing, so they take a run-scoped `-ro-<run_id>` group instead of
174+
# the PR-keyed group: the non-numeric `-ro-` infix keeps them from ever
175+
# colliding with (or superseding) a mutating shard for the same PR.
168176
concurrency:
169-
group: label-pr-review-state-reconcile-${{ matrix.pr_number || github.run_id }}
177+
group: >-
178+
label-pr-review-state-reconcile-${{
179+
((github.event_name == 'pull_request_review' &&
180+
github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) && 'ro-') || ''
181+
}}${{
182+
((github.event_name == 'pull_request_review' &&
183+
github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) && github.run_id) ||
184+
matrix.pr_number ||
185+
github.run_id
186+
}}
170187
cancel-in-progress: false
171188
runs-on: ubuntu-latest
172189
env:

‎src/services/__tests__/pr-review-state-workflow.test.ts‎

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2404,7 +2404,11 @@ describe("PR review-state workflow concurrency groups (#1707)", () => {
24042404
event_name: string
24052405
run_id: number
24062406
event: {
2407-
pull_request?: { number: number }
2407+
pull_request?: {
2408+
number: number
2409+
head?: { repo?: { full_name?: string } | null }
2410+
base?: { repo?: { full_name?: string } | null }
2411+
}
24082412
issue?: { number: number }
24092413
workflow_run?: { id?: number; pull_requests?: Array<{ number: number }> }
24102414
inputs?: { pull_request_number: number }
@@ -2420,7 +2424,7 @@ describe("PR review-state workflow concurrency groups (#1707)", () => {
24202424
matrix?: MatrixContext
24212425
}
24222426

2423-
function splitTopLevel(expression: string, operator: "||" | "&&" | "=="): string[] {
2427+
function splitTopLevel(expression: string, operator: "||" | "&&" | "==" | "!="): string[] {
24242428
const parts: string[] = []
24252429
let depth = 0
24262430
let inString = false
@@ -2510,6 +2514,10 @@ describe("PR review-state workflow concurrency groups (#1707)", () => {
25102514
}
25112515

25122516
function evaluateComparison(expression: string, roots: ExpressionRoots): unknown {
2517+
const notEquals = splitTopLevel(expression, "!=")
2518+
if (notEquals.length > 1) {
2519+
return !looseEquals(evaluatePrimary(notEquals[0], roots), evaluatePrimary(notEquals[1], roots))
2520+
}
25132521
const parts = splitTopLevel(expression, "==")
25142522
if (parts.length === 1) {
25152523
return evaluatePrimary(parts[0], roots)
@@ -2540,7 +2548,7 @@ describe("PR review-state workflow concurrency groups (#1707)", () => {
25402548
}
25412549

25422550
function evaluateTemplate(template: string, roots: ExpressionRoots): string {
2543-
return template.replace(/\$\{\{(.+?)\}\}/s, (_match, expression: string) => {
2551+
return template.replace(/\$\{\{(.+?)\}\}/gs, (_match, expression: string) => {
25442552
const value = evaluateExpression(expression, roots)
25452553
return isTruthy(value) ? String(value) : ""
25462554
})
@@ -2719,6 +2727,69 @@ describe("PR review-state workflow concurrency groups (#1707)", () => {
27192727
}
27202728
})
27212729

2730+
it("keys read-only fork review runs per-run at both levels so they cannot starve mutating runs", () => {
2731+
// Fork pull_request_review runs get a read-only token (see isReadOnlyRun
2732+
// in the reconcile script) and write nothing, so they must not hold the
2733+
// PR-keyed groups whose newest-pending-wins could supersede a queued
2734+
// mutating run for the same PR.
2735+
const forkReview = contextFor({
2736+
event_name: "pull_request_review",
2737+
run_id: 606000111,
2738+
event: {
2739+
pull_request: {
2740+
number: 1664,
2741+
head: { repo: { full_name: "contributor/Zoo-Code" } },
2742+
base: { repo: { full_name: "Zoo-Code-Org/Zoo-Code" } },
2743+
},
2744+
},
2745+
})
2746+
const mutating = contextFor({ event_name: "issue_comment", event: { issue: { number: 1664 } } })
2747+
2748+
expect(workflowGroupFor(forkReview)).toBe("label-pr-review-state-606000111")
2749+
expect(workflowGroupFor(mutating)).toBe("label-pr-review-state-1664")
2750+
expect(workflowGroupFor(forkReview)).not.toBe(workflowGroupFor(mutating))
2751+
2752+
// The `-ro-` infix keeps the read-only shard group from ever numerically
2753+
// colliding with a mutating shard group for the same PR.
2754+
expect(shardGroupFor(forkReview, { pr_number: 1664 })).toBe("label-pr-review-state-reconcile-ro-606000111")
2755+
expect(shardGroupFor(mutating, { pr_number: 1664 })).toBe("label-pr-review-state-reconcile-1664")
2756+
expect(shardGroupFor(forkReview, { pr_number: 1664 })).not.toBe(shardGroupFor(mutating, { pr_number: 1664 }))
2757+
})
2758+
2759+
it("keeps same-repo review events PR-keyed at both levels", () => {
2760+
const sameRepoReview = contextFor({
2761+
event_name: "pull_request_review",
2762+
event: {
2763+
pull_request: {
2764+
number: 1664,
2765+
head: { repo: { full_name: "Zoo-Code-Org/Zoo-Code" } },
2766+
base: { repo: { full_name: "Zoo-Code-Org/Zoo-Code" } },
2767+
},
2768+
},
2769+
})
2770+
2771+
expect(workflowGroupFor(sameRepoReview)).toBe("label-pr-review-state-1664")
2772+
expect(shardGroupFor(sameRepoReview, { pr_number: 1664 })).toBe("label-pr-review-state-reconcile-1664")
2773+
})
2774+
2775+
it("fails toward run-scoped groups when review head repository metadata is missing", () => {
2776+
// A missing head.repo cannot be proven same-repo, so the guard treats the
2777+
// run as read-only-safe (run-scoped) rather than PR-keyed.
2778+
const missingHead = contextFor({
2779+
event_name: "pull_request_review",
2780+
run_id: 606000222,
2781+
event: {
2782+
pull_request: {
2783+
number: 1664,
2784+
base: { repo: { full_name: "Zoo-Code-Org/Zoo-Code" } },
2785+
},
2786+
},
2787+
})
2788+
2789+
expect(workflowGroupFor(missingHead)).toBe("label-pr-review-state-606000222")
2790+
expect(shardGroupFor(missingHead, { pr_number: 1664 })).toBe("label-pr-review-state-reconcile-ro-606000222")
2791+
})
2792+
27222793
it("keeps the empty-resolution shard fallback unique per run", () => {
27232794
const runA = contextFor({
27242795
event_name: "workflow_run",

0 commit comments

Comments
 (0)