Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pullRequestDiffComments.ts

132 lines4.8 KB

1import type { PullRequestReviewPosition, PullRequestReviewThread } from "@t3tools/contracts";
2import { msg, plural, part } from "gt-react-native";
3​
4import { m, type MessageResolver } from "../../i18n/messages";
5import type { NativeReviewDiffRow } from "../diffs/nativeReviewDiffSurface";
6import type { NativeReviewDiffData } from "../review/nativeReviewDiffAdapter";
7import type { PendingReviewComment } from "./pullRequestReviewDrafts";
8​
9export function reviewPosition(row: NativeReviewDiffRow): PullRequestReviewPosition | null {
10 if (row.kind !== "line") return null;
11 if (row.change === "add" && row.newLineNumber)
12 return { kind: "added", newLine: row.newLineNumber };
13 if (row.change === "delete" && row.oldLineNumber)
14 return { kind: "deleted", oldLine: row.oldLineNumber };
15 if (row.oldLineNumber && row.newLineNumber)
16 return {
17 kind: "context",
18 oldLine: row.oldLineNumber,
19 newLine: row.newLineNumber,
20 side: "right",
21 };
22 return null;
24export function discussionCommentCountLabel(count: number, resolve: MessageResolver = m) {
25 return resolve(
26 msg`${plural(
27 { count },
28 {
29 one: part`${{ count }} comment`,
30 other: part`${{ count }} comments`,
31 },
32 )}`,
33 );
35export function reviewPositionLabel(
36 position: PullRequestReviewPosition,
37 resolve: MessageResolver = m,
38) {
39 const oldSide =
40 position.kind === "deleted" || (position.kind === "context" && position.side === "left");
41 const line = reviewLine(position);
42 return oldSide ? resolve(msg`Old line ${{ line }}`) : resolve(msg`Line ${{ line }}`);
44function reviewLine(position: PullRequestReviewPosition) {
45 if (position.kind === "deleted" || (position.kind === "context" && position.side === "left"))
46 return position.oldLine;
47 return position.newLine;
49export function annotatePullRequestDiff(
50 data: NativeReviewDiffData,
51 threads: readonly PullRequestReviewThread[],
52 drafts: readonly PendingReviewComment[],
53 headSha: string | undefined,
54 resolve: MessageResolver = m,
55): NativeReviewDiffData {
56 if (threads.length === 0 && drafts.length === 0) return data;
57 const anchors = new Map<string, NativeReviewDiffRow[]>();
58 const firstByPath = new Map<string, NativeReviewDiffRow>();
59 const lines = new Map<string, NativeReviewDiffRow>();
60 const paths = new Map(data.files.map((file) => [file.id, file.path]));
61 for (const original of data.rows) {
62 const row = {
63 ...original,
64 filePath: original.filePath ?? (original.fileId ? paths.get(original.fileId) : undefined),
65 };
66 if (row.kind === "file" && row.filePath) firstByPath.set(row.filePath, row);
67 if (row.kind !== "line" || !row.filePath) continue;
68 if (row.oldLineNumber)
69 lines.set(JSON.stringify([row.filePath, "left", row.oldLineNumber]), row);
70 if (row.newLineNumber)
71 lines.set(JSON.stringify([row.filePath, "right", row.newLineNumber]), row);
72 }
73 const attach = (
74 anchor: NativeReviewDiffRow | undefined,
75 id: string,
76 body: string,
77 label: string,
78 ) => {
79 if (!anchor) return;
80 const rows = anchors.get(anchor.id) ?? [];
81 rows.push({
82 kind: "comment",
83 id,
84 fileId: anchor.fileId,
85 filePath: anchor.filePath,
86 commentText: body,
87 commentRangeLabel: label,
88 });
89 anchors.set(anchor.id, rows);
90 };
91 for (const thread of threads) {
92 if (thread.isOutdated) continue;
93 const anchor =
94 thread.line === null
95 ? firstByPath.get(thread.path)
96 : lines.get(JSON.stringify([thread.path, thread.side, thread.line]));
97 const first = thread.comments[0];
98 attach(
99 anchor,
100 `discussion:${thread.id}`,
101 `${first?.author?.login ?? resolve(msg("Discussion"))}${
102 thread.isResolved ? ` · ${resolve(msg({ context: "review thread state" })`Resolved`)}` : ""
103 }\n${first?.body ?? ""}\n${resolve(
104 msg`${plural(
105 { count: thread.commentCount ?? thread.comments.length },
106 {
107 one: part`${{ count: thread.commentCount ?? thread.comments.length }} comment · Tap to open`,
108 other: part`${{ count: thread.commentCount ?? thread.comments.length }} comments · Tap to open`,
109 },
110 )}`,
111 )}`,
112 thread.line === null ? "file" : String(thread.line),
113 );
114 }
115 for (const draft of drafts) {
116 if (draft.headSha !== headSha) continue;
117 const side =
118 draft.position.kind === "deleted"
119 ? "left"
120 : draft.position.kind === "context"
121 ? draft.position.side
122 : "right";
123 const line = reviewLine(draft.position);
124 attach(
125 lines.get(JSON.stringify([draft.path, side, line])),
126 `draft:${draft.id}`,
127 `${resolve(msg("Pending review · Tap to edit"))}\n${draft.body}`,
128 String(line),
129 );
130 }
131 return { ...data, rows: data.rows.flatMap((row) => [row, ...(anchors.get(row.id) ?? [])]) };