@@ -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