@devries-cameron20 / t3code-1 / commits / c82e1ba515
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(gitlab): review comments can cover a range of lines (#30535)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Antony committed Nov 11, 2026 at 06:51 UTC c82e1ba515c88922306a92a42c5ff1b0f686205f
3 files changed +189 -25
packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts
+43 -4
@@ -1383,6 +1383,21 @@ layer("GitLabPullRequestCli.layer", (it) => {
1383 ),
1384 ),
1385 );
1386 + // Line 12 is added after a removed line, so GitLab's old counter there is 11, not 0.
1387 + mockedExecute.mockReturnValueOnce(
1388 + Effect.succeed(
1389 + output(
1390 + JSON.stringify([
1391 + {
1392 + old_path: "src/a.ts",
1393 + new_path: "src/b.ts",
1394 + renamed_file: true,
1395 + diff: "@@ -8,4 +9,4 @@\n a\n b\n-c\n+d\n+e\n f\n-g\n",
1396 + },
1397 + ]),
1398 + ),
1399 + ),
1400 + );
1401 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
1402 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1403
@@ -1399,12 +1414,26 @@ layer("GitLabPullRequestCli.layer", (it) => {
1414 position: { kind: "deleted", oldLine: 4 },
1415 body: "why remove?",
1416 },
1417 + {
1418 + path: "src/b.ts",
1419 + oldPath: "src/a.ts",
1420 + startPosition: { kind: "context", oldLine: 9, newLine: 10, side: "right" },
1421 + position: { kind: "added", newLine: 12 },
1422 + body: "this block",
1423 + },
1424 + {
1425 + path: "src/b.ts",
1426 + startPosition: { kind: "added", newLine: 12 },
1427 + position: { kind: "added", newLine: 12 },
1428 + body: "one line",
1429 + },
1430 ],
1431 });
1432
1433 expect(argsOfCall(0)[1]).toContain("merge_requests/7");
1406 - expect(argsOfCall(1)[1]).toContain("/discussions");
1407 - expect(JSON.parse(callAt(1).stdin ?? "")).toEqual({
1434 + expect(argsOfCall(1)[1]).toContain("merge_requests/7/diffs");
1435 + expect(argsOfCall(2)[1]).toContain("/discussions");
1436 + expect(JSON.parse(callAt(2).stdin ?? "")).toEqual({
1437 body: "why remove?",
1438 position: {
1439 base_sha: "base",
@@ -1416,8 +1445,18 @@ layer("GitLabPullRequestCli.layer", (it) => {
1445 old_line: 4,
1446 },
1447 });
1419 - expect(argsOfCall(2)[1]).toContain("/notes");
1420 - expect(argsOfCall(3)[1]).toContain("/approve");
1448 + // sha1("src/b.ts")
1449 + const fileHash = "e55a8d6c5dd26e72538fd71fb7695dd829c8dab5";
1450 + expect(JSON.parse(callAt(3).stdin ?? "").position).toMatchObject({
1451 + new_line: 12,
1452 + line_range: {
1453 + start: { line_code: `${fileHash}_9_10`, type: null, old_line: 9, new_line: 10 },
1454 + end: { line_code: `${fileHash}_11_12`, type: "new", new_line: 12 },
1455 + },
1456 + });
1457 + expect(JSON.parse(callAt(4).stdin ?? "").position).not.toHaveProperty("line_range");
1458 + expect(argsOfCall(5)[1]).toContain("/notes");
1459 + expect(argsOfCall(6)[1]).toContain("/approve");
1460 }),
1461 );
1462
packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts
+145 -21
@@ -1,6 +1,7 @@
1 import { removeAgentCredits } from "@t3tools/source-control-core/server/mergeMessage";
2 import { isCommitSha } from "@t3tools/source-control-core/server/pullRequestUtils";
3 import * as Context from "effect/Context";
4 +import * as Digest from "effect/crypto/Digest";
5 import * as DateTime from "effect/DateTime";
6 import * as Effect from "effect/Effect";
7 import * as Layer from "effect/Layer";
@@ -8,6 +9,7 @@ import * as Option from "effect/Option";
9 import * as Result from "effect/Result";
10 import * as Schema from "effect/Schema";
11 import { resolvePullRequestAuthorFilter, VcsProcessExitError } from "@t3tools/contracts";
12 +import { quoteGitPatchPath } from "@t3tools/shared/gitPatchPath";
13 import type {
14 PullRequestAction,
15 PullRequestActor,
@@ -662,6 +664,90 @@ function gitLabReviewPositionLines(
664 }
665 }
666
667 +function gitLabDiffHeader(oldPath: string, newPath: string): string {
668 + return `diff --git ${quoteGitPatchPath(`a/${oldPath}`)} ${quoteGitPatchPath(`b/${newPath}`)}\n`;
669 +}
670 +
671 +/**
672 + * The old and new line counters GitLab's diff parser holds at a changed line. An added line
673 + * keeps the old counter where it stands, and a removed line the new one, so neither side of a
674 + * changed line's `line_code` is known without walking its hunk.
675 + */
676 +function gitLabLineCounters(
677 + patch: string,
678 + header: string,
679 + position: PullRequestReviewPosition,
680 +): { readonly old: number; readonly new: number } | undefined {
681 + if (position.kind === "context") return { old: position.oldLine, new: position.newLine };
682 + const start = `\n${patch}`.indexOf(`\n${header}`);
683 + if (start === -1) return undefined;
684 + const end = patch.indexOf("\ndiff --git ", start + header.length - 1);
685 + const section = patch.slice(start, end === -1 ? undefined : end);
686 + const firstHunk = section.indexOf("\n@@ ");
687 + if (firstHunk === -1) return undefined;
688 + let oldLine = 0;
689 + let newLine = 0;
690 + for (const line of section.slice(firstHunk + 1).split("\n")) {
691 + const hunk = /^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/u.exec(line);
692 + if (hunk) {
693 + oldLine = Number(hunk[1]);
694 + newLine = Number(hunk[2]);
695 + } else if (line.startsWith("+")) {
696 + if (position.kind === "added" && newLine === position.newLine) {
697 + return { old: oldLine, new: newLine };
698 + }
699 + newLine += 1;
700 + } else if (line.startsWith("-")) {
701 + if (position.kind === "deleted" && oldLine === position.oldLine) {
702 + return { old: oldLine, new: newLine };
703 + }
704 + oldLine += 1;
705 + } else if (line.startsWith(" ")) {
706 + oldLine += 1;
707 + newLine += 1;
708 + }
709 + }
710 + return undefined;
711 +}
712 +
713 +/**
714 + * One end of a ranged comment. GitLab labels the range from `old_line`/`new_line` and `type`,
715 + * the way its own UI sends them (`type` is null on a context line), and finds the lines to
716 + * highlight by `line_code`, which needs both counters from the diff.
717 + */
718 +function gitLabLineRangeEnd(
719 + fileHash: string,
720 + position: PullRequestReviewPosition,
721 + counters: { readonly old: number; readonly new: number } | undefined,
722 +) {
723 + const lines = gitLabReviewPositionLines(position);
724 + const oldLine = counters?.old ?? ("old_line" in lines ? lines.old_line : 0);
725 + const newLine = counters?.new ?? ("new_line" in lines ? lines.new_line : 0);
726 + return {
727 + line_code: `${fileHash}_${oldLine}_${newLine}`,
728 + type: position.kind === "added" ? "new" : position.kind === "deleted" ? "old" : null,
729 + ...lines,
730 + };
731 +}
732 +
733 +const gitLabLineRange = Effect.fn("GitLabPullRequestCli.lineRange")(function* (
734 + patch: string,
735 + comment: PullRequestReviewCommentDraft,
736 +) {
737 + const start = comment.startPosition;
738 + if (start === undefined) return {};
739 + const digest = yield* Digest.Digest;
740 + const fileHash = yield* digest.digestHex(Digest.sha1, new TextEncoder().encode(comment.path));
741 + const header = gitLabDiffHeader(comment.oldPath ?? comment.path, comment.path);
742 + const first = gitLabLineRangeEnd(fileHash, start, gitLabLineCounters(patch, header, start));
743 + const last = gitLabLineRangeEnd(
744 + fileHash,
745 + comment.position,
746 + gitLabLineCounters(patch, header, comment.position),
747 + );
748 + return first.line_code === last.line_code ? {} : { line_range: { start: first, end: last } };
749 +}, Effect.provide(Digest.layerWebCrypto));
750 +
751 function stateParam(state: PullRequestListState): string {
752 return state === "open" ? "opened" : state;
753 }
@@ -1128,6 +1214,28 @@ export const make = Effect.gen(function* () {
1214 }),
1215 );
1216
1217 + /** Reads diff pages only until every header is found, for the files ranged comments sit on. */
1218 + const patchCovering = (
1219 + input: {
1220 + readonly cwd: string;
1221 + readonly host?: string | undefined;
1222 + readonly repository: string;
1223 + readonly number: number;
1224 + },
1225 + headers: ReadonlyArray<string>,
1226 + page: number,
1227 + collected: string,
1228 + ): Effect.Effect<string, GitLabPullRequestCliError> =>
1229 + headers.every((header) => `\n${collected}`.includes(`\n${header}`))
1230 + ? Effect.succeed(collected)
1231 + : diffPage({ ...input, page }).pipe(
1232 + Effect.flatMap((slice) =>
1233 + slice.nextCursor === null
1234 + ? Effect.succeed(collected + slice.patch)
1235 + : patchCovering(input, headers, page + 1, collected + slice.patch),
1236 + ),
1237 + );
1238 +
1239 const notesPage = (input: {
1240 readonly cwd: string;
1241 readonly host?: string | undefined;
@@ -2575,30 +2683,46 @@ export const make = Effect.gen(function* () {
2683 }
2684 if (input.comments.length > 0) {
2685 const refs = yield* getDiffRefs(input);
2686 + const rangedFileHeaders = input.comments.flatMap((comment) =>
2687 + comment.startPosition !== undefined &&
2688 + (comment.startPosition.kind !== "context" || comment.position.kind !== "context")
2689 + ? [gitLabDiffHeader(comment.oldPath ?? comment.path, comment.path)]
2690 + : [],
2691 + );
2692 + // Line codes only steer GitLab's highlight of a range, so an unreadable diff falls back
2693 + // to codes without the other side's counter rather than blocking the review.
2694 + const patch = yield* patchCovering(input, rangedFileHeaders, 1, "").pipe(
2695 + Effect.orElseSucceed(() => ""),
2696 + );
2697 yield* Effect.forEach(
2698 input.comments,
2699 (comment) =>
2581 - api({
2582 - cwd: input.cwd,
2583 - host: input.host,
2584 - path: `${mergeRequest}/discussions`,
2585 - method: "POST",
2586 - stdin: JSON.stringify({
2587 - body: comment.body,
2588 - position: {
2589 - base_sha: refs.baseSha,
2590 - head_sha: refs.headSha,
2591 - start_sha: refs.startSha,
2592 - position_type: "text",
2593 - // Both paths are sent because GitLab resolves a position against both
2594 - // sides of the diff. They differ only for a renamed file, which is why the
2595 - // draft carries the name the file had before the change.
2596 - old_path: comment.oldPath ?? comment.path,
2597 - new_path: comment.path,
2598 - ...gitLabReviewPositionLines(comment.position),
2599 - },
2600 - }),
2601 - }),
2700 + gitLabLineRange(patch, comment).pipe(
2701 + Effect.flatMap((lineRange) =>
2702 + api({
2703 + cwd: input.cwd,
2704 + host: input.host,
2705 + path: `${mergeRequest}/discussions`,
2706 + method: "POST",
2707 + stdin: JSON.stringify({
2708 + body: comment.body,
2709 + position: {
2710 + base_sha: refs.baseSha,
2711 + head_sha: refs.headSha,
2712 + start_sha: refs.startSha,
2713 + position_type: "text",
2714 + // Both paths are sent because GitLab resolves a position against both
2715 + // sides of the diff. They differ only for a renamed file, which is why the
2716 + // draft carries the name the file had before the change.
2717 + old_path: comment.oldPath ?? comment.path,
2718 + new_path: comment.path,
2719 + ...gitLabReviewPositionLines(comment.position),
2720 + ...lineRange,
2721 + },
2722 + }),
2723 + }),
2724 + ),
2725 + ),
2726 { discard: true },
2727 );
2728 }
packages/source-control-gitlab/src/server/GitLabPullRequestProvider.ts
+1
@@ -38,6 +38,7 @@ const CAPABILITIES: PullRequestCapabilities = {
38 viewedFiles: "environment",
39 review: {
40 inlineComment: true,
41 + rangeComment: true,
42 reply: true,
43 resolve: true,
44 verdicts: ["comment", "approve", "request-changes"],