diff --git a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts index a0ef19667743..4d8ed5b95d0e 100644 --- a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts +++ b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts @@ -1129,6 +1129,90 @@ layer("GitLabPullRequestCli.layer", (it) => { }), ); + it.effect("counts a file GitLab withheld as too large and offers to read it alone", () => + Effect.gen(function* () { + mockedExecute + .mockReturnValueOnce( + Effect.succeed( + output( + JSON.stringify([ + { old_path: "src/a.ts", new_path: "src/a.ts", diff: "@@ -1 +1 @@\n-a\n+b\n" }, + { old_path: "src/huge.txt", new_path: "src/huge.txt", diff: "", too_large: true }, + ]), + ), + ), + ) + .mockReturnValueOnce( + Effect.succeed( + output( + JSON.stringify({ + data: { + project: { + mergeRequest: { + diffStatsSummary: { fileCount: 2 }, + diffStats: [ + { path: "src/a.ts", additions: 1, deletions: 1 }, + { path: "src/huge.txt", additions: 20000, deletions: 0 }, + ], + }, + }, + }, + }), + ), + ), + ); + const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; + + const diff = yield* cli.getMergeRequestDiff({ cwd: "/w", repository: "acme/web", number: 7 }); + + expect(diff.omittedFileStats).toEqual([ + { path: "src/huge.txt", additions: 20000, deletions: 0 }, + ]); + assert.isTrue(diff.withheldFilesReadable); + }), + ); + + it.effect("reads one withheld file's section from the merge request's raw diff", () => + Effect.gen(function* () { + mockedExecute.mockReturnValueOnce( + Effect.succeed( + output( + [ + "diff --git a/src/a.ts b/src/a.ts", + "--- a/src/a.ts", + "+++ b/src/a.ts", + "@@ -1 +1 @@", + "-a", + "+b", + "diff --git a/src/huge.txt b/src/huge.txt", + "new file mode 100644", + "--- /dev/null", + "+++ b/src/huge.txt", + "@@ -0,0 +1,2 @@", + "+one", + "+two", + "", + ].join("\n"), + ), + ), + ); + const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli; + + const diff = yield* cli.getMergeRequestDiff({ + cwd: "/w", + repository: "acme/web", + number: 7, + path: "src/huge.txt", + }); + + expect(argsOfCall(0)[1]).toBe("projects/acme%2Fweb/merge_requests/7/raw_diffs"); + assert.strictEqual( + diff.patch, + "diff --git a/src/huge.txt b/src/huge.txt\nnew file mode 100644\n--- /dev/null\n+++ b/src/huge.txt\n@@ -0,0 +1,2 @@\n+one\n+two\n", + ); + }), + ); + it.effect( "pages a merge request diff 30 files at a time, which GitLab 15.7 to 17.10 accept", () => diff --git a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts index b8ad433c96df..588e6a084fd7 100644 --- a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts +++ b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts @@ -1,5 +1,9 @@ import { removeAgentCredits } from "@t3tools/source-control-core/server/mergeMessage"; import { isCommitSha } from "@t3tools/source-control-core/server/pullRequestUtils"; +import { + diffFileSection, + truncateLongDiffLines, +} from "@t3tools/source-control-core/server/diffFileRevisions"; import * as Context from "effect/Context"; import * as Digest from "effect/crypto/Digest"; import * as DateTime from "effect/DateTime"; @@ -25,6 +29,7 @@ import type { PullRequestListState, PullRequestMergeCapabilities, PullRequestMergeMethod, + PullRequestOmittedFileStat, PullRequestReaction, PullRequestReactionContent, PullRequestReviewCommentDraft, @@ -344,6 +349,9 @@ const diffPageSize = (commit: string | undefined) => const CONVERSATION_PAGES = 10; const DIFF_MAX_OUTPUT_BYTES = 8 * 1024 * 1024; const DIFF_TIMEOUT_MS = 60_000; +const WITHHELD_FILE_DIFF_MAX_OUTPUT_BYTES = 32 * 1024 * 1024; +// Withheld files are often generated or minified, where one line can be megabytes. +const WITHHELD_FILE_MAX_LINE_LENGTH = 1_000; const isVcsProcessExitError = Schema.is(VcsProcessExitError); const DIFF_FILE_MAX_OUTPUT_BYTES = 1024 * 1024; // REST list rows carry full descriptions, so a page can pass the 1 MB default output cap. @@ -370,6 +378,8 @@ export interface GitLabMergeRequestDiffSlice { readonly patch: string; readonly truncated: boolean; readonly nextCursor: string | null; + readonly omittedFileStats?: ReadonlyArray; + readonly withheldFilesReadable?: boolean; } export class GitLabPullRequestCli extends Context.Service< @@ -482,6 +492,8 @@ export class GitLabPullRequestCli extends Context.Service< readonly number: number; readonly cursor?: string | undefined; readonly commit?: string | undefined; + /** Reads one file a page withheld as too large; ignores `cursor`. */ + readonly path?: string | undefined; }) => Effect.Effect; readonly getMergeRequestDiffFileContents: (input: { @@ -1174,7 +1186,13 @@ export const make = Effect.gen(function* () { readonly number: number; readonly page: number; readonly commit?: string | undefined; - }): Effect.Effect => + }): Effect.Effect< + GitLabMergeRequestDiffSlice & { + readonly withheldPaths: ReadonlyArray; + readonly legacy: boolean; + }, + GitLabPullRequestCliError + > => api({ cwd: input.cwd, host: input.host, @@ -1246,6 +1264,8 @@ export const make = Effect.gen(function* () { patch: patch.length === 0 ? patch : patch.replace(/\n?$/, "\n"), truncated: decoded.success.truncated, nextCursor: morePages ? String(input.page + 1) : null, + withheldPaths: decoded.success.withheldPaths, + legacy, }); }), ); @@ -1847,6 +1867,67 @@ export const make = Effect.gen(function* () { ): Effect.Effect => read.pipe(Effect.catchIf(isOptionalReadFailure, () => Effect.succeed(fallback))); + const mergeRequestFileStats = (input: { + readonly cwd: string; + readonly host?: string | undefined; + readonly repository: string; + readonly number: number; + }) => + graphqlRead({ + cwd: input.cwd, + host: input.host, + operation: "getMergeRequestFileStats", + query: MERGE_REQUEST_FILE_STATS_GRAPHQL_QUERY, + variables: { fullPath: input.repository, iid: String(input.number) }, + decode: decodeMergeRequestFileStatsJson, + }); + + // GitLab has no per-file diff, so a withheld file is cut from the whole raw diff here and + // only its section reaches the client. `/raw_diffs` needs GitLab 17.9 or newer. + const withheldFileDiff = (input: { + readonly cwd: string; + readonly host?: string | undefined; + readonly repository: string; + readonly number: number; + readonly commit?: string | undefined; + readonly path: string; + }): Effect.Effect => { + const readError = (message: string) => + new GitLabMergeRequestReadError({ + command: "glab", + cwd: input.cwd, + operation: "getMergeRequestDiff", + cause: new Error(message), + }); + if (input.commit !== undefined) { + return Effect.fail(readError("A commit's withheld files cannot be read one at a time.")); + } + return api({ + cwd: input.cwd, + host: input.host, + path: `projects/${projectPath(input.repository)}/merge_requests/${input.number}/raw_diffs`, + maxOutputBytes: WITHHELD_FILE_DIFF_MAX_OUTPUT_BYTES, + timeoutMs: DIFF_TIMEOUT_MS, + }).pipe( + Effect.flatMap((result) => { + const section = result.stdoutTruncated ? null : diffFileSection(result.stdout, input.path); + return section === null + ? Effect.fail( + readError( + result.stdoutTruncated + ? `The diff for ${input.path} is too large to load.` + : `GitLab's diff does not include ${input.path}.`, + ), + ) + : Effect.succeed({ + patch: truncateLongDiffLines(section, WITHHELD_FILE_MAX_LINE_LENGTH), + truncated: false, + nextCursor: null, + }); + }), + ); + }; + const graphqlMutation = (input: { readonly cwd: string; readonly host?: string | undefined; @@ -2305,15 +2386,8 @@ export const make = Effect.gen(function* () { listMergeRequestStats, - getMergeRequestFileStats: Effect.fn("GitLabPullRequestCli.getMergeRequestFileStats")((input) => - graphqlRead({ - cwd: input.cwd, - host: input.host, - operation: "getMergeRequestFileStats", - query: MERGE_REQUEST_FILE_STATS_GRAPHQL_QUERY, - variables: { fullPath: input.repository, iid: String(input.number) }, - decode: decodeMergeRequestFileStatsJson, - }), + getMergeRequestFileStats: Effect.fn("GitLabPullRequestCli.getMergeRequestFileStats")( + mergeRequestFileStats, ), getMergeRequestWatchFingerprint: Effect.fn( @@ -2426,13 +2500,33 @@ export const make = Effect.gen(function* () { number: input.number, ...(input.commit === undefined ? {} : { commit: input.commit }), }; - if (input.cursor === undefined) { - return yield* diffPage({ ...target, page: 1 }); + if (input.path !== undefined) { + return yield* withheldFileDiff({ ...target, path: input.path }); } - const page = diffCursorPage(input.cursor); + const page = input.cursor === undefined ? 1 : diffCursorPage(input.cursor); if (page === null) return yield* new GitLabDiffCursorError({ command: "glab", cwd: input.cwd }); - return yield* diffPage({ ...target, page }); + const { withheldPaths, legacy, ...slice } = yield* diffPage({ ...target, page }); + // A commit's diff has no per-file counts or raw diff to read a withheld file from. + if (input.commit !== undefined) return slice; + const readable = page === 1 && !legacy ? { withheldFilesReadable: true } : {}; + if (withheldPaths.length === 0) return { ...slice, ...readable }; + const stats = yield* orWhenUnanswered(mergeRequestFileStats(target), { + files: [], + truncated: true, + }); + const statsByPath = new Map(stats.files.map((file) => [file.path, file])); + const omittedFileStats = withheldPaths.flatMap((path) => { + const stat = statsByPath.get(path); + return stat === undefined + ? [] + : [{ path, additions: stat.additions, deletions: stat.deletions }]; + }); + return { + ...slice, + ...readable, + ...(omittedFileStats.length === 0 ? {} : { omittedFileStats }), + }; }), getMergeRequestDiffFileContents: Effect.fn( diff --git a/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.ts b/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.ts index 1341205793bf..9a3881b249f8 100644 --- a/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.ts +++ b/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.ts @@ -47,6 +47,7 @@ const CAPABILITIES: PullRequestCapabilities = { reviewers: { request: true, listCandidates: true, searchCandidates: true }, labels: true, edit: { changeRequest: true, comment: true, maintainerEdits: true }, + withheldFileDiff: true, }; const UNAVAILABLE_REQUIREMENT = { diff --git a/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.test.ts b/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.test.ts index 6905daf47d72..22a21e242b28 100644 --- a/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.test.ts +++ b/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.test.ts @@ -681,6 +681,7 @@ describe("decodeMergeRequestDiffsJson", () => { expect(result.truncated).toBe(true); expect(result.patch).toContain("diff --git a/big.bin b/big.bin"); + expect(result.withheldPaths).toEqual(["big.bin"]); }); it("reports how many files GitLab returned, so the caller can page", () => { diff --git a/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts b/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts index daf00998711c..995ae7493926 100644 --- a/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts +++ b/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts @@ -1014,6 +1014,8 @@ export interface GitLabMergeRequestPatch { readonly patch: string; readonly truncated: boolean; readonly rawCount: number; + /** Files GitLab sent without their diff because they are too large or collapsed. */ + readonly withheldPaths: ReadonlyArray; } export function decodeMergeRequestDiffsJson( @@ -1044,6 +1046,7 @@ export function decodeMergeRequestChangesJson( function mergeRequestPatch(entries: ReadonlyArray): GitLabMergeRequestPatch { const sections: string[] = []; + const withheldPaths: string[] = []; let truncated = false; for (const entry of entries) { const file = decodeDiffEntry(entry); @@ -1053,8 +1056,9 @@ function mergeRequestPatch(entries: ReadonlyArray): GitLabMergeRequestP } const value = file.value; const hunks = value.diff ?? ""; - if (hunks.length === 0) { - truncated = truncated || value.too_large === true || value.collapsed === true; + if (hunks.length === 0 && (value.too_large === true || value.collapsed === true)) { + truncated = true; + withheldPaths.push(value.new_path); } const { from, to } = diffHeaderPaths(value); const header = [ @@ -1078,6 +1082,7 @@ function mergeRequestPatch(entries: ReadonlyArray): GitLabMergeRequestP patch: sections.join("\n"), truncated, rawCount: entries.length, + withheldPaths, }; }