Open History to see the last commit
1import type { PullRequestReviewPosition, PullRequestReviewThread } from "@t3tools/contracts";
2import type { NativeReviewDiffRow } from "../diffs/nativeReviewDiffSurface";
3import type { NativeReviewDiffData } from "../review/nativeReviewDiffAdapter";
4import type { PendingReviewComment } from "./pullRequestReviewDrafts";
5
6export function reviewPosition(row: NativeReviewDiffRow): PullRequestReviewPosition | null {
7 if (row.kind !== "line") return null;
8 if (row.change === "add" && row.newLineNumber)
9 return { kind: "added", newLine: row.newLineNumber };
10 if (row.change === "delete" && row.oldLineNumber)
11 return { kind: "deleted", oldLine: row.oldLineNumber };
12 if (row.oldLineNumber && row.newLineNumber)
13 return {
14 kind: "context",
15 oldLine: row.oldLineNumber,
16 newLine: row.newLineNumber,
17 side: "right",
18 };
19 return null;
20}
21export function reviewPositionLabel(position: PullRequestReviewPosition) {
22 const oldSide =
23 position.kind === "deleted" || (position.kind === "context" && position.side === "left");
24 return `${oldSide ? "Old line" : "Line"} ${reviewLine(position)}`;
25}
26function reviewLine(position: PullRequestReviewPosition) {
27 if (position.kind === "deleted" || (position.kind === "context" && position.side === "left"))
28 return position.oldLine;
29 return position.newLine;
30}
31export function annotatePullRequestDiff(
32 data: NativeReviewDiffData,
33 threads: readonly PullRequestReviewThread[],
34 drafts: readonly PendingReviewComment[],
35 headSha: string | undefined,
36): NativeReviewDiffData {
37 const anchors = new Map<string, NativeReviewDiffRow[]>();
38 const firstByPath = new Map<string, NativeReviewDiffRow>();
39 const lines = new Map<string, NativeReviewDiffRow>();
40 const paths = new Map(data.files.map((file) => [file.id, file.path]));
41 for (const original of data.rows) {
42 const row = {
43 ...original,
44 filePath: original.filePath ?? (original.fileId ? paths.get(original.fileId) : undefined),
45 };
46 if (row.kind === "file" && row.filePath) firstByPath.set(row.filePath, row);
47 if (row.kind !== "line" || !row.filePath) continue;
48 if (row.oldLineNumber)
49 lines.set(JSON.stringify([row.filePath, "left", row.oldLineNumber]), row);
50 if (row.newLineNumber)
51 lines.set(JSON.stringify([row.filePath, "right", row.newLineNumber]), row);
52 }
53 const attach = (
54 anchor: NativeReviewDiffRow | undefined,
55 id: string,
56 body: string,
57 label: string,
58 ) => {
59 if (!anchor) return;
60 const rows = anchors.get(anchor.id) ?? [];
61 rows.push({
62 kind: "comment",
63 id,
64 fileId: anchor.fileId,
65 filePath: anchor.filePath,
66 commentText: body,
67 commentRangeLabel: label,
68 });
69 anchors.set(anchor.id, rows);
70 };
71 for (const thread of threads) {
72 if (thread.isOutdated) continue;
73 const anchor =
74 thread.line === null
75 ? firstByPath.get(thread.path)
76 : lines.get(JSON.stringify([thread.path, thread.side, thread.line]));
77 const first = thread.comments[0];
78 attach(
79 anchor,
80 `discussion:${thread.id}`,
81 `${first?.author?.login ?? "Discussion"}${thread.isResolved ? " · Resolved" : ""}\n${first?.body ?? ""}\n${thread.commentCount ?? thread.comments.length} comments · Tap to open`,
82 thread.line === null ? "file" : String(thread.line),
83 );
84 }
85 for (const draft of drafts) {
86 if (draft.headSha !== headSha) continue;
87 const side =
88 draft.position.kind === "deleted"
89 ? "left"
90 : draft.position.kind === "context"
91 ? draft.position.side
92 : "right";
93 const line = reviewLine(draft.position);
94 attach(
95 lines.get(JSON.stringify([draft.path, side, line])),
96 `draft:${draft.id}`,
97 `Pending review · Tap to edit\n${draft.body}`,
98 String(line),
99 );
100 }
101 return { ...data, rows: data.rows.flatMap((row) => [row, ...(anchors.get(row.id) ?? [])]) };
102}