Skip to content

Commit d94fbda

Browse files
authored
fix(gitlab): submit review comments on context lines (pingdotgg#6348)
1 parent b7dbbba commit d94fbda

13 files changed

Lines changed: 338 additions & 54 deletions

‎apps/server/src/pullRequest/BitbucketPullRequestApi.test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,13 @@ layer("BitbucketPullRequestApi.layer", (it) => {
776776
number: 7,
777777
verdict: "request-changes",
778778
body: "Two things.",
779-
comments: [{ path: "src/a.ts", line: 12, side: "left", body: "why remove?" }],
779+
comments: [
780+
{
781+
path: "src/a.ts",
782+
position: { kind: "deleted", oldLine: 12 },
783+
body: "why remove?",
784+
},
785+
],
780786
});
781787

782788
expect(callAt(0).url).toContain("/pullrequests/7/comments");

‎apps/server/src/pullRequest/BitbucketPullRequestApi.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
PullRequestMergeMethod,
1313
PullRequestMergeability,
1414
PullRequestReviewCommentDraft,
15+
PullRequestReviewPosition,
1516
PullRequestReviewThread,
1617
PullRequestReviewVerdict,
1718
PullRequestReviewerCandidateList,
@@ -364,6 +365,19 @@ function mergeStrategy(method: PullRequestMergeMethod | undefined): string {
364365
}
365366
}
366367

368+
function bitbucketReviewPosition(
369+
position: PullRequestReviewPosition,
370+
): { readonly from: number } | { readonly to: number } {
371+
switch (position.kind) {
372+
case "added":
373+
return { to: position.newLine };
374+
case "deleted":
375+
return { from: position.oldLine };
376+
case "context":
377+
return position.side === "left" ? { from: position.oldLine } : { to: position.newLine };
378+
}
379+
}
380+
367381
export const make = Effect.gen(function* () {
368382
const bitbucket = yield* BitbucketApi.BitbucketApi;
369383

@@ -794,7 +808,7 @@ export const make = Effect.gen(function* () {
794808
content: { raw: comment.body },
795809
inline: {
796810
path: comment.path,
797-
...(comment.side === "left" ? { from: comment.line } : { to: comment.line }),
811+
...bitbucketReviewPosition(comment.position),
798812
},
799813
}),
800814
}),

‎apps/server/src/pullRequest/GitHubPullRequestCli.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1624,7 +1624,7 @@ layer("GitHubPullRequestCli.layer", (it) => {
16241624
number: 7,
16251625
verdict: "approve",
16261626
body: "Looks right.",
1627-
comments: [{ path: "src/a.ts", line: 4, side: "right", body: "nit" }],
1627+
comments: [{ path: "src/a.ts", position: { kind: "added", newLine: 4 }, body: "nit" }],
16281628
});
16291629

16301630
expect(callAt(0).args).toEqual([

‎apps/server/src/pullRequest/GitLabPullRequestCli.test.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,12 @@ layer("GitLabPullRequestCli.layer", (it) => {
10181018
verdict: "approve",
10191019
body: "Looks right.",
10201020
comments: [
1021-
{ path: "src/b.ts", oldPath: "src/a.ts", line: 4, side: "left", body: "why remove?" },
1021+
{
1022+
path: "src/b.ts",
1023+
oldPath: "src/a.ts",
1024+
position: { kind: "deleted", oldLine: 4 },
1025+
body: "why remove?",
1026+
},
10221027
],
10231028
});
10241029

@@ -1197,7 +1202,7 @@ layer("GitLabPullRequestCli.layer", (it) => {
11971202
number: 7,
11981203
verdict: "comment",
11991204
body: "",
1200-
comments: [{ path: "src/a.ts", line: 4, side: "right", body: "nit" }],
1205+
comments: [{ path: "src/a.ts", position: { kind: "added", newLine: 4 }, body: "nit" }],
12011206
}),
12021207
);
12031208

‎apps/server/src/pullRequest/GitLabPullRequestCli.ts‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
PullRequestReaction,
1515
PullRequestReactionContent,
1616
PullRequestReviewCommentDraft,
17+
PullRequestReviewPosition,
1718
PullRequestReviewThread,
1819
PullRequestReviewVerdict,
1920
PullRequestReviewerCandidateList,
@@ -400,6 +401,22 @@ function projectPath(repository: string): string {
400401
return encodeURIComponent(repository.trim());
401402
}
402403

404+
function gitLabReviewPositionLines(
405+
position: PullRequestReviewPosition,
406+
):
407+
| { readonly new_line: number }
408+
| { readonly old_line: number }
409+
| { readonly old_line: number; readonly new_line: number } {
410+
switch (position.kind) {
411+
case "added":
412+
return { new_line: position.newLine };
413+
case "deleted":
414+
return { old_line: position.oldLine };
415+
case "context":
416+
return { old_line: position.oldLine, new_line: position.newLine };
417+
}
418+
}
419+
403420
function stateParam(state: PullRequestListState): string {
404421
// GitLab's `closed` already excludes merged merge requests, so no extra filter is needed,
405422
// and it spans every state under `all`.
@@ -1324,9 +1341,7 @@ export const make = Effect.gen(function* () {
13241341
// draft carries the name the file had before the change.
13251342
old_path: comment.oldPath ?? comment.path,
13261343
new_path: comment.path,
1327-
...(comment.side === "left"
1328-
? { old_line: comment.line }
1329-
: { new_line: comment.line }),
1344+
...gitLabReviewPositionLines(comment.position),
13301345
},
13311346
}),
13321347
}),

‎apps/server/src/pullRequest/PullRequestService.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1534,7 +1534,7 @@ it.effect("refuses line comments on a host that takes only a summary", () =>
15341534
number: 1,
15351535
verdict: "comment",
15361536
body: "",
1537-
comments: [{ path: "src/a.ts", line: 1, side: "right", body: "nit" }],
1537+
comments: [{ path: "src/a.ts", position: { kind: "added", newLine: 1 }, body: "nit" }],
15381538
}),
15391539
);
15401540

‎apps/server/src/pullRequest/gitHubPullRequestJson.test.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,8 +1147,16 @@ describe("review submission payload", () => {
11471147
verdict: "request-changes",
11481148
body: "Two things.",
11491149
comments: [
1150-
{ path: "src/a.ts", line: 12, side: "right", body: "rename this" },
1151-
{ path: "src/b.ts", line: 3, side: "left", body: "why remove?" },
1150+
{
1151+
path: "src/a.ts",
1152+
position: { kind: "added", newLine: 12 },
1153+
body: "rename this",
1154+
},
1155+
{
1156+
path: "src/b.ts",
1157+
position: { kind: "deleted", oldLine: 3 },
1158+
body: "why remove?",
1159+
},
11521160
],
11531161
}),
11541162
) as Record<string, unknown>;

‎apps/server/src/pullRequest/gitHubPullRequestJson.ts‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
PullRequestReactionContent,
1818
PullRequestReviewCommentDraft,
1919
PullRequestReviewDecision,
20+
PullRequestReviewPosition,
2021
PullRequestReviewThread,
2122
PullRequestReviewVerdict,
2223
PullRequestReviewerCandidate,
@@ -933,6 +934,22 @@ export const REVIEW_DISMISSALS_GRAPHQL_QUERY = `query($owner: String!, $name: St
933934
}
934935
}`;
935936

937+
function gitHubReviewPosition(position: PullRequestReviewPosition): {
938+
readonly line: number;
939+
readonly side: "LEFT" | "RIGHT";
940+
} {
941+
switch (position.kind) {
942+
case "added":
943+
return { line: position.newLine, side: "RIGHT" };
944+
case "deleted":
945+
return { line: position.oldLine, side: "LEFT" };
946+
case "context":
947+
return position.side === "left"
948+
? { line: position.oldLine, side: "LEFT" }
949+
: { line: position.newLine, side: "RIGHT" };
950+
}
951+
}
952+
936953
/** The whole review as one request body, which is how GitHub keeps it invisible until sent. */
937954
export function buildReviewSubmissionJson(input: {
938955
readonly verdict: PullRequestReviewVerdict;
@@ -944,8 +961,7 @@ export function buildReviewSubmissionJson(input: {
944961
body: input.body,
945962
comments: input.comments.map((comment) => ({
946963
path: comment.path,
947-
line: comment.line,
948-
side: comment.side === "left" ? ("LEFT" as const) : ("RIGHT" as const),
964+
...gitHubReviewPosition(comment.position),
949965
body: comment.body,
950966
})),
951967
});

‎apps/web/src/components/pullRequest/PullRequestCodeTab.tsx‎

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
PullRequestDiffSide,
77
PullRequestOmittedFileStat,
88
PullRequestRef,
9+
PullRequestReviewPosition,
910
PullRequestReviewThread,
1011
} from "@t3tools/contracts";
1112
import {
@@ -43,7 +44,11 @@ import {
4344
} from "~/lib/diffRendering";
4445
import { cn } from "~/lib/utils";
4546
import { createPullRequestDiffFileContentsLoader } from "~/lib/diffFileContents";
46-
import { buildDiffReviewComment, type ReviewCommentContext } from "~/reviewCommentContext";
47+
import {
48+
buildDiffReviewComment,
49+
resolveDiffReviewPosition,
50+
type ReviewCommentContext,
51+
} from "~/reviewCommentContext";
4752
import { pullRequestEnvironment } from "~/state/pullRequests";
4853
import { useEnvironmentQuery } from "~/state/query";
4954
import { useAtomCommand } from "~/state/use-atom-command";
@@ -129,8 +134,7 @@ interface DraftAnchor {
129134
readonly path: string;
130135
/** What the file was called before the change, for the hosts that resolve a position by both. */
131136
readonly oldPath: string | null;
132-
readonly line: number;
133-
readonly side: PullRequestDiffSide;
137+
readonly position: PullRequestReviewPosition;
134138
/** The whole selection, which the comment collapses to one line but a question keeps. */
135139
readonly range: SelectedLineRange;
136140
}
@@ -148,8 +152,21 @@ function toViewerSide(side: PullRequestDiffSide) {
148152
return side === "left" ? ("deletions" as const) : ("additions" as const);
149153
}
150154

151-
function fromViewerSide(side: string | undefined): PullRequestDiffSide {
152-
return side === "deletions" ? "left" : "right";
155+
function getReviewPositionAnchor(position: PullRequestReviewPosition): {
156+
line: number;
157+
side: PullRequestDiffSide;
158+
} {
159+
switch (position.kind) {
160+
case "added":
161+
return { line: position.newLine, side: "right" };
162+
case "deleted":
163+
return { line: position.oldLine, side: "left" };
164+
case "context":
165+
return {
166+
line: position.side === "left" ? position.oldLine : position.newLine,
167+
side: position.side,
168+
};
169+
}
153170
}
154171

155172
/**
@@ -442,10 +459,14 @@ export function PullRequestCodeTab({
442459
if (commit === null) {
443460
for (const comment of pendingComments) {
444461
if (comment.path !== path) continue;
445-
groupAt(comment.side, comment.line).pending.push(comment);
462+
const anchor = getReviewPositionAnchor(comment.position);
463+
groupAt(anchor.side, anchor.line).pending.push(comment);
446464
}
447465
}
448-
if (draft?.fileKey === fileKey) groupAt(draft.side, draft.line).draft = true;
466+
if (draft?.fileKey === fileKey) {
467+
const anchor = getReviewPositionAnchor(draft.position);
468+
groupAt(anchor.side, anchor.line).draft = true;
469+
}
449470

450471
const collapsed = isFileDiffCollapsed(fileKey, foldOverride, toggledFiles);
451472

@@ -594,12 +615,13 @@ export function PullRequestCodeTab({
594615
// that silently lost its first line on the other hosts would be worse than one line.
595616
const path = resolveFileDiffPath(file);
596617
const previousPath = resolveFileDiffPreviousPath(file);
618+
const position = resolveDiffReviewPosition(file, range.end, range.endSide ?? range.side);
619+
if (position === null) return;
597620
setDraft({
598621
fileKey: item.id,
599622
path,
600623
oldPath: previousPath === path ? null : previousPath,
601-
line: range.end,
602-
side: fromViewerSide(range.endSide ?? range.side),
624+
position,
603625
range,
604626
});
605627
},
@@ -842,7 +864,7 @@ export function PullRequestCodeTab({
842864
{annotation.metadata.draft && draft ? (
843865
<DiffCommentAnnotation
844866
kind="draft"
845-
rangeLabel={`${draft.path}:${draft.line}`}
867+
rangeLabel={`${draft.path}:${getReviewPositionAnchor(draft.position).line}`}
846868
text=""
847869
submitLabel="Add to review"
848870
{...(onAskAboutSelection
@@ -864,8 +886,7 @@ export function PullRequestCodeTab({
864886
id: nextPendingReviewCommentId(),
865887
path: draft.path,
866888
...(draft.oldPath === null ? {} : { oldPath: draft.oldPath }),
867-
line: draft.line,
868-
side: draft.side,
889+
position: draft.position,
869890
body,
870891
});
871892
setDraft(null);

‎apps/web/src/components/pullRequest/pullRequestReviewStore.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { beforeEach, describe, expect, it } from "vite-plus/test";
33
import { type PendingReviewComment, usePullRequestReviewStore } from "./pullRequestReviewStore";
44

55
function comment(id: string, body = id): PendingReviewComment {
6-
return { id, body, path: "src/app.ts", line: 1, side: "right" };
6+
return { id, body, path: "src/app.ts", position: { kind: "added", newLine: 1 } };
77
}
88

99
describe("pull request review drafts", () => {

0 commit comments

Comments
 (0)