Skip to content

Commit 0c218ff

Browse files
only21milclaude
andcommitted
fix(github): expand pull request context from the merge base
GitHub computes a pull request's patch from the merge base of its base and head. Expanding hidden lines read the old file at base.sha, which is the base branch tip and moves on as the base branch gets new commits. When the base branch had since changed the same file, the expanded old side no longer matched the patch. Read the merge base from the compare endpoint and load the old side there. Page 2 of a compare leaves out the file list. A failed compare read keeps the base tip, as before. Commit diffs keep their first parent. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5ffe045 commit 0c218ff

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

‎packages/source-control-github/src/server/GitHubPullRequestApi.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ const checkContextsPage = (
571571
});
572572

573573
const pullRequestRefs = encodeJson({ base: { sha: "a1b2c3d" }, head: { sha: "b1c2d3e" } });
574+
const pullRequestMergeBase = encodeJson({ merge_base_commit: { sha: "c1d2e3f" } });
574575

575576
const crossRepositoryDetail = (headRefOid = "abc123") =>
576577
coreResponse({
@@ -2670,6 +2671,34 @@ layer("GitHubPullRequestApi.layer", (it) => {
26702671
}),
26712672
);
26722673

2674+
it.effect("expands a pull request file from its merge base, not the moved base tip", () =>
2675+
Effect.gen(function* () {
2676+
const answers: Record<string, string> = {
2677+
"repos/acme/web/pulls/7": pullRequestRefs,
2678+
"repos/acme/web/compare/a1b2c3d...b1c2d3e?per_page=1&page=2": pullRequestMergeBase,
2679+
"repos/acme/web/contents/src/a.ts?ref=a1b2c3d": "base tip\n",
2680+
"repos/acme/web/contents/src/a.ts?ref=c1d2e3f": "merge base\n",
2681+
"repos/acme/web/contents/src/a.ts?ref=b1c2d3e": "head\n",
2682+
};
2683+
mockedExecute.mockImplementation((call) =>
2684+
Effect.succeed(output(call.kind === "rest" ? (answers[call.path] ?? "") : "")),
2685+
);
2686+
const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
2687+
2688+
const contents = yield* cli.getPullRequestDiffFileContents({
2689+
cwd: "/w",
2690+
repository: "acme/web",
2691+
host: "github.com",
2692+
number: 7,
2693+
changeType: "change",
2694+
oldPath: "src/a.ts",
2695+
newPath: "src/a.ts",
2696+
});
2697+
2698+
expect(contents).toEqual({ oldContents: "merge base\n", newContents: "head\n" });
2699+
}),
2700+
);
2701+
26732702
it.effect("reports unusable diff revisions as a structured error", () =>
26742703
Effect.gen(function* () {
26752704
mockedExecute.mockReturnValueOnce(Effect.succeed(output("not-a-sha\tstill-not-a-sha\n")));
@@ -2699,6 +2728,7 @@ layer("GitHubPullRequestApi.layer", (it) => {
26992728
it.effect("reports undecodable diff file contents as binary", () =>
27002729
Effect.gen(function* () {
27012730
mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequestRefs)));
2731+
mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequestMergeBase)));
27022732
mockedExecute.mockReturnValueOnce(Effect.succeed(output("binary�contents", false, true)));
27032733
const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
27042734

@@ -2725,6 +2755,7 @@ layer("GitHubPullRequestApi.layer", (it) => {
27252755
it.effect("returns valid text containing a literal replacement character", () =>
27262756
Effect.gen(function* () {
27272757
mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequestRefs)));
2758+
mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequestMergeBase)));
27282759
mockedExecute.mockReturnValueOnce(Effect.succeed(output("before�after")));
27292760
const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
27302761

‎packages/source-control-github/src/server/GitHubPullRequestApi.ts‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,13 @@ const decodeRevisionRefs = Schema.decodeUnknownOption(
890890
),
891891
);
892892

893+
/** Where a pull request's head forked off its base, the commit its patch is computed from. */
894+
const decodeMergeBase = Schema.decodeUnknownOption(
895+
Schema.fromJsonString(
896+
Schema.Struct({ merge_base_commit: Schema.Struct({ sha: Schema.String }) }),
897+
),
898+
);
899+
893900
const GRAPHQL_MERGE_METHODS = {
894901
merge: "MERGE",
895902
squash: "SQUASH",
@@ -1337,6 +1344,29 @@ export const make = Effect.gen(function* () {
13371344
...(input.commit === undefined ? {} : { commit: input.commit }),
13381345
});
13391346
}
1347+
// A pull request's patch runs from the merge base, and the base branch may have moved on
1348+
// since, so its old side is read there. Page 2 of a compare leaves out the file list; a
1349+
// failed read falls back to the base branch tip.
1350+
const oldRef =
1351+
input.commit === undefined && input.changeType !== "new"
1352+
? yield* api
1353+
.rest({
1354+
host: input.host,
1355+
operation: "getPullRequestDiffFileContents",
1356+
path: `repos/${owner}/${name}/compare/${baseRef}...${headRef}?per_page=1&page=2`,
1357+
timeout: DIFF_TIMEOUT,
1358+
})
1359+
.pipe(
1360+
Effect.map((response) =>
1361+
decodeMergeBase(response.body).pipe(
1362+
Option.map((compare) => compare.merge_base_commit.sha),
1363+
Option.filter(isCommitSha),
1364+
Option.getOrElse(() => baseRef ?? ""),
1365+
),
1366+
),
1367+
Effect.orElseSucceed(() => baseRef ?? ""),
1368+
)
1369+
: (baseRef ?? "");
13401370

13411371
const readFile = (revision: string, filePath: string) =>
13421372
api
@@ -1367,7 +1397,7 @@ export const make = Effect.gen(function* () {
13671397

13681398
const [oldContents, newContents] = yield* Effect.all(
13691399
[
1370-
input.changeType === "new" ? Effect.succeed("") : readFile(baseRef ?? "", input.oldPath),
1400+
input.changeType === "new" ? Effect.succeed("") : readFile(oldRef, input.oldPath),
13711401
input.changeType === "deleted" ? Effect.succeed("") : readFile(headRef, input.newPath),
13721402
],
13731403
{ concurrency: 2 },

0 commit comments

Comments
 (0)