Open History to see the last commit
1/**
2 * Load and validate pull request metadata from GitHub REST. Changed files
3 * come from the Git snapshot in `git.ts`.
4 */
5
6import type { GitHubClient } from '../../scripts/maintainer/github.ts'
7
8function isRecord(value: unknown): value is Record<string, unknown> {
9 return typeof value === 'object' && value !== null && !Array.isArray(value)
10}
11
12function fail(path: string, message: string): never {
13 throw new Error(`GitHub GET ${path} ${message}`)
14}
15
16function parseAuthorLogin(user: unknown, path: string) {
17 if (user === null) return null
18 if (!isRecord(user) || typeof user.login !== 'string') {
19 fail(path, 'is missing user.login')
20 }
21 return user.login
22}
23
24function parseLabelNames(labels: unknown, path: string) {
25 if (!Array.isArray(labels)) fail(path, 'is missing labels')
26 const names = []
27 for (const label of labels) {
28 if (!isRecord(label) || typeof label.name !== 'string') {
29 fail(path, 'has a label without name')
30 }
31 names.push(label.name)
32 }
33 return names
34}
35
36function parseHead(head: unknown, path: string) {
37 if (!isRecord(head)) fail(path, 'is missing head')
38 if (typeof head.sha !== 'string' || !/^[0-9a-f]{40}$/i.test(head.sha)) {
39 fail(path, 'is missing a valid head.sha')
40 }
41 if (typeof head.ref !== 'string') fail(path, 'is missing head.ref')
42 if (!isRecord(head.repo)) fail(path, 'is missing head.repo')
43 if (typeof head.repo.full_name !== 'string') {
44 fail(path, 'is missing head.repo.full_name')
45 }
46 return {
47 sha: head.sha,
48 ref: head.ref,
49 repo: head.repo.full_name,
50 }
51}
52
53function parseBase(base: unknown, path: string) {
54 if (!isRecord(base)) fail(path, 'is missing base')
55 if (typeof base.sha !== 'string' || !/^[0-9a-f]{40}$/i.test(base.sha)) {
56 fail(path, 'is missing a valid base.sha')
57 }
58 if (typeof base.ref !== 'string') fail(path, 'is missing base.ref')
59 if (!isRecord(base.repo) || typeof base.repo.full_name !== 'string')
60 fail(path, 'is missing base.repo.full_name')
61 return { sha: base.sha, ref: base.ref, repo: base.repo.full_name }
62}
63
64function parsePull(raw: unknown, path: string) {
65 if (!isRecord(raw)) fail(path, 'did not return an object')
66 if (typeof raw.number !== 'number') fail(path, 'is missing number')
67 if (typeof raw.title !== 'string') fail(path, 'is missing title')
68 if (raw.body !== null && typeof raw.body !== 'string') {
69 fail(path, 'is missing body')
70 }
71 if (typeof raw.html_url !== 'string') fail(path, 'is missing html_url')
72 if (typeof raw.draft !== 'boolean') fail(path, 'is missing draft')
73 if (typeof raw.maintainer_can_modify !== 'boolean') {
74 fail(path, 'is missing maintainer_can_modify')
75 }
76 const head = parseHead(raw.head, path)
77 const base = parseBase(raw.base, path)
78 return {
79 number: raw.number,
80 title: raw.title,
81 body: raw.body,
82 htmlUrl: raw.html_url,
83 isDraft: raw.draft,
84 authorLogin: parseAuthorLogin(raw.user, path),
85 baseSha: base.sha,
86 baseRef: base.ref,
87 baseRepo: base.repo,
88 state: raw.state,
89 headSha: head.sha,
90 headRef: head.ref,
91 headRepo: head.repo,
92 maintainerCanModify: raw.maintainer_can_modify,
93 labels: parseLabelNames(raw.labels, path),
94 }
95}
96
97/** Load pull metadata. Changed files come only from the fixed Git snapshot. */
98export async function fetchPullRequest(
99 client: GitHubClient,
100 repo: string,
101 number: number,
102) {
103 const path = `/repos/${repo}/pulls/${number}`
104 const pull = parsePull(await client.rest('GET', path), path)
105 return pull
106}