Skip to content

Commit a34cf2e

Browse files
gregbergeclaude
andauthored
fix(core): identify the test merge from the merge ref, not the payload (#372)
getTestMergeBaseCommitSha() recognised GitHub's test-merge commit by comparing its second parent against pull_request.head.sha. That value can lag behind the merge ref the runner checked out: GitHub recomputes refs/pull/<n>/merge when either branch moves, without always firing a new event. When a push lands in between, the payload still carries the previous head while the checkout is the newer test-merge commit. The guard then rejected a genuine test-merge build and fell back to the merge base, baselining the pull request against its fork point and reporting every change merged into the base branch since as its own. GITHUB_REF is set by the runner from the ref the run was triggered on, so on a pull_request event it names the merge ref and cannot go stale. Together with the existing check that the build runs on GITHUB_SHA it identifies the test merge without the payload; the head.sha comparison stays as the fallback when the merge ref is absent. The parents.length check is split out so a non-merge HEAD is rejected before either identification path runs. Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
1 parent 12a8c62 commit a34cf2e

2 files changed

Lines changed: 73 additions & 3 deletions

File tree

‎packages/core/src/ci-environment/services/github-actions.test.ts‎

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ describe("#getMergeBaseCommitSha", () => {
2020
/** Test-merge commit: `feature` merged into the tip of `main`. */
2121
let mergeSha: string;
2222

23-
function writeEventPayload(input: { baseRef: string }) {
23+
function writeEventPayload(input: { baseRef: string; headSha?: string }) {
2424
writeFileSync(
2525
eventPath,
2626
JSON.stringify({
2727
pull_request: {
2828
number: 1,
29-
head: { ref: "feature", sha: featureSha },
29+
head: { ref: "feature", sha: input.headSha ?? featureSha },
3030
base: { ref: input.baseRef },
3131
},
3232
}),
@@ -137,4 +137,51 @@ describe("#getMergeBaseCommitSha", () => {
137137
);
138138
expect(sha).toBe(forkSha);
139139
});
140+
141+
it("returns the base commit when the payload head is stale", async () => {
142+
// A push landing between the event and the merge ref being recomputed
143+
// leaves "pull_request.head.sha" behind the test-merge commit that is
144+
// actually checked out. GITHUB_REF still names the merge ref, so the build
145+
// must be baselined against the commit GitHub merged in.
146+
writeEventPayload({ baseRef: "main", headSha: forkSha });
147+
const sha = await service.getMergeBaseCommitSha(
148+
{ base: "main", head: "feature" },
149+
createContext({ GITHUB_REF: "refs/pull/1/merge" }),
150+
);
151+
expect(sha).toBe(mainSha);
152+
});
153+
154+
it("falls back to the merge base on a stale payload without the merge ref", async () => {
155+
// Without GITHUB_REF naming the merge ref, the payload is the only way to
156+
// tell GitHub's test merge apart from a merge the author made.
157+
writeEventPayload({ baseRef: "main", headSha: forkSha });
158+
const sha = await service.getMergeBaseCommitSha(
159+
{ base: "main", head: "feature" },
160+
createContext({}),
161+
);
162+
expect(sha).toBe(forkSha);
163+
});
164+
165+
it("ignores the merge ref of another pull request", async () => {
166+
writeEventPayload({ baseRef: "main", headSha: forkSha });
167+
const sha = await service.getMergeBaseCommitSha(
168+
{ base: "main", head: "feature" },
169+
createContext({ GITHUB_REF: "refs/pull/2/merge" }),
170+
);
171+
expect(sha).toBe(forkSha);
172+
});
173+
174+
it("falls back to the merge base when the head is not a merge commit", async () => {
175+
// The merge ref alone must not be trusted when the checkout is not a merge:
176+
// a single-parent commit has no base branch tip to read.
177+
execFileSync("git", ["-C", repoDir, "checkout", "--detach", featureSha]);
178+
const sha = await service.getMergeBaseCommitSha(
179+
{ base: "main", head: "feature" },
180+
createContext({
181+
GITHUB_SHA: featureSha,
182+
GITHUB_REF: "refs/pull/1/merge",
183+
}),
184+
);
185+
expect(sha).toBe(forkSha);
186+
});
140187
});

‎packages/core/src/ci-environment/services/github-actions.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,30 @@ async function getTestMergeBaseCommitSha(
351351

352352
// A test-merge commit merges the pull request head (second parent) into the
353353
// tip of the base branch (first parent).
354-
if (parents?.length !== 2 || parents[1] !== pullRequest.head.sha) {
354+
if (parents?.length !== 2) {
355+
debug(`${sha} is not a merge commit, ignoring the test-merge commit`);
356+
return null;
357+
}
358+
359+
// `GITHUB_REF` is set by the runner from the ref the run was triggered on, so
360+
// on a `pull_request` event it names the merge ref itself. Together with the
361+
// check above that the build runs on `GITHUB_SHA`, it identifies the test
362+
// merge without reading the payload.
363+
//
364+
// The payload can lag behind the merge ref: when a push lands between the
365+
// event and the merge ref being recomputed, `pull_request.head.sha` still
366+
// carries the previous head while the checkout is the newer test-merge
367+
// commit. Comparing the second parent against that stale head then rejects a
368+
// genuine test-merge build, and the fallback baselines it against the fork
369+
// point — reporting every change merged into the base branch since as a
370+
// change of the pull request.
371+
if (ctx.env.GITHUB_REF === `refs/pull/${pullRequest.number}/merge`) {
372+
return parents[0] ?? null;
373+
}
374+
375+
// Without the merge ref, the payload is the only way to tell GitHub's test
376+
// merge apart from a merge the author made.
377+
if (parents[1] !== pullRequest.head.sha) {
355378
debug(`${sha} is not a test-merge commit of #${pullRequest.number}`);
356379
return null;
357380
}

0 commit comments

Comments
 (0)