Repository navigation
Expand file tree
/
Copy pathgithub.ts
More file actions
222 lines (198 loc) · 9.83 KB
/
Copy pathgithub.ts
File metadata and controls
222 lines (198 loc) · 9.83 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
import type { BranchInfo, CommitInfo, FetchFunction, RepositoryInfo, ScanOptions, TreeEntry } from "./types.js";
const DEFAULT_API_URL = "https://api.github.com";
const PAGE_SIZE = 100;
const NOT_FOUND_STATUS = 404;
const LIMIT_STATUSES = new Set([403, 429]);
const LIMIT_HINT = " The rate limit may be reached: set GITHUB_TOKEN, for example GITHUB_TOKEN=$(gh auth token).";
const NO_TOKEN_HINT = " Without a token GitHub allows 60 requests per hour; with GITHUB_TOKEN=$(gh auth token) it allows 5,000.";
const MILLISECONDS_PER_SECOND = 1000;
/** The default longest wait, in seconds, for the rate limit to reset before giving up. */
export const DEFAULT_MAX_WAIT_SECONDS = 300;
const MAX_ATTEMPTS = 4;
/** The extra wait after the announced reset, because the clocks of GitHub and of the computer differ a little. */
const CLOCK_MARGIN_MILLISECONDS = 1000;
/** The error thrown when the GitHub API refuses a request or cannot be reached. */
export class GitHubError extends Error {
readonly status: number | undefined;
/** Builds the error with its message and, when GitHub answered, its HTTP status. */
constructor(message: string, status?: number) {
super(message);
this.name = "GitHubError";
this.status = status;
}
}
/** A small read-only client for the GitHub REST API. */
export interface GitHubClient {
getJson: <T>(path: string) => Promise<T>;
}
/** Builds the headers sent with every request, with the token when one is given. */
function buildHeaders(token: string | undefined): Record<string, string> {
const headers: Record<string, string> = {
Accept: "application/vnd.github+json",
"User-Agent": "wormsign",
"X-GitHub-Api-Version": "2022-11-28",
};
if (token !== undefined && token !== "") {
headers.Authorization = `Bearer ${token}`;
}
return headers;
}
/** Sends one GET request and returns the response, or throws a GitHubError when the network fails. */
async function sendRequest(fetchFunction: FetchFunction, url: string, headers: Record<string, string>): Promise<Response> {
try {
return await fetchFunction(url, { headers });
} catch (error) {
const reason = error instanceof Error ? error.message : String(error);
throw new GitHubError(`GitHub cannot be reached: ${reason}`);
}
}
/** Tells the caller how many requests GitHub still allows, when GitHub sends the numbers. */
function reportRateLimit(response: Response, onRateLimit: ScanOptions["onRateLimit"]): void {
const remaining = response.headers.get("x-ratelimit-remaining");
const limit = response.headers.get("x-ratelimit-limit");
if (onRateLimit === undefined || remaining === null || limit === null) {
return;
}
onRateLimit(Number(remaining), Number(limit));
}
/** Returns how many milliseconds GitHub asks to wait before the next request, or undefined when it is not a rate limit. */
export function readWaitMilliseconds(response: Response, now: number): number | undefined {
if (!LIMIT_STATUSES.has(response.status)) {
return undefined;
}
const retryAfter = response.headers.get("retry-after");
if (retryAfter !== null) {
return Number(retryAfter) * MILLISECONDS_PER_SECOND;
}
const reset = response.headers.get("x-ratelimit-reset");
if (response.headers.get("x-ratelimit-remaining") !== "0" || reset === null) {
return undefined;
}
return Math.max(0, Number(reset) * MILLISECONDS_PER_SECOND - now) + CLOCK_MARGIN_MILLISECONDS;
}
/** Waits for the given number of milliseconds. */
function sleep(milliseconds: number): Promise<void> {
return new Promise<void>(function waitThenResolve(resolve: () => void): void {
setTimeout(resolve, milliseconds);
});
}
/** Builds the error of a refused request, with a hint on the rate limit when it may be the cause. */
function buildRefusalError(response: Response, path: string, hasToken: boolean): GitHubError {
const isLimit = LIMIT_STATUSES.has(response.status);
const hint = isLimit && !hasToken ? NO_TOKEN_HINT : "";
const limitHint = isLimit && hasToken ? LIMIT_HINT : hint;
return new GitHubError(`GitHub answered ${response.status} for ${path}.${limitHint}`, response.status);
}
/** Creates a client that reads the GitHub REST API with the given options, and waits when the rate limit is reached. */
export function createGitHubClient(options: ScanOptions): GitHubClient {
const fetchFunction = options.fetch ?? globalThis.fetch;
const apiUrl = options.apiUrl ?? DEFAULT_API_URL;
const headers = buildHeaders(options.token);
const hasToken = options.token !== undefined && options.token !== "";
const maxWait = (options.maxWaitSeconds ?? DEFAULT_MAX_WAIT_SECONDS) * MILLISECONDS_PER_SECOND;
const wait = options.sleep ?? sleep;
/** Reads one API path and returns its JSON body, or throws a GitHubError with the HTTP status. */
async function getJson<T>(path: string): Promise<T> {
for (let attempt = 1; attempt < MAX_ATTEMPTS; attempt += 1) {
const response = await sendRequest(fetchFunction, `${apiUrl}${path}`, headers);
reportRateLimit(response, options.onRateLimit);
if (response.ok) {
return (await response.json()) as T;
}
const waitMilliseconds = readWaitMilliseconds(response, Date.now());
if (waitMilliseconds === undefined || waitMilliseconds > maxWait) {
throw buildRefusalError(response, path, hasToken);
}
options.onWait?.(Math.ceil(waitMilliseconds / MILLISECONDS_PER_SECOND));
await wait(waitMilliseconds);
}
throw new GitHubError(`GitHub kept refusing ${path} after waiting three times for the rate limit.${LIMIT_HINT}`);
}
return { getJson };
}
/** Lists every public repository of a user or an organization, page by page. */
export async function listRepositories(client: GitHubClient, owner: string): Promise<RepositoryInfo[]> {
const repositories: RepositoryInfo[] = [];
let page = 1;
let pageIsFull = true;
while (pageIsFull) {
const path = `/users/${encodeURIComponent(owner)}/repos?per_page=${PAGE_SIZE}&page=${page}&type=owner`;
const pageOfRepositories = await client.getJson<RepositoryInfo[]>(path);
repositories.push(...pageOfRepositories);
pageIsFull = pageOfRepositories.length === PAGE_SIZE;
page += 1;
}
return repositories;
}
/** Reads one repository written owner/name. */
export async function getRepository(client: GitHubClient, fullName: string): Promise<RepositoryInfo> {
return client.getJson<RepositoryInfo>(`/repos/${fullName}`);
}
/** Lists the branches of a repository, at most the given number, and always includes the default branch. */
export async function listBranches(client: GitHubClient, fullName: string, maxBranches: number, defaultBranch?: string): Promise<BranchInfo[]> {
const pageSize = Math.min(maxBranches, PAGE_SIZE);
const branches = await client.getJson<BranchInfo[]>(`/repos/${fullName}/branches?per_page=${pageSize}`);
const firstBranches = branches.slice(0, maxBranches);
const repositoryIsEmpty = firstBranches.length === 0;
const defaultBranchIsListed = defaultBranch === undefined || repositoryIsEmpty || firstBranches.some(hasName, defaultBranch);
if (defaultBranchIsListed) {
return firstBranches;
}
const defaultHead = await client.getJson<BranchInfo>(`/repos/${fullName}/branches/${encodeURIComponent(defaultBranch)}`);
return [defaultHead, ...firstBranches.slice(0, maxBranches - 1)];
}
/** Tells whether a branch has the name given as this. */
function hasName(this: string, branch: BranchInfo): boolean {
return branch.name === this;
}
/** Reads the full file tree of a commit, without downloading any file; a tree that GitHub cannot find is empty. */
export async function getTree(client: GitHubClient, fullName: string, sha: string): Promise<TreeEntry[]> {
try {
const tree = await client.getJson<{ tree: TreeEntry[] }>(`/repos/${fullName}/git/trees/${sha}?recursive=1`);
return tree.tree;
} catch (error) {
const treeIsMissing = error instanceof GitHubError && error.status === NOT_FOUND_STATUS;
if (treeIsMissing) {
return [];
}
throw error;
}
}
/** Turns the base64 content sent by GitHub into text, the same way in Node.js and in a browser. */
export function decodeBase64Text(content: string): string {
const binary = atob(content.replace(/\s/gu, ""));
const bytes = Uint8Array.from(binary, readCharacterCode);
return new TextDecoder().decode(bytes);
}
/** Returns the code of one character of a binary string, which is the value of one byte. */
function readCharacterCode(character: string): number {
return character.charCodeAt(0);
}
/** Reads a base64 text answer of the API, and returns undefined when it has no content or is missing. */
async function getBase64Text(client: GitHubClient, url: string): Promise<string | undefined> {
try {
const file = await client.getJson<{ content?: string }>(url);
if (typeof file.content !== "string") {
return undefined;
}
return decodeBase64Text(file.content);
} catch (error) {
const fileIsMissing = error instanceof GitHubError && error.status === NOT_FOUND_STATUS;
if (fileIsMissing) {
return undefined;
}
throw error;
}
}
/** Reads a small text file at a commit and returns its text, or undefined when the file is missing or is a symbolic link. */
export async function getFileText(client: GitHubClient, fullName: string, path: string, sha: string): Promise<string | undefined> {
return getBase64Text(client, `/repos/${fullName}/contents/${encodeURI(path)}?ref=${sha}`);
}
/** Reads a blob by its id and returns its text, which for a symbolic link is the path of its target. */
export async function getBlobText(client: GitHubClient, fullName: string, sha: string): Promise<string | undefined> {
return getBase64Text(client, `/repos/${fullName}/git/blobs/${sha}`);
}
/** Reads one commit of a repository. */
export async function getCommit(client: GitHubClient, fullName: string, sha: string): Promise<CommitInfo> {
return client.getJson<CommitInfo>(`/repos/${fullName}/commits/${sha}`);
}