Skip to content

Commit a89275d

Browse files
zoomote[bot]roomoteedelauna
authored
[Fix] Review-state label on fork PRs does not update after a new review (#2134)
* fix(ci): dispatch fork review follow-up when head_repository is the base repo * test: reject incorrect head filters in fork review regression --------- Co-authored-by: Roomote <[email protected]> Co-authored-by: Elliott de Launay <[email protected]>
1 parent 13cb03a commit a89275d

3 files changed

Lines changed: 103 additions & 73 deletions

File tree

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

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -52,43 +52,44 @@ jobs:
5252
return;
5353
}
5454
55-
// Same-repository review runs already ran with a privileged
56-
// token and wrote their labels; only fork runs need the
57-
// follow-up.
58-
if (run.head_repository && run.head_repository.fork === false) {
59-
core.info('Ignoring same-repository pull_request_review run');
60-
return;
61-
}
62-
63-
// Resolve which PRs the completed run targeted. Fork runs
64-
// backfill pull_requests empty, so fall back to the exact head
65-
// owner, branch, and SHA lookup used by the resolve job of
66-
// label-pr-review-state.yml. Keep both copies in sync.
55+
// GitHub reports the base repository as head_repository on
56+
// every pull_request_review run, fork PR or not (issue
57+
// #2133), so the payload cannot tell fork from same-repo
58+
// runs. Dispatching for a same-repo PR is idempotent: the
59+
// reconciliation writes the same labels, and its completion
60+
// carries event 'workflow_dispatch', which the guard above
61+
// filters out.
62+
//
63+
// Resolve which PRs the completed run targeted. Review runs
64+
// backfill pull_requests empty for fork PRs, and the head
65+
// owner is not recoverable from head_repository, so the
66+
// fallback lists open PRs and matches the exact head branch,
67+
// SHA, and base repository. The workflow_run branch of
68+
// label-pr-review-state.yml keeps an owner-and-branch query
69+
// instead: its runs are CI runs triggered by pull_request,
70+
// whose head_repository is the real fork.
6771
let prNumbers = [];
6872
if (run.pull_requests?.length > 0) {
6973
prNumbers = run.pull_requests.map(pr => pr.number);
70-
} else {
71-
const headOwner = run.head_repository?.owner?.login;
72-
if (headOwner && run.head_branch && run.head_sha) {
73-
const candidates = await github.paginate(github.rest.pulls.list, {
74-
owner,
75-
repo,
76-
state: 'open',
77-
head: `${headOwner}:${run.head_branch}`,
78-
per_page: 100,
79-
});
80-
const baseRepository = `${owner}/${repo}`.toLowerCase();
81-
prNumbers = candidates
82-
.filter(pr =>
83-
pr.head?.sha === run.head_sha &&
84-
pr.base?.repo?.full_name?.toLowerCase() === baseRepository
85-
)
86-
.map(pr => pr.number);
87-
core.info(
88-
`Resolved workflow_run ${run.id ?? '(unknown)'} to ` +
89-
`${prNumbers.length} PR(s) by exact head owner, branch, and SHA`
90-
);
91-
}
74+
} else if (run.head_branch && run.head_sha) {
75+
const candidates = await github.paginate(github.rest.pulls.list, {
76+
owner,
77+
repo,
78+
state: 'open',
79+
per_page: 100,
80+
});
81+
const baseRepository = `${owner}/${repo}`.toLowerCase();
82+
prNumbers = candidates
83+
.filter(pr =>
84+
pr.head?.sha === run.head_sha &&
85+
pr.head?.ref === run.head_branch &&
86+
pr.base?.repo?.full_name?.toLowerCase() === baseRepository
87+
)
88+
.map(pr => pr.number);
89+
core.info(
90+
`Resolved workflow_run ${run.id ?? '(unknown)'} to ` +
91+
`${prNumbers.length} PR(s) by exact head branch, SHA, and base repository`
92+
);
9293
}
9394
9495
// Each dispatch starts a privileged workflow_dispatch run of the

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,13 @@ jobs:
138138
// number in the payload; sweep triggers (hourly schedule, pushes to
139139
// main) list every open PR; workflow_run events use the run's
140140
// associated PRs, with a fork-safe backfill by exact head owner,
141-
// branch, and SHA when GitHub left pull_requests empty. Keep the
142-
// backfill in sync with label-pr-review-state-fork-review-followup.yml.
141+
// branch, and SHA when GitHub left pull_requests empty. The
142+
// workflows listed in workflow_run trigger on pull_request, whose
143+
// head_repository is the real fork, so the owner-and-branch query
144+
// is safe here. The follow-up workflow's copy of this backfill
145+
// matches by head branch, SHA, and base repository instead: its
146+
// pull_request_review runs report the base repository as
147+
// head_repository (issue #2133).
143148
let prNumbers = [];
144149
if (context.payload.pull_request?.number) {
145150
prNumbers = [context.payload.pull_request.number];

‎src/services/__tests__/fork-review-followup-workflow.test.ts‎

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,43 @@ const workflow = parse(
1515
)
1616
const dispatchScript = workflow.jobs.dispatch.steps[0].with.script as string
1717

18-
const SHA = "a".repeat(40)
18+
// Head commit and branch of fork PR #1662 (fork-owner/Zoo-Code), the run
19+
// documented in issue #2133.
20+
const SHA = "029ea76b91f0c784087989470c81ade55b13c82f"
21+
const HEAD_BRANCH = "fix/1591-openai-compatible-cache-reads"
1922
const OLD_SHA = "b".repeat(40)
2023

2124
interface HarnessOptions {
2225
workflowRunEvent?: string
23-
fork?: boolean
2426
associatedPrNumbers?: number[]
25-
fallback?: "match" | "sha-mismatch" | "base-mismatch" | "none"
26-
headRepository?: null
27-
headSha?: string
27+
fallback?: "match" | "sha-mismatch" | "ref-mismatch" | "base-mismatch" | "none"
28+
headBranch?: string | null
29+
headSha?: string | null
2830
failDispatchFor?: number
2931
}
3032

3133
function buildForkPr() {
3234
return {
33-
number: 1437,
35+
number: 1662,
3436
state: "open",
35-
head: { sha: SHA, repo: { full_name: "contributor/Zoo-Code" } },
37+
head: { ref: HEAD_BRANCH, sha: SHA, repo: { full_name: "fork-owner/Zoo-Code" } },
3638
base: { ref: "main", repo: { full_name: "Zoo-Code-Org/Zoo-Code" } },
3739
}
3840
}
3941

4042
/** Executes the embedded github-script follow-up against deterministic GitHub API doubles. */
4143
async function runFollowup(options: HarnessOptions = {}) {
42-
const fork = options.fork ?? true
4344
const pr = buildForkPr()
44-
const listPullRequests = vi.fn(async ({ state }: { state?: string }) => {
45+
// Like the GitHub API, honor a `head` filter ("owner:branch"). A base-owner filter
46+
// matches no fork PR, so the broken request cannot pass the regression scenario.
47+
const listPullRequests = vi.fn(async ({ state, head }: { state?: string; head?: string }) => {
4548
if (state !== "open") return []
49+
if (head !== undefined && head !== `fork-owner:${HEAD_BRANCH}`) return []
4650
if (options.fallback === "match") return [pr]
4751
if (options.fallback === "sha-mismatch") return [{ ...pr, head: { ...pr.head, sha: OLD_SHA } }]
52+
if (options.fallback === "ref-mismatch") {
53+
return [{ ...pr, head: { ...pr.head, ref: "some-other-branch" } }]
54+
}
4855
if (options.fallback === "base-mismatch") {
4956
return [{ ...pr, base: { ...pr.base, repo: { full_name: "someone-else/Zoo-Code" } } }]
5057
}
@@ -69,18 +76,21 @@ async function runFollowup(options: HarnessOptions = {}) {
6976
repo: { owner: "Zoo-Code-Org", repo: "Zoo-Code" },
7077
payload: {
7178
workflow_run: {
72-
id: 987654,
79+
// Fields copied from workflow run 39534658962, the read-only fork
80+
// review run for PR #1662 documented in issue #2133. Fields the
81+
// script never reads are trimmed. GitHub reports the base
82+
// repository as head_repository on every pull_request_review run,
83+
// even for fork PRs, and backfills pull_requests empty for them.
84+
id: 39534658962,
7385
event: options.workflowRunEvent ?? "pull_request_review",
7486
pull_requests: (options.associatedPrNumbers ?? []).map((number) => ({ number })),
75-
head_repository:
76-
options.headRepository === null
77-
? null
78-
: {
79-
owner: { login: fork ? "contributor" : "Zoo-Code-Org" },
80-
fork,
81-
},
82-
head_branch: "feature/test",
83-
head_sha: options.headSha ?? SHA,
87+
head_repository: {
88+
full_name: "Zoo-Code-Org/Zoo-Code",
89+
owner: { login: "Zoo-Code-Org" },
90+
fork: false,
91+
},
92+
head_branch: options.headBranch === undefined ? HEAD_BRANCH : options.headBranch,
93+
head_sha: options.headSha === undefined ? SHA : options.headSha,
8494
},
8595
},
8696
}
@@ -97,7 +107,7 @@ async function runFollowup(options: HarnessOptions = {}) {
97107
return { listPullRequests, createWorkflowDispatch, info: core.info, setFailed: core.setFailed }
98108
}
99109

100-
describe("fork review follow-up workflow (#2046, #2086)", () => {
110+
describe("fork review follow-up workflow (#2046, #2086, #2133)", () => {
101111
it("listens to the label workflow without listing itself", () => {
102112
expect(workflow.name).toBe("Label PR review state fork review follow-up")
103113
expect(workflow.on.workflow_run.workflows).toEqual(["Label PR review state"])
@@ -121,26 +131,30 @@ describe("fork review follow-up workflow (#2046, #2086)", () => {
121131
})
122132

123133
it("dispatches a privileged reconciliation for the fork PR a read-only review run targeted", async () => {
124-
// Fork runs backfill pull_requests empty, so the follow-up resolves the
125-
// PR by exact head owner, branch, and SHA.
134+
// Fork runs backfill pull_requests empty, and head_repository is the
135+
// base repository (issue #2133), so the follow-up resolves the PR by
136+
// exact head branch, SHA, and base repository.
126137
const result = await runFollowup({ associatedPrNumbers: [], fallback: "match" })
127138

128-
expect(result.listPullRequests).toHaveBeenCalledWith(
129-
expect.objectContaining({ head: "contributor:feature/test", state: "open" }),
130-
)
139+
expect(result.listPullRequests).toHaveBeenCalledWith({
140+
owner: "Zoo-Code-Org",
141+
repo: "Zoo-Code",
142+
state: "open",
143+
per_page: 100,
144+
})
131145
expect(result.createWorkflowDispatch).toHaveBeenCalledTimes(1)
132146
expect(result.createWorkflowDispatch).toHaveBeenCalledWith({
133147
owner: "Zoo-Code-Org",
134148
repo: "Zoo-Code",
135149
workflow_id: "label-pr-review-state.yml",
136150
ref: "main",
137-
inputs: { pull_request_number: "1437" },
151+
inputs: { pull_request_number: "1662" },
138152
})
139153
expect(result.setFailed).not.toHaveBeenCalled()
140154
})
141155

142156
it("dispatches directly for runs whose pull_requests are populated", async () => {
143-
const result = await runFollowup({ associatedPrNumbers: [1437] })
157+
const result = await runFollowup({ associatedPrNumbers: [1662] })
144158

145159
expect(result.listPullRequests).not.toHaveBeenCalled()
146160
expect(result.createWorkflowDispatch).toHaveBeenCalledTimes(1)
@@ -169,18 +183,28 @@ describe("fork review follow-up workflow (#2046, #2086)", () => {
169183
}
170184
})
171185

172-
it("ignores a same-repository review run, which already wrote its labels", async () => {
173-
const result = await runFollowup({ fork: false, associatedPrNumbers: [1437] })
186+
it("dispatches for a same-repository review run, whose payload is indistinguishable from a fork run", async () => {
187+
// Every pull_request_review run reports head_repository as the base
188+
// repository with fork: false (issue #2133), so fork-ness cannot gate
189+
// the dispatch. Same-repo PRs appear in pull_requests, but resolving
190+
// through the fallback works for them too, and the dispatch is
191+
// idempotent: the reconciliation writes the same labels.
192+
const result = await runFollowup({ fallback: "match" })
174193

175-
expect(result.listPullRequests).not.toHaveBeenCalled()
176-
expect(result.createWorkflowDispatch).not.toHaveBeenCalled()
194+
expect(result.createWorkflowDispatch).toHaveBeenCalledTimes(1)
195+
expect(result.createWorkflowDispatch).toHaveBeenCalledWith(
196+
expect.objectContaining({ inputs: { pull_request_number: "1662" } }),
197+
)
198+
expect(result.setFailed).not.toHaveBeenCalled()
177199
})
178200

179-
it("dispatches nothing when no open PR matches the run's exact head", async () => {
180-
const result = await runFollowup({ fallback: "sha-mismatch" })
201+
it("dispatches nothing when no open PR matches the run's exact head branch and SHA", async () => {
202+
for (const fallback of ["sha-mismatch", "ref-mismatch", "none"] as const) {
203+
const result = await runFollowup({ fallback })
181204

182-
expect(result.listPullRequests).toHaveBeenCalledTimes(1)
183-
expect(result.createWorkflowDispatch).not.toHaveBeenCalled()
205+
expect(result.listPullRequests).toHaveBeenCalledTimes(1)
206+
expect(result.createWorkflowDispatch).not.toHaveBeenCalled()
207+
}
184208
})
185209

186210
it("dispatches nothing when the matching PR targets another base repository", async () => {
@@ -190,8 +214,8 @@ describe("fork review follow-up workflow (#2046, #2086)", () => {
190214
expect(result.createWorkflowDispatch).not.toHaveBeenCalled()
191215
})
192216

193-
it("dispatches nothing when the run has no head repository or head SHA to resolve", async () => {
194-
for (const options of [{ headRepository: null }, { headSha: "" }] as const) {
217+
it("dispatches nothing when the run has no head branch or head SHA to resolve", async () => {
218+
for (const options of [{ headBranch: null }, { headSha: null }] as const) {
195219
const result = await runFollowup({ ...options, fallback: "match" })
196220

197221
expect(result.listPullRequests).not.toHaveBeenCalled()

0 commit comments

Comments
 (0)