Skip to content
TypeScript

fix(gitlab): merge request diffs page 30 files at a time so GitLab 15.7 to 17.10 answer (#21909)

Igor Makowski588150e39d37
diff --git a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.legacy.test.ts b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.legacy.test.ts
index 6b059d8fa58..91b649fd985 100644
--- a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.legacy.test.ts
+++ b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.legacy.test.ts
@@ -147,7 +147,7 @@ layer("legacy GitLab diff requests through GitLabCli", (it) => {
);
it.effect("keeps modern pagination without requesting legacy changes", () =>
Effect.gen(function* () {
- run.mockReturnValueOnce(output(Array.from({ length: 100 }, () => file)));
+ run.mockReturnValueOnce(output(Array.from({ length: 30 }, () => file)));
run.mockReturnValueOnce(output([file]));
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
const first = yield* cli.getMergeRequestDiff(input);
@@ -157,7 +157,7 @@ layer("legacy GitLab diff requests through GitLabCli", (it) => {
).toBeNull();
expect(run).toHaveBeenCalledTimes(2);
expect(run.mock.calls[1]?.[0].args).toContain(
- "projects/acme%2Fnested%2Fweb/merge_requests/7/diffs?per_page=100&page=2",
+ "projects/acme%2Fnested%2Fweb/merge_requests/7/diffs?per_page=30&page=2",
);
}),
);
diff --git a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts
index f9a88a84618..30b9ba9b75c 100644
--- a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts
+++ b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts
@@ -430,10 +430,39 @@ layer("GitLabPullRequestCli.layer", (it) => {
assert.strictEqual(mockedExecute.mock.calls.length, 1);
assert.isNotNull(diff.nextCursor);
assert.isFalse(diff.truncated);
- expect(argsOfCall(0)[1]).toContain("merge_requests/7/diffs?per_page=100&page=1");
+ expect(argsOfCall(0)[1]).toContain("merge_requests/7/diffs?per_page=30&page=1");
}),
);
+ it.effect(
+ "pages a merge request diff 30 files at a time, which GitLab 15.7 to 17.10 accept",
+ () =>
+ Effect.gen(function* () {
+ mockedExecute
+ .mockReturnValueOnce(Effect.succeed(output(diffPage(0, 30))))
+ .mockReturnValueOnce(Effect.succeed(output(diffPage(30, 30))))
+ .mockReturnValueOnce(Effect.succeed(output(diffPage(60, 5))));
+ const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
+ const target = { cwd: "/w", repository: "acme/web", number: 7 };
+
+ let patch = "";
+ let cursor: string | undefined;
+ do {
+ const slice = yield* cli.getMergeRequestDiff({ ...target, cursor });
+ patch += slice.patch;
+ cursor = slice.nextCursor ?? undefined;
+ } while (cursor !== undefined);
+
+ expect(mockedExecute.mock.calls.map(([call]) => call.args[1])).toEqual([
+ "projects/acme%2Fweb/merge_requests/7/diffs?per_page=30&page=1",
+ "projects/acme%2Fweb/merge_requests/7/diffs?per_page=30&page=2",
+ "projects/acme%2Fweb/merge_requests/7/diffs?per_page=30&page=3",
+ ]);
+ assert.strictEqual(patch.match(/^diff --git /gm)?.length, 65);
+ expect(patch).toContain("diff --git a/src/64.ts b/src/64.ts");
+ }),
+ );
+
it.effect("refuses a cursor it never handed out rather than reading it into a query", () =>
Effect.gen(function* () {
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
diff --git a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts
index a49d175edac..d66992141f9 100644
--- a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts
+++ b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts
@@ -181,6 +181,14 @@ export type GitLabPullRequestCliError =
/** GitLab's own ceiling on `per_page`, so a larger page has to be walked. */
const MAX_PAGE_SIZE = 100;
const COMMIT_PAGE_SIZE = 100;
+/**
+ * GitLab 15.7 through 17.10 answer the merge request `/diffs` endpoint with a 500 for any
+ * `per_page` above 30 (fixed in 17.11), so those pages stay at a size every version accepts.
+ * A commit's own diff endpoint never had that bug and keeps the full page.
+ */
+const MERGE_REQUEST_DIFF_PAGE_SIZE = 30;
+const diffPageSize = (commit: string | undefined) =>
+ commit === undefined ? MERGE_REQUEST_DIFF_PAGE_SIZE : MAX_PAGE_SIZE;
const CONVERSATION_PAGES = 10;
const DIFF_MAX_OUTPUT_BYTES = 8 * 1024 * 1024;
const DIFF_TIMEOUT_MS = 60_000;
@@ -615,7 +623,7 @@ export const make = Effect.gen(function* () {
? `merge_requests/${input.number}/diffs`
: `repository/commits/${input.commit}/diff`
}?${query([
- ["per_page", String(MAX_PAGE_SIZE)],
+ ["per_page", String(diffPageSize(input.commit))],
["page", String(input.page)],
])}`,
maxOutputBytes: DIFF_MAX_OUTPUT_BYTES,
@@ -673,7 +681,7 @@ export const make = Effect.gen(function* () {
const patch = decoded.success.patch;
// Counted before decoding, so a page whose files all failed to decode still moves on
// rather than pointing the reader back at the page it just read.
- const morePages = !legacy && decoded.success.rawCount >= MAX_PAGE_SIZE;
+ const morePages = !legacy && decoded.success.rawCount >= diffPageSize(input.commit);
return Effect.succeed({
// The slice ends on a newline, so a file GitLab gave a header and no hunks for does
// not run into the first line of the next slice.