Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

comments.ts

122 lines3.3 KB

1/**
2 * One bot review comment per PR. HTML marker makes later runs edit the same
3 * comment instead of posting a second copy.
4 */
5​
6import type { GitHubClient } from '../../scripts/maintainer/github'
7​
8export const COMMENT_MARKER = '<!-- tanstack-ai-review-bot:v1 -->'
9​
10function parseIssueComment(value: unknown) {
11 if (typeof value !== 'object' || value === null) return null
12 if (!('id' in value) || !('body' in value)) return null
13 if (typeof value.id !== 'number' || typeof value.body !== 'string') {
14 return null
15 }
16 const user =
17 'user' in value && typeof value.user === 'object' && value.user !== null
18 ? value.user
19 : null
20 const userLogin =
21 user !== null && 'login' in user && typeof user.login === 'string'
22 ? user.login
23 : null
24 return { id: value.id, body: value.body, userLogin }
26​
27export function isBotReviewComment(
28 comment: { body: string; userLogin: string | null },
29 machineUserLogin: string,
30) {
31 return (
32 comment.body.includes(COMMENT_MARKER) &&
33 comment.userLogin !== null &&
34 comment.userLogin.toLowerCase() === machineUserLogin.toLowerCase()
35 )
37​
38export function buildReviewComment(input: {
39 verdict: 'reject' | 'polish' | 'ready'
40 headSha: string
41 findings: Array<string>
42 pushNote: string
43 label: 'ai-rejected' | 'ai-needs-work' | 'ai-ready'
44 securityNote: string
45}) {
46 const findingLines =
47 input.findings.length === 0
48 ? '- None'
49 : input.findings.map((finding) => `- ${finding}`).join('\n')
50 return [
51 'This comment is automated by a Grok agent. It is not a maintainer review.',
52 '',
53 `**Verdict:** ${input.verdict}`,
54 `**Head SHA:** ${input.headSha}`,
55 `**Label:** \`${input.label}\``,
56 '',
57 '**Findings**',
58 findingLines,
59 '',
60 '**Push**',
61 input.pushNote,
62 '',
63 '**Security**',
64 input.securityNote,
65 '',
66 'Maintainers still GitHub-approve.',
67 COMMENT_MARKER,
68 ].join('\n')
70​
71/** Comment for a PR the host check blocked. It carries no verdict and no label. */
72export function buildBlockedComment(input: {
73 headSha: string
74 reasons: Array<string>
75}) {
76 return [
77 'This comment is automated by a Grok agent. It is not a maintainer review.',
78 '',
79 '**Verdict:** none. The security check blocked this review.',
80 `**Head SHA:** ${input.headSha}`,
81 '',
82 '**Security**',
83 ...input.reasons.map((reason) => `- ${reason}`),
84 '',
85 'A maintainer must review this pull request by hand.',
86 COMMENT_MARKER,
87 ].join('\n')
89​
90export async function upsertReviewComment(
91 client: GitHubClient,
92 repo: string,
93 issueNumber: number,
94 body: string,
95 machineUserLogin: string,
96) {
97 const listPath = `/repos/${repo}/issues/${issueNumber}/comments`
98 // ponytail: first 100 comments only. Paginate if a PR ever passes that.
99 const list = await client.rest('GET', `${listPath}?per_page=100`)
100 if (!Array.isArray(list)) {
101 throw new Error(`GitHub GET ${listPath} did not return an array`)
102 }
103 const comments = []
104 for (const item of list) {
105 const comment = parseIssueComment(item)
106 if (comment !== null) comments.push(comment)
107 }
108 const existing = comments.find((comment) =>
109 isBotReviewComment(comment, machineUserLogin),
110 )
111 if (existing) {
112 await client.rest(
113 'PATCH',
114 `/repos/${repo}/issues/comments/${existing.id}`,
115 {
116 body,
117 },
118 )
119 return
120 }
121 await client.rest('POST', listPath, { body })