-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathget-pr-comments.ts
More file actions
276 lines (250 loc) · 6.93 KB
/
Copy pathget-pr-comments.ts
File metadata and controls
276 lines (250 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import chalk from "chalk";
import { execa } from "execa";
import { z } from "zod";
/**
* Target format for existing comment threads, to be used to pass back to the AI when its review is requested again.
*/
export type ExistingCommentThread = {
/**
* The database id of the thread. This is the number the GitHub API requires when creating a reply to a comment thread.
*/
threadId: number;
/**
* Whether the thread is resolved.
*/
isResolved: boolean;
/**
* The author of the original comment (GitHub username, for `@tagging`)
*/
author: string;
/**
* The body text of the original comment.
*/
body: string;
/**
* The path to the file that the comment is about.
*/
path: string;
/**
* The 'position' of the comment in the diff (if any), expressed as the line number relative to the first chunk in a file diff.
* @see add-diff-line-numbers.ts
*/
position: number | null;
/**
* Whether the AI needs to respond to the thread.
* 'Yes' if it started the thread, but wasn't the last to reply. 'Maybe' if someone else started the thread, and the AI wasn't the last to reply. 'No' if the AI was the last to reply.
*/
requiresAiResponse: "yes" | "no" | "maybe";
/**
* The replies to the original comment.
*/
replies: {
/**
* The body text of the reply.
*/
body: string;
/**
* The author of the reply (GitHub username, for `@tagging`)
*/
author: string;
/**
* The date and time the reply was created.
*/
createdAt: string;
}[];
};
/**
* Schema for a comment node in the GitHub API GraphQL response.
*/
const CommentNodeSchema = z.object({
id: z.string(),
author: z.object({
login: z.string(),
}),
databaseId: z.number(),
body: z.string(),
createdAt: z.string(),
path: z.string(),
position: z.number().nullable(),
replyTo: z
.object({
id: z.string(),
author: z.object({
login: z.string(),
}),
})
.nullable()
.optional(),
});
/**
* Schema for the page info in the GitHub API GraphQL response.
*/
const PageInfoSchema = z.object({
hasNextPage: z.boolean(),
endCursor: z.string().nullable(),
});
/**
* Schema for a comment thread node in the GitHub API GraphQL response.
*/
const ThreadNodeSchema = z.object({
id: z.string(),
path: z.string(),
isResolved: z.boolean(),
comments: z.object({
nodes: z.array(CommentNodeSchema),
pageInfo: PageInfoSchema,
}),
});
/**
* Schema for the GitHub API GraphQL response for review threads.
*/
const ReviewThreadsGraphQLResponseSchema = z.object({
data: z.object({
repository: z.object({
pullRequest: z.object({
reviewThreads: z.object({
nodes: z.array(ThreadNodeSchema),
pageInfo: PageInfoSchema,
}),
}),
}),
}),
});
type ThreadNode = z.infer<typeof ThreadNodeSchema>;
/**
* Fetches all review threads with their comments, handling pagination.
*/
const fetchAllThreadsWithComments = async (
prNumber: string,
cursor?: string,
): Promise<ThreadNode[]> => {
const query = `
query($owner: String!, $repo: String!, $prNumber: Int!, $cursor: String) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $prNumber) {
reviewThreads(first: 100, after: $cursor) {
nodes {
id
path
isResolved
comments(first: 100) {
nodes {
id
databaseId
author {
login
}
body
createdAt
path
position
replyTo {
id
author {
login
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
}
`;
try {
const { stdout } = await execa("gh", [
"api",
"graphql",
"-f",
`query=${query}`,
"-F",
`prNumber=${prNumber}`,
"-f",
`owner=hashintel`,
"-f",
`repo=hash`,
...(cursor ? ["-f", `cursor=${cursor}`] : []),
]);
const parsedResponse = ReviewThreadsGraphQLResponseSchema.parse(
JSON.parse(stdout),
);
const threads =
parsedResponse.data.repository.pullRequest.reviewThreads.nodes;
const pageInfo =
parsedResponse.data.repository.pullRequest.reviewThreads.pageInfo;
if (pageInfo.hasNextPage && pageInfo.endCursor) {
const nextThreads = await fetchAllThreadsWithComments(
prNumber,
pageInfo.endCursor,
);
return [...threads, ...nextThreads];
}
return threads;
} catch {
console.error(chalk.red("Error fetching review threads"));
return [];
}
};
/**
* Fetches all comment threads on the PR, and nest the replies.
*
* Mark the thread as `requiresAiResponse`:
* - 'yes' if the AI started the thread, but wasn't the last to reply
* - 'maybe' if someone else started the thread, and the AI wasn't the last to reply
* - 'no' if the AI was the last to reply
*/
export const getPrComments = async (
prNumber: string,
): Promise<ExistingCommentThread[]> => {
const allThreads = await fetchAllThreadsWithComments(prNumber);
const commentThreads: ExistingCommentThread[] = [];
for (const thread of allThreads) {
const comments = thread.comments.nodes;
const rootComment = comments.find((comment) => comment.replyTo === null);
if (!rootComment) {
throw new Error("No root comment found");
}
const originalAuthor = rootComment.author.login;
const replies = comments.filter(
(comment) => comment.replyTo?.id === rootComment.id,
);
replies.sort(
(a, b) =>
new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime(),
);
const lastReply = replies[replies.length - 1];
const aiCreated = originalAuthor === "hashdotai";
const nonAiLastComment = lastReply
? lastReply.author.login !== "hashdotai"
: originalAuthor !== "hashdotai";
commentThreads.push({
threadId: rootComment.databaseId,
isResolved: thread.isResolved,
requiresAiResponse:
nonAiLastComment && !thread.isResolved
? aiCreated || rootComment.body.includes("@hashdotai")
? "yes"
: "maybe"
: "no",
author: aiCreated ? "you" : originalAuthor,
body: rootComment.body,
path: rootComment.path,
position: rootComment.position,
replies: replies.map((reply) => ({
body: reply.body,
author: reply.author.login === "hashdotai" ? "you" : reply.author.login,
createdAt: reply.createdAt,
})),
});
}
return commentThreads;
};