Open History to see the last commit
1export interface ReviewCommentContext {
2 readonly id: string;
3 readonly sectionId: string;
4 readonly sectionTitle: string;
5 readonly filePath: string;
6 readonly startIndex: number;
7 readonly endIndex: number;
8 readonly rangeLabel: string;
9 readonly text: string;
10 readonly diff: string;
11}
12
13export type ReviewCommentMessageSegment =
14 | {
15 readonly kind: "text";
16 readonly id: string;
17 readonly text: string;
18 }
19 | {
20 readonly kind: "review-comment";
21 readonly comment: ReviewCommentContext;
22 };
23
24const REVIEW_COMMENT_BLOCK_PATTERN = /<review_comment\b([^>]*)>\s*([\s\S]*?)<\/review_comment>/g;
25const REVIEW_COMMENT_ATTRIBUTE_PATTERN = /([a-zA-Z][a-zA-Z0-9_-]*)="([^"]*)"/g;
26const REVIEW_COMMENT_DIFF_FENCE_PATTERN = /```diff\s*\n([\s\S]*?)\n```/;
27
28function unescapeReviewCommentAttribute(value: string): string {
29 return value
30 .replace(/</g, "<")
31 .replace(/"/g, '"')
32 .replace(/&/g, "&");
33}
34
35function readReviewCommentAttributes(rawAttributes: string): Record<string, string> {
36 const attributes: Record<string, string> = {};
37 for (const match of rawAttributes.matchAll(REVIEW_COMMENT_ATTRIBUTE_PATTERN)) {
38 attributes[match[1]!] = unescapeReviewCommentAttribute(match[2] ?? "");
39 }
40 return attributes;
41}
42
43function readNonNegativeInteger(value: string | undefined): number | null {
44 if (value === undefined || !/^\d+$/.test(value)) {
45 return null;
46 }
47 return Number(value);
48}
49
50function extractReviewCommentText(rawBody: string): string {
51 const fenceIndex = rawBody.indexOf("```diff");
52 const commentBody = fenceIndex >= 0 ? rawBody.slice(0, fenceIndex) : rawBody;
53 return commentBody.trim();
54}
55
56function extractReviewCommentDiff(rawBody: string): string {
57 return rawBody.match(REVIEW_COMMENT_DIFF_FENCE_PATTERN)?.[1]?.trim() ?? "";
58}
59
60function parseReviewCommentContext(
61 rawAttributes: string,
62 rawBody: string,
63 index: number,
64): ReviewCommentContext | null {
65 const attributes = readReviewCommentAttributes(rawAttributes);
66 const startIndex = readNonNegativeInteger(attributes.startIndex);
67 const endIndex = readNonNegativeInteger(attributes.endIndex);
68 const filePath = attributes.filePath?.trim();
69 const sectionId = attributes.sectionId?.trim();
70 if (!filePath || !sectionId || startIndex === null || endIndex === null) {
71 return null;
72 }
73
74 return {
75 id: `review-comment:${index}:${sectionId}:${filePath}:${startIndex}:${endIndex}`,
76 sectionId,
77 sectionTitle: attributes.sectionTitle?.trim() || "Review",
78 filePath,
79 startIndex: Math.min(startIndex, endIndex),
80 endIndex: Math.max(startIndex, endIndex),
81 rangeLabel: attributes.rangeLabel?.trim() || "line",
82 text: extractReviewCommentText(rawBody),
83 diff: extractReviewCommentDiff(rawBody),
84 };
85}
86
87export function parseReviewCommentMessageSegments(
88 value: string,
89): ReadonlyArray<ReviewCommentMessageSegment> {
90 const segments: ReviewCommentMessageSegment[] = [];
91 let cursor = 0;
92 let parsedCommentIndex = 0;
93
94 for (const match of value.matchAll(REVIEW_COMMENT_BLOCK_PATTERN)) {
95 const matchIndex = match.index ?? 0;
96 const beforeText = value.slice(cursor, matchIndex);
97 if (beforeText.length > 0) {
98 segments.push({
99 kind: "text",
100 id: `review-comment-text:${cursor}`,
101 text: beforeText,
102 });
103 }
104
105 const comment = parseReviewCommentContext(match[1] ?? "", match[2] ?? "", parsedCommentIndex);
106 if (comment) {
107 segments.push({ kind: "review-comment", comment });
108 parsedCommentIndex += 1;
109 } else {
110 segments.push({
111 kind: "text",
112 id: `review-comment-invalid:${matchIndex}`,
113 text: match[0],
114 });
115 }
116
117 cursor = matchIndex + match[0].length;
118 }
119
120 const rest = value.slice(cursor);
121 if (rest.length > 0) {
122 segments.push({
123 kind: "text",
124 id: `review-comment-text:${cursor}`,
125 text: rest,
126 });
127 }
128
129 return segments;
130}
131
132export function hasReviewCommentMessageSegments(value: string): boolean {
133 return parseReviewCommentMessageSegments(value).some(
134 (segment) => segment.kind === "review-comment",
135 );
136}
137
138export function buildReviewCommentRenderablePatch(comment: ReviewCommentContext): string {
139 const diff = comment.diff.trim();
140 if (diff.length === 0) {
141 return "";
142 }
143 if (diff.startsWith("diff --git ")) {
144 return diff;
145 }
146
147 const normalizedPath = comment.filePath.replaceAll("\\", "/");
148 return [
149 `diff --git a/${normalizedPath} b/${normalizedPath}`,
150 `--- a/${normalizedPath}`,
151 `+++ b/${normalizedPath}`,
152 diff,
153 ].join("\n");
154}