Skip to content

Commit 124b12f

Browse files
zoomote[bot]roomoteedelauna
authored
[Fix] Fork PR review-state labels stay stale until the hourly sweep (Zoo-Code-Org#2049)
* fix(ci): reconcile fork PR review labels in a privileged workflow_run follow-up (Zoo-Code-Org#2046) * test(ci): cover self-run guard for non-review workflow_run events --------- Co-authored-by: Roomote <[email protected]> Co-authored-by: Elliott de Launay <[email protected]>
1 parent cb698be commit 124b12f

2 files changed

Lines changed: 147 additions & 3 deletions

File tree

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

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,22 @@ on:
3232
# provides a trusted base-repository event that can reconcile those PRs promptly.
3333
issue_comment:
3434
types: [created, edited]
35+
# Fork pull_request_review runs get a read-only token and skip their label
36+
# writes. This workflow lists itself so such a run's completion re-triggers
37+
# it with a privileged token that writes the computed labels (issue #2046).
38+
# The resolve job accepts a workflow_run of this workflow only when the
39+
# completed run was itself a pull_request_review run, so the chain stops
40+
# after one hop. The privileged follow-up takes no artifact input and never
41+
# checks out code, so no untrusted PR code or artifacts are touched.
3542
workflow_run:
36-
workflows: ["Code QA Roo Code", "E2E Tests (Mocked)", "Webview Visual Regression", "CodeQL Advanced"]
43+
workflows:
44+
[
45+
"Code QA Roo Code",
46+
"E2E Tests (Mocked)",
47+
"Webview Visual Regression",
48+
"CodeQL Advanced",
49+
"Label PR review state",
50+
]
3751
types: [completed]
3852

3953
permissions:
@@ -85,9 +99,17 @@ concurrency:
8599
jobs:
86100
resolve:
87101
name: Zoo Code / resolve PRs to reconcile
102+
# The second clause is the loop guard for the self workflow_run
103+
# follow-up (issue #2046): accept a workflow_run of this workflow only
104+
# when the completed run was triggered by pull_request_review. A run
105+
# triggered by workflow_run has event 'workflow_run', so the next level
106+
# is filtered out and the chain stops after one hop.
88107
if: >-
89-
github.event_name != 'issue_comment' ||
90-
(github.event.issue.pull_request && github.event.comment.user.login == 'coderabbitai[bot]')
108+
(github.event_name != 'issue_comment' ||
109+
(github.event.issue.pull_request && github.event.comment.user.login == 'coderabbitai[bot]')) &&
110+
(github.event_name != 'workflow_run' ||
111+
github.event.workflow_run.name != 'Label PR review state' ||
112+
github.event.workflow_run.event == 'pull_request_review')
91113
runs-on: ubuntu-latest
92114
outputs:
93115
pr_numbers: ${{ steps.resolve.outputs.pr_numbers }}
@@ -125,6 +147,22 @@ jobs:
125147
return;
126148
}
127149
150+
// Loop guard for the self workflow_run follow-up (issue #2046),
151+
// mirroring the job-level `if`: accept a workflow_run of this
152+
// workflow only when the completed run was triggered by
153+
// pull_request_review. A run triggered by workflow_run has event
154+
// 'workflow_run', so the chain stops after one hop.
155+
if (context.eventName === 'workflow_run' &&
156+
context.payload.workflow_run?.name === 'Label PR review state' &&
157+
context.payload.workflow_run?.event !== 'pull_request_review') {
158+
core.info(
159+
'Ignoring workflow_run of this workflow not triggered by a ' +
160+
'pull_request_review run'
161+
);
162+
core.setOutput('pr_numbers', '[]');
163+
return;
164+
}
165+
128166
// Resolve which PRs this event targets. Single-PR events carry the
129167
// number in the payload; sweep triggers (hourly schedule, pushes to
130168
// main) list every open PR; workflow_run events use the run's
@@ -195,6 +233,9 @@ jobs:
195233
if: >-
196234
(github.event_name != 'issue_comment' ||
197235
(github.event.issue.pull_request && github.event.comment.user.login == 'coderabbitai[bot]')) &&
236+
(github.event_name != 'workflow_run' ||
237+
github.event.workflow_run.name != 'Label PR review state' ||
238+
github.event.workflow_run.event == 'pull_request_review') &&
198239
needs.resolve.outputs.pr_numbers != '[]'
199240
strategy:
200241
fail-fast: false

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

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ interface HarnessOptions {
3535
workflowRunFallback?: "match" | "sha-mismatch" | "base-mismatch" | "none"
3636
workflowRunHeadBranch?: string
3737
workflowRunMissing?: "repository" | "branch" | "sha"
38+
workflowRunName?: string
39+
workflowRunEvent?: string
3840
workflowDispatchPrNumber?: number
3941
prNumber?: number
4042
runNumber?: number
@@ -175,6 +177,11 @@ function buildEventPayload(eventName: string, options: HarnessOptions) {
175177
: eventName === "workflow_run"
176178
? {
177179
workflow_run: {
180+
// Defaults model a listed CI workflow so existing
181+
// workflow_run tests stay outside the self-run loop
182+
// guard (issue #2046).
183+
name: options.workflowRunName ?? "Code QA Roo Code",
184+
event: options.workflowRunEvent ?? "pull_request",
178185
pull_requests:
179186
options.workflowRunAssociated === false
180187
? []
@@ -2159,6 +2166,102 @@ describe("PR review-state workflow", () => {
21592166
expect(resolution.prNumbers()).toEqual([])
21602167
})
21612168

2169+
describe("fork review self-follow-up (#2046)", () => {
2170+
it("writes labels for a fork PR in the trusted run that follows its read-only review run", async () => {
2171+
// The workflow lists itself under workflow_run so a fork
2172+
// pull_request_review run's completion re-triggers it with a
2173+
// privileged token; both jobs gate that self-run on the completed
2174+
// run's event.
2175+
expect(workflow.on.workflow_run.workflows).toContain("Label PR review state")
2176+
expect(String(workflow.jobs.resolve.if)).toContain(
2177+
"github.event.workflow_run.event == 'pull_request_review'",
2178+
)
2179+
expect(String(workflow.jobs.reconcile.if)).toContain(
2180+
"github.event.workflow_run.event == 'pull_request_review'",
2181+
)
2182+
2183+
// The fork review run computes the right label but writes nothing.
2184+
const readOnly = await runWorkflow({ eventName: "pull_request_review", fork: true })
2185+
2186+
expect(readOnly.addLabels).not.toHaveBeenCalled()
2187+
expect(readOnly.removeLabel).not.toHaveBeenCalled()
2188+
expect(readOnly.setFailed).not.toHaveBeenCalled()
2189+
2190+
// Its completion re-triggers this workflow as a trusted workflow_run.
2191+
// Fork runs backfill pull_requests empty, so the resolve job falls
2192+
// back to the exact head owner, branch, and SHA lookup.
2193+
const resolution = await runResolve({
2194+
eventName: "workflow_run",
2195+
fork: true,
2196+
workflowRunAssociated: false,
2197+
workflowRunFallback: "match",
2198+
workflowRunName: "Label PR review state",
2199+
workflowRunEvent: "pull_request_review",
2200+
})
2201+
2202+
expect(resolution.listPullRequests).toHaveBeenCalledWith(
2203+
expect.objectContaining({ head: "contributor:feature/test", state: "open" }),
2204+
)
2205+
expect(resolution.prNumbers()).toEqual([1437])
2206+
2207+
// The trusted follow-up shard writes the label the read-only run
2208+
// computed but could not write. The reconcile script reads the PR
2209+
// from the matrix number and ignores the workflow_run name and
2210+
// event, so the default workflow_run payload is enough here.
2211+
const result = await runWorkflow({ eventName: "workflow_run", fork: true })
2212+
2213+
expect(result.addLabels).toHaveBeenCalledWith(
2214+
expect.objectContaining({ labels: ["coderabbit-review-active"] }),
2215+
)
2216+
expect(result.setFailed).not.toHaveBeenCalled()
2217+
})
2218+
2219+
it("ignores a self workflow_run triggered by a non-review run", async () => {
2220+
// Every other run of this workflow (hourly sweep, push to main,
2221+
// dispatch, pull_request_target) also completes and re-triggers it;
2222+
// those follow-ups carry no new review state and are filtered out.
2223+
for (const workflowRunEvent of ["schedule", "push", "workflow_dispatch", "pull_request_target"]) {
2224+
const resolution = await runResolve({
2225+
eventName: "workflow_run",
2226+
workflowRunName: "Label PR review state",
2227+
workflowRunEvent,
2228+
})
2229+
2230+
expect(resolution.listPullRequests).not.toHaveBeenCalled()
2231+
expect(resolution.getPullRequest).not.toHaveBeenCalled()
2232+
expect(resolution.prNumbers()).toEqual([])
2233+
}
2234+
})
2235+
2236+
it("does not apply the self-run guard to other workflows that report event workflow_run", async () => {
2237+
// Only the 'Label PR review state' name is guarded. A listed CI
2238+
// workflow with event 'workflow_run' must still resolve its PR.
2239+
const resolution = await runResolve({
2240+
eventName: "workflow_run",
2241+
workflowRunName: "Code QA Roo Code",
2242+
workflowRunEvent: "workflow_run",
2243+
})
2244+
2245+
expect(resolution.prNumbers()).toEqual([1437])
2246+
})
2247+
2248+
it("stops the self workflow_run chain after one hop", async () => {
2249+
// The trusted follow-up run completes with event 'workflow_run'. The
2250+
// guard requires 'pull_request_review', so its own completion cannot
2251+
// queue another run.
2252+
const resolution = await runResolve({
2253+
eventName: "workflow_run",
2254+
fork: true,
2255+
workflowRunName: "Label PR review state",
2256+
workflowRunEvent: "workflow_run",
2257+
})
2258+
2259+
expect(resolution.listPullRequests).not.toHaveBeenCalled()
2260+
expect(resolution.getPullRequest).not.toHaveBeenCalled()
2261+
expect(resolution.prNumbers()).toEqual([])
2262+
})
2263+
})
2264+
21622265
it("invalidates prior automated and human reviews after a new push", async () => {
21632266
const result = await runWorkflow({
21642267
permissions: { maintainer: "write" },

0 commit comments

Comments
 (0)