Skip to content

Commit b64c6fe

Browse files
agomespclaude
andcommitted
fix(web): keep review comments inside the pull request's hunks
An expanded pull request file shows the lines between hunks, and a comment could be anchored there. GitHub refuses the whole review over one such comment ("Line could not be resolved"). A context line outside every hunk no longer resolves to a review position, so it cannot start a comment. Part of #12549: surfacing GitHub's validation error is left for a follow-up. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent dfbb11b commit b64c6fe

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

‎apps/web/src/reviewCommentContext.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { hydratePartialDiff } from "@pierre/diffs";
12
import { parsePatchFiles } from "@pierre/diffs/utils/parsePatchFiles";
23
import { describe, expect, it } from "vite-plus/test";
34

@@ -6,6 +7,7 @@ import {
67
buildFileReviewComment,
78
formatReviewCommentFence,
89
inferReviewCommentFenceLanguage,
10+
resolveDiffReviewPosition,
911
restoreDiffReviewCommentRange,
1012
} from "./reviewCommentContext";
1113

@@ -112,4 +114,56 @@ describe("review comment context parsing", () => {
112114
endSide: "additions",
113115
});
114116
});
117+
118+
it("anchors review comments only on lines inside the host's hunks", () => {
119+
const oldLines = Array.from({ length: 30 }, (_, index) => `line ${index + 1}\n`);
120+
const newLines = [
121+
...oldLines.slice(0, 6),
122+
...Array.from({ length: 10 }, (_, index) => `new ${index + 1}\n`),
123+
...oldLines.slice(6),
124+
].map((line) => (line === "line 25\n" ? "line twenty-five\n" : line));
125+
// What the host returns: new lines 4 to 19 and 32 to 38. The insertion shifts the new side,
126+
// so new line 25 (old line 15) sits between the hunks while old line 25 is inside one.
127+
const fileDiff = parsePatchFiles(
128+
[
129+
"diff --git a/src/app.ts b/src/app.ts",
130+
"--- a/src/app.ts",
131+
"+++ b/src/app.ts",
132+
"@@ -4,6 +4,16 @@",
133+
" line 4",
134+
" line 5",
135+
" line 6",
136+
...Array.from({ length: 10 }, (_, index) => `+new ${index + 1}`),
137+
" line 7",
138+
" line 8",
139+
" line 9",
140+
"@@ -22,7 +32,7 @@",
141+
" line 22",
142+
" line 23",
143+
" line 24",
144+
"-line 25",
145+
"+line twenty-five",
146+
" line 26",
147+
" line 27",
148+
" line 28",
149+
].join("\n"),
150+
"review-position-hunks",
151+
)[0]!.files[0]!;
152+
// Expanding the file loads both sides whole, so the gaps around the hunks become lines
153+
// the viewer can select but the host will not anchor a comment to.
154+
hydratePartialDiff("merge", fileDiff, {
155+
oldFile: { name: "src/app.ts", contents: oldLines.join("") },
156+
newFile: { name: "src/app.ts", contents: newLines.join("") },
157+
});
158+
expect(fileDiff.isPartial).toBe(false);
159+
160+
expect(resolveDiffReviewPosition(fileDiff, 1, "additions")).toBeNull();
161+
expect(resolveDiffReviewPosition(fileDiff, 25, "additions")).toBeNull();
162+
expect(resolveDiffReviewPosition(fileDiff, 33, "additions")).toEqual({
163+
kind: "context",
164+
oldLine: 23,
165+
newLine: 33,
166+
side: "right",
167+
});
168+
});
115169
});

‎apps/web/src/reviewCommentContext.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { FileDiffMetadata, SelectedLineRange, SelectionSide } from "@pierre
22
import { PullRequestContextMetadata, type PullRequestReviewPosition } from "@t3tools/contracts";
33
import * as Schema from "effect/Schema";
44

5+
import { isLineInFileDiff } from "./components/pullRequest/pullRequestDiff.logic";
6+
57
const ReviewCommentSelectionSchema = Schema.Struct({
68
start: Schema.Number,
79
side: Schema.Literals(["additions", "deletions"]),
@@ -331,7 +333,10 @@ function findDiffReviewLineIndex(
331333
: findOnSide(selectedSide === "left" ? "right" : "left");
332334
}
333335

334-
/** Resolve the host-facing coordinates of a line selected in the diff viewer. */
336+
/**
337+
* Resolve the host-facing coordinates of a line selected in the diff viewer, or null when the
338+
* host cannot anchor a comment there.
339+
*/
335340
export function resolveDiffReviewPosition(
336341
fileDiff: FileDiffMetadata,
337342
lineNumber: number,
@@ -351,7 +356,11 @@ export function resolveDiffReviewPosition(
351356
case "delete":
352357
return line.oldLineNumber === null ? null : { kind: "deleted", oldLine: line.oldLineNumber };
353358
case "context":
354-
return line.oldLineNumber === null || line.newLineNumber === null
359+
// An expanded file also shows the gaps between hunks, and GitHub rejects a whole review
360+
// over one comment anchored there.
361+
return line.oldLineNumber === null ||
362+
line.newLineNumber === null ||
363+
!isLineInFileDiff(fileDiff, "right", line.newLineNumber)
355364
? null
356365
: {
357366
kind: "context",

0 commit comments

Comments
 (0)