Skip to content

Commit 8dd8ef3

Browse files
roomoteedelauna
authored andcommitted
fix(ci): key label-pr-review-state concurrency per PR
The label-pr-review-state workflow fires on many repo-wide triggers (pull_request_target, pull_request_review, issue_comment, workflow_run, push to main, hourly schedule, workflow_dispatch) but shared a single concurrency group. GitHub keeps only the newest pending run per group even with cancel-in-progress: false, so bursts of unrelated triggers superseded queued per-PR reconciliation runs and they never executed (e.g. PR #1664's reconcile was queue-cancelled 18s after queueing, and a manual dispatch met the same fate), leaving review-state labels to lag up to an hour behind the hourly sweep. Key the concurrency group per PR (pull_request/issue/workflow_run/ dispatch input number), share one group for sweep triggers (schedule and pushes to main), and fall back to a unique per-run group for events that cannot be tied to a PR (fork workflow_run without backfilled pull_requests) so they are never superseded. cancel-in-progress remains false: within one PR, newest-pending-wins is freshest-state-wins. Adds deterministic vitest coverage that evaluates the group expression and proves per-PR group isolation, the shared sweep bucket, and the unique fallback. Fixes #1707 Co-authored-by: Roomote <[email protected]>
1 parent 9c069e9 commit 8dd8ef3

2 files changed

Lines changed: 250 additions & 1 deletion

File tree

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,24 @@ permissions:
4545
checks: read
4646
statuses: write
4747

48+
# One concurrency group per PR so bursts of activity on unrelated PRs cannot
49+
# starve an individual PR's reconciliation (issue #1707). GitHub keeps only the
50+
# newest pending run per group; keyed per PR that becomes freshest-state-wins
51+
# within a PR. Issue/PR numbers share one GitHub sequence, so issue-comment runs
52+
# can never collide with a different PR's group. Sweep triggers (hourly
53+
# schedule, pushes to main) share a single group, and events that cannot be
54+
# tied to a PR (fork workflow_run without backfilled pull_requests) fall back
55+
# to a unique per-run group so they are never superseded.
4856
concurrency:
49-
group: label-pr-review-state
57+
group: >-
58+
label-pr-review-state-${{
59+
github.event.pull_request.number ||
60+
github.event.issue.number ||
61+
github.event.workflow_run.pull_requests[0].number ||
62+
github.event.inputs.pull_request_number ||
63+
((github.event_name == 'schedule' || github.event_name == 'push') && 'sweep') ||
64+
github.run_id
65+
}}
5066
cancel-in-progress: false
5167

5268
jobs:

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

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2819,3 +2819,236 @@ describe("PR review-state workflow", () => {
28192819
})
28202820
})
28212821
})
2822+
2823+
describe("PR review-state workflow concurrency group (#1707)", () => {
2824+
// Minimal evaluator for the exact GitHub expression subset used by
2825+
// workflow.concurrency.group: top-level `||` chains, `&&`, loose `==`,
2826+
// `github.` property paths, `[0]` indexing (null parent -> null),
2827+
// parenthesized operands, and string/number literals. GitHub expressions
2828+
// use loose truthiness, so `||`/`&&` return operand values rather than
2829+
// booleans and missing properties evaluate to null instead of throwing.
2830+
interface GithubExpressionContext {
2831+
event_name: string
2832+
run_id: number
2833+
event: {
2834+
pull_request?: { number: number }
2835+
issue?: { number: number }
2836+
workflow_run?: { pull_requests: Array<{ number: number }> }
2837+
inputs?: { pull_request_number: number }
2838+
}
2839+
}
2840+
2841+
function splitTopLevel(expression: string, operator: "||" | "&&" | "=="): string[] {
2842+
const parts: string[] = []
2843+
let depth = 0
2844+
let inString = false
2845+
let start = 0
2846+
for (let index = 0; index < expression.length; index++) {
2847+
const char = expression[index]
2848+
if (char === "'") {
2849+
inString = !inString
2850+
} else if (!inString && char === "(") {
2851+
depth++
2852+
} else if (!inString && char === ")") {
2853+
depth--
2854+
} else if (!inString && depth === 0 && expression.startsWith(operator, index)) {
2855+
parts.push(expression.slice(start, index))
2856+
index += operator.length - 1
2857+
start = index + 1
2858+
}
2859+
}
2860+
parts.push(expression.slice(start))
2861+
return parts
2862+
}
2863+
2864+
function isTruthy(value: unknown): boolean {
2865+
return value !== null && value !== undefined && value !== false && value !== 0 && value !== ""
2866+
}
2867+
2868+
function looseEquals(left: unknown, right: unknown): boolean {
2869+
if (left === null || left === undefined || right === null || right === undefined) {
2870+
return (left ?? null) === (right ?? null)
2871+
}
2872+
if (typeof left === "string" && typeof right === "string") {
2873+
return left.toLowerCase() === right.toLowerCase()
2874+
}
2875+
return left === right
2876+
}
2877+
2878+
function resolvePath(path: string, github: GithubExpressionContext): unknown {
2879+
let value: unknown = github
2880+
for (const segment of path.split(".")) {
2881+
if (value === null || value === undefined) {
2882+
return null
2883+
}
2884+
const match = /^([A-Za-z_][A-Za-z0-9_]*)(?:\[(\d+)\])?$/.exec(segment)
2885+
if (!match) {
2886+
throw new Error(`Unsupported path segment: ${segment}`)
2887+
}
2888+
value = (value as Record<string, unknown>)[match[1]]
2889+
if (match[2] !== undefined) {
2890+
if (!Array.isArray(value)) {
2891+
return null
2892+
}
2893+
value = value[Number(match[2])] ?? null
2894+
}
2895+
}
2896+
return value ?? null
2897+
}
2898+
2899+
function evaluatePrimary(expression: string, github: GithubExpressionContext): unknown {
2900+
const current = expression.trim()
2901+
if (current.startsWith("(")) {
2902+
// Strip the parentheses only when the first one wraps the whole expression.
2903+
let depth = 0
2904+
for (let index = 0; index < current.length; index++) {
2905+
if (current[index] === "(") {
2906+
depth++
2907+
} else if (current[index] === ")") {
2908+
depth--
2909+
}
2910+
if (depth === 0) {
2911+
if (index === current.length - 1) {
2912+
return evaluateExpression(current.slice(1, -1), github)
2913+
}
2914+
break
2915+
}
2916+
}
2917+
}
2918+
if (current.startsWith("'") && current.endsWith("'")) {
2919+
return current.slice(1, -1)
2920+
}
2921+
if (/^\d+$/.test(current)) {
2922+
return Number(current)
2923+
}
2924+
if (current.startsWith("github.")) {
2925+
return resolvePath(current.slice("github.".length), github)
2926+
}
2927+
return evaluateExpression(current, github)
2928+
}
2929+
2930+
function evaluateComparison(expression: string, github: GithubExpressionContext): unknown {
2931+
const parts = splitTopLevel(expression, "==")
2932+
if (parts.length === 1) {
2933+
return evaluatePrimary(parts[0], github)
2934+
}
2935+
return looseEquals(evaluatePrimary(parts[0], github), evaluatePrimary(parts[1], github))
2936+
}
2937+
2938+
function evaluateAndChain(expression: string, github: GithubExpressionContext): unknown {
2939+
let result: unknown = true
2940+
for (const part of splitTopLevel(expression, "&&")) {
2941+
result = evaluateComparison(part, github)
2942+
if (!isTruthy(result)) {
2943+
return result
2944+
}
2945+
}
2946+
return result
2947+
}
2948+
2949+
function evaluateExpression(expression: string, github: GithubExpressionContext): unknown {
2950+
let result: unknown = null
2951+
for (const part of splitTopLevel(expression, "||")) {
2952+
result = evaluateAndChain(part, github)
2953+
if (isTruthy(result)) {
2954+
return result
2955+
}
2956+
}
2957+
return result
2958+
}
2959+
2960+
function concurrencyGroupFor(github: GithubExpressionContext): string {
2961+
const groupTemplate = workflow.concurrency.group as string
2962+
return groupTemplate.replace(/\$\{\{(.+?)\}\}/s, (_match, expression: string) => {
2963+
const value = evaluateExpression(expression, github)
2964+
return isTruthy(value) ? String(value) : ""
2965+
})
2966+
}
2967+
2968+
function contextFor(overrides: {
2969+
event_name: string
2970+
run_id?: number
2971+
event?: GithubExpressionContext["event"]
2972+
}): GithubExpressionContext {
2973+
return {
2974+
event_name: overrides.event_name,
2975+
run_id: overrides.run_id ?? 21057,
2976+
event: overrides.event ?? {},
2977+
}
2978+
}
2979+
2980+
it("keeps cancel-in-progress disabled", () => {
2981+
expect(workflow.concurrency["cancel-in-progress"]).toBe(false)
2982+
})
2983+
2984+
it("keys every per-PR event type for the same PR to the same group", () => {
2985+
const contexts = [
2986+
contextFor({ event_name: "pull_request_target", event: { pull_request: { number: 1664 } } }),
2987+
contextFor({ event_name: "pull_request_review", event: { pull_request: { number: 1664 } } }),
2988+
// CodeRabbit status comments arrive as issue_comment events on the PR.
2989+
contextFor({ event_name: "issue_comment", event: { issue: { number: 1664 } } }),
2990+
contextFor({ event_name: "workflow_dispatch", event: { inputs: { pull_request_number: 1664 } } }),
2991+
contextFor({
2992+
event_name: "workflow_run",
2993+
event: { workflow_run: { pull_requests: [{ number: 1664 }] } },
2994+
}),
2995+
]
2996+
2997+
for (const context of contexts) {
2998+
expect(concurrencyGroupFor(context)).toBe("label-pr-review-state-1664")
2999+
}
3000+
})
3001+
3002+
it("keys different PRs to different groups so unrelated triggers cannot starve a pending run", () => {
3003+
// The #1707 regression: with one shared group, a trigger for PR B superseded
3004+
// PR A's pending reconcile. Per-PR keys make that supersession impossible.
3005+
const prA = contextFor({ event_name: "pull_request_target", event: { pull_request: { number: 1664 } } })
3006+
const prB = contextFor({ event_name: "pull_request_target", event: { pull_request: { number: 1707 } } })
3007+
3008+
const groupA = concurrencyGroupFor(prA)
3009+
const groupB = concurrencyGroupFor(prB)
3010+
expect(groupA).toBe("label-pr-review-state-1664")
3011+
expect(groupB).toBe("label-pr-review-state-1707")
3012+
expect(groupB).not.toBe(groupA)
3013+
})
3014+
3015+
it("keys issue comments by issue number, matching the PR group for the same numeric id", () => {
3016+
// Issues and PRs share one GitHub number sequence, so an issue-comment run
3017+
// can never collide with a different PR's group.
3018+
const issueComment = contextFor({ event_name: "issue_comment", event: { issue: { number: 1707 } } })
3019+
const prEvent = contextFor({ event_name: "pull_request_target", event: { pull_request: { number: 1707 } } })
3020+
3021+
expect(concurrencyGroupFor(issueComment)).toBe(concurrencyGroupFor(prEvent))
3022+
})
3023+
3024+
it("falls back to unique per-run groups for workflow runs without an associated PR", () => {
3025+
// Fork workflow_run events leave pull_requests empty, as do branch runs with
3026+
// no open PR. They must never be superseded by (or supersede) another run.
3027+
const forkRunA = contextFor({
3028+
event_name: "workflow_run",
3029+
run_id: 35477088534,
3030+
event: { workflow_run: { pull_requests: [] } },
3031+
})
3032+
const forkRunB = contextFor({
3033+
event_name: "workflow_run",
3034+
run_id: 35477090001,
3035+
event: { workflow_run: { pull_requests: [] } },
3036+
})
3037+
3038+
const groupA = concurrencyGroupFor(forkRunA)
3039+
const groupB = concurrencyGroupFor(forkRunB)
3040+
expect(groupA).toBe("label-pr-review-state-35477088534")
3041+
expect(groupB).toBe("label-pr-review-state-35477090001")
3042+
expect(groupA).not.toBe(groupB)
3043+
expect(groupA).not.toBe("label-pr-review-state-1664")
3044+
expect(groupB).not.toBe("label-pr-review-state-1664")
3045+
})
3046+
3047+
it("shares a single sweep group for the hourly schedule and pushes to main", () => {
3048+
const scheduled = contextFor({ event_name: "schedule" })
3049+
const push = contextFor({ event_name: "push" })
3050+
3051+
expect(concurrencyGroupFor(scheduled)).toBe("label-pr-review-state-sweep")
3052+
expect(concurrencyGroupFor(push)).toBe("label-pr-review-state-sweep")
3053+
})
3054+
})

0 commit comments

Comments
 (0)