Skip to content
TypeScript

fix(github): expand pull request context from the merge base (#23638)

Victor Vogel2b98f54fc052
diff --git a/packages/source-control-github/src/server/GitHubPullRequestApi.test.ts b/packages/source-control-github/src/server/GitHubPullRequestApi.test.ts
index 78deffdfaeb..5a5c46f28d9 100644
--- a/packages/source-control-github/src/server/GitHubPullRequestApi.test.ts
+++ b/packages/source-control-github/src/server/GitHubPullRequestApi.test.ts
@@ -574,6 +574,7 @@ const checkContextsPage = (
});
const pullRequestRefs = encodeJson({ base: { sha: "a1b2c3d" }, head: { sha: "b1c2d3e" } });
+const pullRequestMergeBase = encodeJson({ merge_base_commit: { sha: "c1d2e3f" } });
const crossRepositoryDetail = (headRefOid = "abc123") =>
coreResponse({
@@ -2856,6 +2857,34 @@ layer("GitHubPullRequestApi.layer", (it) => {
}),
);
+ it.effect("expands a pull request file from its merge base, not the moved base tip", () =>
+ Effect.gen(function* () {
+ const answers: Record<string, string> = {
+ "repos/acme/web/pulls/7": pullRequestRefs,
+ "repos/acme/web/compare/a1b2c3d...b1c2d3e?per_page=1&page=2": pullRequestMergeBase,
+ "repos/acme/web/contents/src/a.ts?ref=a1b2c3d": "base tip\n",
+ "repos/acme/web/contents/src/a.ts?ref=c1d2e3f": "merge base\n",
+ "repos/acme/web/contents/src/a.ts?ref=b1c2d3e": "head\n",
+ };
+ mockedExecute.mockImplementation((call) =>
+ Effect.succeed(output(call.kind === "rest" ? (answers[call.path] ?? "") : "")),
+ );
+ const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
+
+ const contents = yield* cli.getPullRequestDiffFileContents({
+ cwd: "/w",
+ repository: "acme/web",
+ host: "github.com",
+ number: 7,
+ changeType: "change",
+ oldPath: "src/a.ts",
+ newPath: "src/a.ts",
+ });
+
+ expect(contents).toEqual({ oldContents: "merge base\n", newContents: "head\n" });
+ }),
+ );
+
it.effect("reports unusable diff revisions as a structured error", () =>
Effect.gen(function* () {
mockedExecute.mockReturnValueOnce(Effect.succeed(output("not-a-sha\tstill-not-a-sha\n")));
@@ -2885,6 +2914,7 @@ layer("GitHubPullRequestApi.layer", (it) => {
it.effect("reports undecodable diff file contents as binary", () =>
Effect.gen(function* () {
mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequestRefs)));
+ mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequestMergeBase)));
mockedExecute.mockReturnValueOnce(Effect.succeed(output("binary�contents", false, true)));
const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
@@ -2911,6 +2941,7 @@ layer("GitHubPullRequestApi.layer", (it) => {
it.effect("returns valid text containing a literal replacement character", () =>
Effect.gen(function* () {
mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequestRefs)));
+ mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequestMergeBase)));
mockedExecute.mockReturnValueOnce(Effect.succeed(output("before�after")));
const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
diff --git a/packages/source-control-github/src/server/GitHubPullRequestApi.ts b/packages/source-control-github/src/server/GitHubPullRequestApi.ts
index b812fd7682e..2defbb66849 100644
--- a/packages/source-control-github/src/server/GitHubPullRequestApi.ts
+++ b/packages/source-control-github/src/server/GitHubPullRequestApi.ts
@@ -863,6 +863,13 @@ const decodeRevisionRefs = Schema.decodeUnknownOption(
),
);
+/** Where a pull request's head forked off its base, the commit its patch is computed from. */
+const decodeMergeBase = Schema.decodeUnknownOption(
+ Schema.fromJsonString(
+ Schema.Struct({ merge_base_commit: Schema.Struct({ sha: Schema.String }) }),
+ ),
+);
+
const GRAPHQL_MERGE_METHODS = {
merge: "MERGE",
squash: "SQUASH",
@@ -1299,6 +1306,29 @@ export const make = Effect.gen(function* () {
...(input.commit === undefined ? {} : { commit: input.commit }),
});
}
+ // A pull request's patch runs from the merge base, and the base branch may have moved on
+ // since, so its old side is read there. Page 2 of a compare leaves out the file list; a
+ // failed read falls back to the base branch tip.
+ const oldRef =
+ input.commit === undefined && input.changeType !== "new"
+ ? yield* api
+ .rest({
+ host: input.host,
+ operation: "getPullRequestDiffFileContents",
+ path: `repos/${owner}/${name}/compare/${baseRef}...${headRef}?per_page=1&page=2`,
+ timeout: DIFF_TIMEOUT,
+ })
+ .pipe(
+ Effect.map((response) =>
+ decodeMergeBase(response.body).pipe(
+ Option.map((compare) => compare.merge_base_commit.sha),
+ Option.filter(isCommitSha),
+ Option.getOrElse(() => baseRef ?? ""),
+ ),
+ ),
+ Effect.orElseSucceed(() => baseRef ?? ""),
+ )
+ : (baseRef ?? "");
const readFile = (revision: string, filePath: string) =>
api
@@ -1329,7 +1359,7 @@ export const make = Effect.gen(function* () {
const [oldContents, newContents] = yield* Effect.all(
[
- input.changeType === "new" ? Effect.succeed("") : readFile(baseRef ?? "", input.oldPath),
+ input.changeType === "new" ? Effect.succeed("") : readFile(oldRef, input.oldPath),
input.changeType === "deleted" ? Effect.succeed("") : readFile(headRef, input.newPath),
],
{ concurrency: 2 },