Open History to see the last commit
1import type { PullRequestReviewGuideExcerpt, PullRequestReviewGuideStop } from "@t3tools/contracts";
2import type { ReviewGuideGenerationResult } from "@t3tools/provider-core/server/textGeneration";
3
4const MAX_EXCERPT_LINES = 25;
5
6interface LineRange {
7 readonly start: number;
8 readonly end: number;
9}
10
11interface FileHunks {
12 readonly new: Array<LineRange>;
13 readonly old: Array<LineRange>;
14}
15
16export interface ReviewGuideChange {
17 readonly files: ReadonlyArray<string>;
18 readonly hunks: ReadonlyMap<string, FileHunks>;
19}
20
21const HUNK_HEADER = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
22
23const hunkRange = (start: string, count: string | undefined): LineRange | null => {
24 const lines = count === undefined ? 1 : Number(count);
25 return lines === 0 ? null : { start: Number(start), end: Number(start) + lines - 1 };
26};
27
28export function parseReviewGuideChange(nameOnly: string, patch: string): ReviewGuideChange {
29 const hunks = new Map<string, FileHunks>();
30 let oldPath: string | null = null;
31 let current: FileHunks | null = null;
32 // Inside a file's hunks a `--- ` or `+++ ` line is a removed or added source line, not a header.
33 let inHunks = false;
34 for (const line of patch.split("\n")) {
35 if (line.startsWith("diff --git ")) {
36 oldPath = null;
37 current = null;
38 inHunks = false;
39 } else if (!inHunks && line.startsWith("--- ")) {
40 oldPath = line.startsWith("--- a/") ? line.slice(6) : null;
41 } else if (!inHunks && line.startsWith("+++ ")) {
42 const path = line.startsWith("+++ b/") ? line.slice(6) : oldPath;
43 if (path === null) continue;
44 current = hunks.get(path) ?? { new: [], old: [] };
45 hunks.set(path, current);
46 } else if (current !== null) {
47 const header = HUNK_HEADER.exec(line);
48 if (header === null) continue;
49 inHunks = true;
50 const removed = hunkRange(header[1]!, header[2]);
51 const added = hunkRange(header[3]!, header[4]);
52 if (removed !== null) current.old.push(removed);
53 if (added !== null) current.new.push(added);
54 }
55 }
56 return { files: nameOnly.split("\0").filter((path) => path !== ""), hunks };
57}
58
59function clampExcerpt(
60 excerpt: PullRequestReviewGuideExcerpt,
61 change: ReviewGuideChange,
62): PullRequestReviewGuideExcerpt | null {
63 const start = Math.min(excerpt.start, excerpt.end);
64 const end = Math.max(excerpt.start, excerpt.end);
65 const overlapping = (change.hunks.get(excerpt.path)?.[excerpt.side] ?? []).filter(
66 (hunk) => hunk.start <= end && start <= hunk.end,
67 );
68 const first = overlapping[0];
69 const last = overlapping.at(-1);
70 if (first === undefined || last === undefined) return null;
71 return { ...excerpt, start: Math.max(start, first.start), end: Math.min(end, last.end) };
72}
73
74function mergeExcerpts(
75 excerpts: ReadonlyArray<PullRequestReviewGuideExcerpt>,
76): Array<PullRequestReviewGuideExcerpt> {
77 const merged: Array<PullRequestReviewGuideExcerpt> = [];
78 for (const excerpt of excerpts) {
79 const index = merged.findIndex(
80 (other) =>
81 other.path === excerpt.path &&
82 other.side === excerpt.side &&
83 other.start <= excerpt.end + 1 &&
84 excerpt.start <= other.end + 1,
85 );
86 const other = merged[index];
87 if (other === undefined) {
88 merged.push(excerpt);
89 continue;
90 }
91 merged[index] = {
92 ...other,
93 start: Math.min(other.start, excerpt.start),
94 end: Math.max(other.end, excerpt.end),
95 caption: other.caption || excerpt.caption,
96 };
97 }
98 return merged.map((excerpt) => ({
99 ...excerpt,
100 end: Math.min(excerpt.end, excerpt.start + MAX_EXCERPT_LINES - 1),
101 }));
102}
103
104const nonEmpty = (value: string) => value.trim() !== "";
105
106export function normalizeReviewGuide(
107 guide: ReviewGuideGenerationResult,
108 change: ReviewGuideChange,
109): ReviewGuideGenerationResult {
110 const changed = new Set(change.files);
111 const inChange = (path: string) => changed.has(path);
112
113 const shaped: Array<PullRequestReviewGuideStop> = guide.stops
114 .filter((stop) => nonEmpty(stop.title))
115 .map((stop) => {
116 const excerpts = mergeExcerpts(
117 stop.excerpts.flatMap((excerpt) => {
118 const clamped = inChange(excerpt.path) ? clampExcerpt(excerpt, change) : null;
119 return clamped === null ? [] : [clamped];
120 }),
121 );
122 return {
123 ...stop,
124 attention: stop.attention !== null && nonEmpty(stop.attention.note) ? stop.attention : null,
125 excerpts,
126 files: stop.files
127 .filter((file) => inChange(file.path))
128 .map(({ path, note }) => (note?.trim() ? { path, note } : { path })),
129 notes: stop.notes.filter(
130 (note) =>
131 nonEmpty(note.title) &&
132 excerpts.some(
133 (excerpt) =>
134 excerpt.path === note.path &&
135 excerpt.start <= note.line &&
136 note.line <= excerpt.end,
137 ),
138 ),
139 questions: stop.questions.filter(nonEmpty),
140 };
141 });
142
143 const placed = new Set(shaped.flatMap((stop) => stop.excerpts.map((excerpt) => excerpt.path)));
144 const stops = shaped.map((stop) => {
145 const files = stop.files.filter((file) => !placed.has(file.path));
146 for (const file of files) placed.add(file.path);
147 return { ...stop, files };
148 });
149 const unplaced = change.files.filter((path) => !placed.has(path));
150 if (unplaced.length > 0) {
151 stops.push({
152 title: "Other files",
153 summary: "Changed files the tour does not otherwise cover.",
154 attention: null,
155 excerpts: [],
156 files: unplaced.map((path) => ({ path })),
157 notes: [],
158 questions: [],
159 });
160 }
161
162 return { ...guide, stops };
163}