Skip to content

Commit 588150e

Browse files
authored
fix(gitlab): merge request diffs page 30 files at a time so GitLab 15.7 to 17.10 answer (pingdotgg#21909)
1 parent aacd1ee commit 588150e

3 files changed

Lines changed: 42 additions & 5 deletions

File tree

‎packages/source-control-gitlab/src/server/GitLabPullRequestCli.legacy.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ layer("legacy GitLab diff requests through GitLabCli", (it) => {
147147
);
148148
it.effect("keeps modern pagination without requesting legacy changes", () =>
149149
Effect.gen(function* () {
150-
run.mockReturnValueOnce(output(Array.from({ length: 100 }, () => file)));
150+
run.mockReturnValueOnce(output(Array.from({ length: 30 }, () => file)));
151151
run.mockReturnValueOnce(output([file]));
152152
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
153153
const first = yield* cli.getMergeRequestDiff(input);
@@ -157,7 +157,7 @@ layer("legacy GitLab diff requests through GitLabCli", (it) => {
157157
).toBeNull();
158158
expect(run).toHaveBeenCalledTimes(2);
159159
expect(run.mock.calls[1]?.[0].args).toContain(
160-
"projects/acme%2Fnested%2Fweb/merge_requests/7/diffs?per_page=100&page=2",
160+
"projects/acme%2Fnested%2Fweb/merge_requests/7/diffs?per_page=30&page=2",
161161
);
162162
}),
163163
);

‎packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,39 @@ layer("GitLabPullRequestCli.layer", (it) => {
430430
assert.strictEqual(mockedExecute.mock.calls.length, 1);
431431
assert.isNotNull(diff.nextCursor);
432432
assert.isFalse(diff.truncated);
433-
expect(argsOfCall(0)[1]).toContain("merge_requests/7/diffs?per_page=100&page=1");
433+
expect(argsOfCall(0)[1]).toContain("merge_requests/7/diffs?per_page=30&page=1");
434434
}),
435435
);
436436

437+
it.effect(
438+
"pages a merge request diff 30 files at a time, which GitLab 15.7 to 17.10 accept",
439+
() =>
440+
Effect.gen(function* () {
441+
mockedExecute
442+
.mockReturnValueOnce(Effect.succeed(output(diffPage(0, 30))))
443+
.mockReturnValueOnce(Effect.succeed(output(diffPage(30, 30))))
444+
.mockReturnValueOnce(Effect.succeed(output(diffPage(60, 5))));
445+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
446+
const target = { cwd: "/w", repository: "acme/web", number: 7 };
447+
448+
let patch = "";
449+
let cursor: string | undefined;
450+
do {
451+
const slice = yield* cli.getMergeRequestDiff({ ...target, cursor });
452+
patch += slice.patch;
453+
cursor = slice.nextCursor ?? undefined;
454+
} while (cursor !== undefined);
455+
456+
expect(mockedExecute.mock.calls.map(([call]) => call.args[1])).toEqual([
457+
"projects/acme%2Fweb/merge_requests/7/diffs?per_page=30&page=1",
458+
"projects/acme%2Fweb/merge_requests/7/diffs?per_page=30&page=2",
459+
"projects/acme%2Fweb/merge_requests/7/diffs?per_page=30&page=3",
460+
]);
461+
assert.strictEqual(patch.match(/^diff --git /gm)?.length, 65);
462+
expect(patch).toContain("diff --git a/src/64.ts b/src/64.ts");
463+
}),
464+
);
465+
437466
it.effect("refuses a cursor it never handed out rather than reading it into a query", () =>
438467
Effect.gen(function* () {
439468
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;

‎packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ export type GitLabPullRequestCliError =
181181
/** GitLab's own ceiling on `per_page`, so a larger page has to be walked. */
182182
const MAX_PAGE_SIZE = 100;
183183
const COMMIT_PAGE_SIZE = 100;
184+
/**
185+
* GitLab 15.7 through 17.10 answer the merge request `/diffs` endpoint with a 500 for any
186+
* `per_page` above 30 (fixed in 17.11), so those pages stay at a size every version accepts.
187+
* A commit's own diff endpoint never had that bug and keeps the full page.
188+
*/
189+
const MERGE_REQUEST_DIFF_PAGE_SIZE = 30;
190+
const diffPageSize = (commit: string | undefined) =>
191+
commit === undefined ? MERGE_REQUEST_DIFF_PAGE_SIZE : MAX_PAGE_SIZE;
184192
const CONVERSATION_PAGES = 10;
185193
const DIFF_MAX_OUTPUT_BYTES = 8 * 1024 * 1024;
186194
const DIFF_TIMEOUT_MS = 60_000;
@@ -615,7 +623,7 @@ export const make = Effect.gen(function* () {
615623
? `merge_requests/${input.number}/diffs`
616624
: `repository/commits/${input.commit}/diff`
617625
}?${query([
618-
["per_page", String(MAX_PAGE_SIZE)],
626+
["per_page", String(diffPageSize(input.commit))],
619627
["page", String(input.page)],
620628
])}`,
621629
maxOutputBytes: DIFF_MAX_OUTPUT_BYTES,
@@ -673,7 +681,7 @@ export const make = Effect.gen(function* () {
673681
const patch = decoded.success.patch;
674682
// Counted before decoding, so a page whose files all failed to decode still moves on
675683
// rather than pointing the reader back at the page it just read.
676-
const morePages = !legacy && decoded.success.rawCount >= MAX_PAGE_SIZE;
684+
const morePages = !legacy && decoded.success.rawCount >= diffPageSize(input.commit);
677685
return Effect.succeed({
678686
// The slice ends on a newline, so a file GitLab gave a header and no hunks for does
679687
// not run into the first line of the next slice.

0 commit comments

Comments
 (0)