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

labels.ts

76 lines2.2 KB

1/**
2 * Mutually exclusive bot-review labels. Separate from maintainer
3 * `ready-to-merge` and `waiting-on:*` labels, which this module never
4 * touches.
5 */
6​
7import type { GitHubClient } from '../../scripts/maintainer/github.ts'
8​
9export const REVIEW_LABELS = [
10 {
11 name: 'ai-rejected',
12 color: 'b60205',
13 description: 'Grok review bot: not useful, or does not fix the claimed bug',
14 },
15 {
16 name: 'ai-needs-work',
17 color: 'fbca04',
18 description: 'Grok review bot: listed fixes are not on the branch yet',
19 },
20 {
21 name: 'ai-ready',
22 color: '0e8a16',
23 description: 'Grok review bot: a maintainer can merge after they Approve',
24 },
25] as const
26​
27type ReviewState = (typeof REVIEW_LABELS)[number]['name']
28​
29function errorMentionsStatus(error: unknown, status: string) {
30 return error instanceof Error && error.message.includes(`HTTP ${status}:`)
32​
33/**
34 * Set the bot review state on an issue or PR.
35 *
36 * Creates the `ai-*` labels if they are missing, removes the other two from
37 * the issue, then adds `state`. Never touches `ready-to-merge` or
38 * `waiting-on:*`.
39 *
40 * @param client GitHub REST client
41 * @param repo owner/name, for example `TanStack/ai`
42 * @param issueNumber issue or pull request number
43 * @param state one of `ai-rejected`, `ai-needs-work`, `ai-ready`
44 */
45export async function setReviewState(
46 client: GitHubClient,
47 repo: string,
48 issueNumber: number,
49 state: ReviewState,
50) {
51 for (const label of REVIEW_LABELS) {
52 try {
53 await client.rest('POST', `/repos/${repo}/labels`, label)
54 } catch (error) {
55 // 422 = already exists; anything else should surface.
56 if (!errorMentionsStatus(error, '422')) throw error
57 }
58 }
59​
60 const others = REVIEW_LABELS.filter((label) => label.name !== state)
61 for (const label of others) {
62 try {
63 await client.rest(
64 'DELETE',
65 `/repos/${repo}/issues/${issueNumber}/labels/${encodeURIComponent(label.name)}`,
66 )
67 } catch (error) {
68 // 404 = not on the issue; anything else should surface.
69 if (!errorMentionsStatus(error, '404')) throw error
70 }
71 }
72​
73 await client.rest('POST', `/repos/${repo}/issues/${issueNumber}/labels`, {
74 labels: [state],
75 })