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

pr.ts

156 lines4.5 KB

1/**
2 * Load pull request metadata and changed files from GitHub REST.
3 */
4​
5import type { GitHubClient } from '../../scripts/maintainer/github.ts'
6​
7function isRecord(value: unknown): value is Record<string, unknown> {
8 return typeof value === 'object' && value !== null && !Array.isArray(value)
9}
10​
11function fail(path: string, message: string): never {
12 throw new Error(`GitHub GET ${path} ${message}`)
14​
15function parseAuthorLogin(user: unknown, path: string) {
16 if (user === null) return null
17 if (!isRecord(user) || typeof user.login !== 'string') {
18 fail(path, 'is missing user.login')
19 }
20 return user.login
22​
23function parseLabelNames(labels: unknown, path: string) {
24 if (!Array.isArray(labels)) fail(path, 'is missing labels')
25 const names = []
26 for (const label of labels) {
27 if (!isRecord(label) || typeof label.name !== 'string') {
28 fail(path, 'has a label without name')
29 }
30 names.push(label.name)
31 }
32 return names
34​
35function parseHead(head: unknown, path: string) {
36 if (!isRecord(head)) fail(path, 'is missing head')
37 if (typeof head.sha !== 'string') fail(path, 'is missing head.sha')
38 if (typeof head.ref !== 'string') fail(path, 'is missing head.ref')
39 if (!isRecord(head.repo)) fail(path, 'is missing head.repo')
40 if (typeof head.repo.full_name !== 'string') {
41 fail(path, 'is missing head.repo.full_name')
42 }
43 return {
44 sha: head.sha,
45 ref: head.ref,
46 repo: head.repo.full_name,
47 }
49​
50function parsePull(raw: unknown, path: string) {
51 if (!isRecord(raw)) fail(path, 'did not return an object')
52 if (typeof raw.number !== 'number') fail(path, 'is missing number')
53 if (typeof raw.title !== 'string') fail(path, 'is missing title')
54 if (raw.body !== null && typeof raw.body !== 'string') {
55 fail(path, 'is missing body')
56 }
57 if (typeof raw.html_url !== 'string') fail(path, 'is missing html_url')
58 if (typeof raw.draft !== 'boolean') fail(path, 'is missing draft')
59 if (typeof raw.maintainer_can_modify !== 'boolean') {
60 fail(path, 'is missing maintainer_can_modify')
61 }
62 const head = parseHead(raw.head, path)
63 return {
64 number: raw.number,
65 title: raw.title,
66 body: raw.body,
67 htmlUrl: raw.html_url,
68 isDraft: raw.draft,
69 authorLogin: parseAuthorLogin(raw.user, path),
70 headSha: head.sha,
71 headRef: head.ref,
72 headRepo: head.repo,
73 maintainerCanModify: raw.maintainer_can_modify,
74 labels: parseLabelNames(raw.labels, path),
75 }
77​
78function parseFiles(raw: unknown, path: string) {
79 if (!Array.isArray(raw)) fail(path, 'did not return an array')
80 const files = []
81 for (const item of raw) {
82 if (!isRecord(item) || typeof item.filename !== 'string') {
83 fail(path, 'has a file without filename')
84 }
85 const patch = typeof item.patch === 'string' ? item.patch : null
86 files.push({ path: item.filename, patch })
87 }
88 return files
90​
91/**
92 * List changed files for a pull request.
93 *
94 * `patch` is null when GitHub omits it (binary or too large).
95 *
96 * @param client GitHub REST client
97 * @param repo owner/name, for example `TanStack/ai`
98 * @param number pull request number
99 */
100export async function fetchPullRequestFiles(
101 client: GitHubClient,
102 repo: string,
103 number: number,
104) {
105 const perPage = 100
106 const all = []
107 let page = 1
108 while (true) {
109 const path = `/repos/${repo}/pulls/${number}/files?per_page=${perPage}&page=${page}`
110 const batch = parseFiles(await client.rest('GET', path), path)
111 all.push(...batch)
112 if (batch.length < perPage) break
113 page += 1
114 }
115 return all
117​
118/**
119 * Unified-ish diff from each file's `patch` field.
120 *
121 * @param client GitHub REST client
122 * @param repo owner/name, for example `TanStack/ai`
123 * @param number pull request number
124 */
125export async function fetchPullRequestDiff(
126 client: GitHubClient,
127 repo: string,
128 number: number,
129) {
130 // ponytail: GitHubClient always Accepts JSON, so the diff is files[].patch joined
131 const files = await fetchPullRequestFiles(client, repo, number)
132 const parts = []
133 for (const file of files) {
134 if (file.patch === null) continue
135 parts.push(`--- a/${file.path}\n${file.patch}`)
136 }
137 return parts.join('\n')
139​
140/**
141 * Load pull request metadata and changed files.
142 *
143 * @param client GitHub REST client
144 * @param repo owner/name, for example `TanStack/ai`
145 * @param number pull request number
146 */
147export async function fetchPullRequest(
148 client: GitHubClient,
149 repo: string,
150 number: number,
151) {
152 const path = `/repos/${repo}/pulls/${number}`
153 const pull = parsePull(await client.rest('GET', path), path)
154 const files = await fetchPullRequestFiles(client, repo, number)
155 return { ...pull, files }