Skip to content

Commit 73ac59a

Browse files
authored
feat(desktop): add inline PR diff comments (#2162)
1 parent de2220a commit 73ac59a

9 files changed

Lines changed: 570 additions & 34 deletions

‎desktop/src/features/projects/hooks.ts‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,22 @@ import type {
3939
import { summarizeProjectActivityEvents } from "./projectActivity.mjs";
4040
import type { ProjectIssue } from "./projectIssues.mjs";
4141
import { projectIssueEventsToIssues } from "./projectIssues.mjs";
42-
import type { ProjectPullRequest } from "./projectPullRequests.mjs";
43-
import { projectPullRequestEventsToPullRequests } from "./projectPullRequests.mjs";
42+
import type {
43+
ProjectPullRequest,
44+
ProjectPullRequestCommentAnchor,
45+
} from "./projectPullRequests.mjs";
46+
import {
47+
normalizeProjectPullRequestCommentAnchor,
48+
PR_INLINE_COMMENT_LABEL,
49+
projectPullRequestEventsToPullRequests,
50+
} from "./projectPullRequests.mjs";
4451
import { fetchProjectsWorkItems } from "./projectWorkItems";
4552

46-
export type { ProjectIssue, ProjectPullRequest };
53+
export type {
54+
ProjectIssue,
55+
ProjectPullRequest,
56+
ProjectPullRequestCommentAnchor,
57+
};
4758

4859
const HIDDEN_PROJECT_CARDS_KEY = "buzz.projects.hidden-cards.v1";
4960

@@ -391,12 +402,14 @@ async function fetchProjectPullRequests(
391402
// features/pulse/lib/projectComments.ts). If the relay ever allowlists
392403
// 1111, migrate these to NIP-22 comments and drop that filter.
393404
async function createProjectPullRequestComment({
405+
anchor,
394406
content,
395407
mediaTags,
396408
mentionPubkeys = [],
397409
project,
398410
pullRequest,
399411
}: {
412+
anchor?: ProjectPullRequestCommentAnchor;
400413
content: string;
401414
mediaTags?: string[][];
402415
mentionPubkeys?: string[];
@@ -407,6 +420,15 @@ async function createProjectPullRequestComment({
407420
if (!body) {
408421
throw new Error("Comment cannot be empty.");
409422
}
423+
const normalizedAnchor = anchor
424+
? normalizeProjectPullRequestCommentAnchor(anchor)
425+
: null;
426+
if (anchor && !normalizedAnchor) {
427+
throw new Error("Comment location is invalid.");
428+
}
429+
if (normalizedAnchor && !pullRequest.commit) {
430+
throw new Error("Pull request commit is required for inline comments.");
431+
}
410432

411433
const recipients = new Set([
412434
project.owner.toLowerCase(),
@@ -418,6 +440,15 @@ async function createProjectPullRequestComment({
418440
["e", pullRequest.id, "", "root"],
419441
["a", project.repoAddress],
420442
...[...recipients].map((recipient) => ["p", recipient]),
443+
...(normalizedAnchor
444+
? [
445+
["t", PR_INLINE_COMMENT_LABEL],
446+
["c", pullRequest.commit as string],
447+
["file", normalizedAnchor.path],
448+
["side", normalizedAnchor.side],
449+
["line", String(normalizedAnchor.line)],
450+
]
451+
: []),
421452
...(mediaTags ?? []),
422453
];
423454

@@ -826,18 +857,21 @@ export function useCreateProjectPullRequestCommentMutation(
826857

827858
return useMutation({
828859
mutationFn: ({
860+
anchor,
829861
content,
830862
mediaTags,
831863
mentionPubkeys,
832864
pullRequest,
833865
}: {
866+
anchor?: ProjectPullRequestCommentAnchor;
834867
content: string;
835868
mediaTags?: string[][];
836869
mentionPubkeys?: string[];
837870
pullRequest: ProjectPullRequest;
838871
}) => {
839872
if (!project) throw new Error("No project selected.");
840873
return createProjectPullRequestComment({
874+
anchor,
841875
content,
842876
mediaTags,
843877
mentionPubkeys,

‎desktop/src/features/projects/projectPullRequests.d.mts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export type ProjectPullRequestComment = {
1515
author: string;
1616
createdAt: number;
1717
commit: string | null;
18+
anchor: ProjectPullRequestCommentAnchor | null;
19+
inlineCommentStatus: "current" | "outdated" | null;
20+
isInlineComment: boolean;
1821
isApproval: boolean;
1922
isChangeRequest: boolean;
2023
isReviewRequest: boolean;
@@ -25,6 +28,12 @@ export type ProjectPullRequestComment = {
2528
reviewerPubkeys: string[];
2629
};
2730

31+
export type ProjectPullRequestCommentAnchor = {
32+
line: number;
33+
path: string;
34+
side: "old" | "new";
35+
};
36+
2837
export type ProjectPullRequestApproval = {
2938
id: string;
3039
author: string;
@@ -44,6 +53,18 @@ export type ProjectPullRequestChangeRequest = {
4453
export const PR_REVIEW_REQUEST_LABEL: string;
4554
export const PR_APPROVAL_LABEL: string;
4655
export const PR_CHANGES_REQUESTED_LABEL: string;
56+
export const PR_INLINE_COMMENT_LABEL: string;
57+
58+
export function normalizeProjectPullRequestCommentAnchor(
59+
anchor:
60+
| {
61+
line?: unknown;
62+
path?: unknown;
63+
side?: unknown;
64+
}
65+
| null
66+
| undefined,
67+
): ProjectPullRequestCommentAnchor | null;
4768

4869
export type ProjectPullRequest = {
4970
id: string;

‎desktop/src/features/projects/projectPullRequests.mjs‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,54 @@ function eventToPullRequestUpdate(event) {
148148
export const PR_REVIEW_REQUEST_LABEL = "review-request";
149149
export const PR_APPROVAL_LABEL = "approval";
150150
export const PR_CHANGES_REQUESTED_LABEL = "changes-requested";
151+
export const PR_INLINE_COMMENT_LABEL = "inline-comment";
152+
153+
/** Validate an inline diff anchor without normalizing attacker-controlled paths. */
154+
export function normalizeProjectPullRequestCommentAnchor(anchor) {
155+
if (!anchor || typeof anchor.path !== "string") return null;
156+
const path = anchor.path;
157+
if (
158+
path.length === 0 ||
159+
path.length > 4_096 ||
160+
path.startsWith("/") ||
161+
path.includes("\\") ||
162+
path.includes("\0") ||
163+
path
164+
.split("/")
165+
.some((segment) => !segment || segment === "." || segment === "..")
166+
) {
167+
return null;
168+
}
169+
if (anchor.side !== "old" && anchor.side !== "new") return null;
170+
if (!Number.isSafeInteger(anchor.line) || anchor.line < 1) return null;
171+
return { line: anchor.line, path, side: anchor.side };
172+
}
151173

152174
function eventToPullRequestComment(event) {
153175
const labels = getAllTags(event, "t").map((label) => label.toLowerCase());
154176
const isReviewRequest = labels.includes(PR_REVIEW_REQUEST_LABEL);
155177
const isApproval = labels.includes(PR_APPROVAL_LABEL);
156178
const isChangeRequest = labels.includes(PR_CHANGES_REQUESTED_LABEL);
179+
const lineTag = getTag(event, "line");
180+
const parsedLine =
181+
lineTag && /^[1-9]\d*$/.test(lineTag) ? Number(lineTag) : Number.NaN;
182+
const anchor =
183+
isReviewRequest || isApproval || isChangeRequest
184+
? null
185+
: normalizeProjectPullRequestCommentAnchor({
186+
line: parsedLine,
187+
path: getTag(event, "file"),
188+
side: getTag(event, "side"),
189+
});
157190
return {
158191
id: event.id,
159192
content: event.content,
160193
author: event.pubkey,
161194
createdAt: event.created_at,
162195
commit: getTag(event, "c") ?? null,
196+
anchor,
197+
isInlineComment:
198+
Boolean(anchor) || labels.includes(PR_INLINE_COMMENT_LABEL),
163199
isApproval,
164200
isChangeRequest,
165201
isReviewRequest,
@@ -281,6 +317,12 @@ export function eventToProjectPullRequest(
281317
const initialCommit = getTag(pullRequest, "c") ?? null;
282318
const comments = parsedComments.map((comment) => ({
283319
...comment,
320+
inlineCommentStatus: comment.anchor
321+
? latestCommit &&
322+
reviewDecisionCommit(comment, initialCommit) === latestCommit
323+
? "current"
324+
: "outdated"
325+
: null,
284326
isTrustedReviewDecision:
285327
Boolean(comment.reviewDecision) &&
286328
trustedActors.has(comment.author.toLowerCase()),

‎desktop/src/features/projects/projectPullRequests.test.mjs‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ function commentEvent({
179179
labels = [],
180180
reviewers = [],
181181
commit,
182+
anchor,
182183
}) {
183184
return {
184185
id: id ?? `comment-${pubkey.slice(0, 8)}-${createdAt}`,
@@ -192,10 +193,89 @@ function commentEvent({
192193
...reviewers.map((reviewer) => ["p", reviewer]),
193194
...labels.map((label) => ["t", label]),
194195
...(commit ? [["c", commit]] : []),
196+
...(anchor
197+
? [
198+
["t", "inline-comment"],
199+
["file", anchor.path],
200+
["side", anchor.side],
201+
["line", String(anchor.line)],
202+
]
203+
: []),
195204
],
196205
};
197206
}
198207

208+
test("parses commit-scoped inline comment anchors", () => {
209+
const commit = "1111111111111111111111111111111111111111";
210+
const comment = commentEvent({
211+
pubkey: ATTACKER,
212+
createdAt: 150,
213+
content: "Could this be clearer?",
214+
commit,
215+
anchor: {
216+
path: "desktop/src/features/projects/hooks.ts",
217+
side: "new",
218+
line: 42,
219+
},
220+
});
221+
222+
const pullRequest = eventToProjectPullRequest(
223+
pullRequestEvent(),
224+
[],
225+
[comment],
226+
);
227+
228+
assert.deepEqual(pullRequest.comments[0].anchor, {
229+
path: "desktop/src/features/projects/hooks.ts",
230+
side: "new",
231+
line: 42,
232+
});
233+
assert.equal(pullRequest.comments[0].inlineCommentStatus, "current");
234+
assert.equal(pullRequest.comments[0].isInlineComment, true);
235+
});
236+
237+
test("marks inline comments on earlier commits as outdated", () => {
238+
const initialCommit = "1111111111111111111111111111111111111111";
239+
const currentCommit = "2222222222222222222222222222222222222222";
240+
const comment = commentEvent({
241+
pubkey: ATTACKER,
242+
createdAt: 150,
243+
commit: initialCommit,
244+
anchor: { path: "src/example.ts", side: "old", line: 7 },
245+
});
246+
const update = updateEvent({
247+
pubkey: AUTHOR,
248+
createdAt: 200,
249+
commit: currentCommit,
250+
});
251+
252+
const pullRequest = eventToProjectPullRequest(
253+
pullRequestEvent(),
254+
[update],
255+
[comment],
256+
);
257+
258+
assert.equal(pullRequest.comments[0].inlineCommentStatus, "outdated");
259+
});
260+
261+
test("rejects malformed inline comment anchors", () => {
262+
const comment = commentEvent({
263+
pubkey: ATTACKER,
264+
createdAt: 150,
265+
commit: "1111111111111111111111111111111111111111",
266+
anchor: { path: "../secrets", side: "new", line: 0 },
267+
});
268+
269+
const pullRequest = eventToProjectPullRequest(
270+
pullRequestEvent(),
271+
[],
272+
[comment],
273+
);
274+
275+
assert.equal(pullRequest.comments[0].anchor, null);
276+
assert.equal(pullRequest.comments[0].inlineCommentStatus, null);
277+
});
278+
199279
test("draft/ready toggles via status kinds 1633 and 1630", () => {
200280
const draft = statusEvent({ kind: 1633, pubkey: AUTHOR, createdAt: 300 });
201281
const draftPullRequest = eventToProjectPullRequest(

0 commit comments

Comments
 (0)