Skip to content

Commit 2b98f54

Browse files
only21milclaude
andauthored
fix(github): expand pull request context from the merge base (#23638)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 72986b4 commit 2b98f54

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
@@ -574,6 +574,7 @@ const checkContextsPage = (
574574
});
575575

576576
const pullRequestRefs = encodeJson({ base: { sha: "a1b2c3d" }, head: { sha: "b1c2d3e" } });
577+
const pullRequestMergeBase = encodeJson({ merge_base_commit: { sha: "c1d2e3f" } });
577578

578579
const crossRepositoryDetail = (headRefOid = "abc123") =>
579580
coreResponse({
@@ -2856,6 +2857,34 @@ layer("GitHubPullRequestApi.layer", (it) => {
28562857
}),
28572858
);
28582859

2860+
it.effect("expands a pull request file from its merge base, not the moved base tip", () =>
2861+
Effect.gen(function* () {
2862+
const answers: Record<string, string> = {
2863+
"repos/acme/web/pulls/7": pullRequestRefs,
2864+
"repos/acme/web/compare/a1b2c3d...b1c2d3e?per_page=1&page=2": pullRequestMergeBase,
2865+
"repos/acme/web/contents/src/a.ts?ref=a1b2c3d": "base tip\n",
2866+
"repos/acme/web/contents/src/a.ts?ref=c1d2e3f": "merge base\n",
2867+
"repos/acme/web/contents/src/a.ts?ref=b1c2d3e": "head\n",
2868+
};
2869+
mockedExecute.mockImplementation((call) =>
2870+
Effect.succeed(output(call.kind === "rest" ? (answers[call.path] ?? "") : "")),
2871+
);
2872+
const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
2873+
2874+
const contents = yield* cli.getPullRequestDiffFileContents({
2875+
cwd: "/w",
2876+
repository: "acme/web",
2877+
host: "github.com",
2878+
number: 7,
2879+
changeType: "change",
2880+
oldPath: "src/a.ts",
2881+
newPath: "src/a.ts",
2882+
});
2883+
2884+
expect(contents).toEqual({ oldContents: "merge base\n", newContents: "head\n" });
2885+
}),
2886+
);
2887+
28592888
it.effect("reports unusable diff revisions as a structured error", () =>
28602889
Effect.gen(function* () {
28612890
mockedExecute.mockReturnValueOnce(Effect.succeed(output("not-a-sha\tstill-not-a-sha\n")));
@@ -2885,6 +2914,7 @@ layer("GitHubPullRequestApi.layer", (it) => {
28852914
it.effect("reports undecodable diff file contents as binary", () =>
28862915
Effect.gen(function* () {
28872916
mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequestRefs)));
2917+
mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequestMergeBase)));
28882918
mockedExecute.mockReturnValueOnce(Effect.succeed(output("binary�contents", false, true)));
28892919
const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
28902920

@@ -2911,6 +2941,7 @@ layer("GitHubPullRequestApi.layer", (it) => {
29112941
it.effect("returns valid text containing a literal replacement character", () =>
29122942
Effect.gen(function* () {
29132943
mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequestRefs)));
2944+
mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequestMergeBase)));
29142945
mockedExecute.mockReturnValueOnce(Effect.succeed(output("before�after")));
29152946
const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
29162947

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,13 @@ const decodeRevisionRefs = Schema.decodeUnknownOption(
863863
),
864864
);
865865

866+
/** Where a pull request's head forked off its base, the commit its patch is computed from. */
867+
const decodeMergeBase = Schema.decodeUnknownOption(
868+
Schema.fromJsonString(
869+
Schema.Struct({ merge_base_commit: Schema.Struct({ sha: Schema.String }) }),
870+
),
871+
);
872+
866873
const GRAPHQL_MERGE_METHODS = {
867874
merge: "MERGE",
868875
squash: "SQUASH",
@@ -1299,6 +1306,29 @@ export const make = Effect.gen(function* () {
12991306
...(input.commit === undefined ? {} : { commit: input.commit }),
13001307
});
13011308
}
1309+
// A pull request's patch runs from the merge base, and the base branch may have moved on
1310+
// since, so its old side is read there. Page 2 of a compare leaves out the file list; a
1311+
// failed read falls back to the base branch tip.
1312+
const oldRef =
1313+
input.commit === undefined && input.changeType !== "new"
1314+
? yield* api
1315+
.rest({
1316+
host: input.host,
1317+
operation: "getPullRequestDiffFileContents",
1318+
path: `repos/${owner}/${name}/compare/${baseRef}...${headRef}?per_page=1&page=2`,
1319+
timeout: DIFF_TIMEOUT,
1320+
})
1321+
.pipe(
1322+
Effect.map((response) =>
1323+
decodeMergeBase(response.body).pipe(
1324+
Option.map((compare) => compare.merge_base_commit.sha),
1325+
Option.filter(isCommitSha),
1326+
Option.getOrElse(() => baseRef ?? ""),
1327+
),
1328+
),
1329+
Effect.orElseSucceed(() => baseRef ?? ""),
1330+
)
1331+
: (baseRef ?? "");
13021332

13031333
const readFile = (revision: string, filePath: string) =>
13041334
api
@@ -1329,7 +1359,7 @@ export const make = Effect.gen(function* () {
13291359

13301360
const [oldContents, newContents] = yield* Effect.all(
13311361
[
1332-
input.changeType === "new" ? Effect.succeed("") : readFile(baseRef ?? "", input.oldPath),
1362+
input.changeType === "new" ? Effect.succeed("") : readFile(oldRef, input.oldPath),
13331363
input.changeType === "deleted" ? Effect.succeed("") : readFile(headRef, input.newPath),
13341364
],
13351365
{ concurrency: 2 },

0 commit comments

Comments
 (0)