-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub.ts
More file actions
134 lines (126 loc) · 3.39 KB
/
Copy pathgithub.ts
File metadata and controls
134 lines (126 loc) · 3.39 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
export class GitHubApiError extends Error {
constructor(
public status: number,
message: string,
) {
super(message);
}
}
async function gh<T>(
path: string,
token: string,
init?: RequestInit,
): Promise<T> {
const res = await fetch(`https://api.github.com${path}`, {
...init,
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
...(init?.headers ?? {}),
},
});
const body = (await res.json().catch(() => ({}))) as Record<string, unknown>;
if (!res.ok) {
throw new GitHubApiError(
res.status,
typeof body.message === "string"
? body.message
: `GitHub API ${res.status} ${res.statusText}`,
);
}
return body as T;
}
export type GhUser = { login: string; id: number; email?: string | null };
export const getGhUser = (token: string) => gh<GhUser>("/user", token);
export async function exchangeGhCode(params: {
code: string;
clientId: string;
clientSecret: string;
redirectUri: string;
}): Promise<{ access_token: string; scope: string; token_type: string }> {
const body = new URLSearchParams({
client_id: params.clientId,
client_secret: params.clientSecret,
code: params.code,
redirect_uri: params.redirectUri,
});
const res = await fetch("https://github.com/login/oauth/access_token", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/x-www-form-urlencoded",
},
body,
});
const data = (await res.json()) as {
access_token?: string;
scope?: string;
token_type?: string;
error?: string;
error_description?: string;
};
if (!data.access_token) {
throw new GitHubApiError(
400,
data.error_description ?? data.error ?? "GitHub OAuth exchange failed",
);
}
return {
access_token: data.access_token,
scope: data.scope ?? "",
token_type: data.token_type ?? "bearer",
};
}
export type GhRepo = {
id: number;
name: string;
full_name: string;
html_url: string;
default_branch: string;
owner: { login: string };
};
export async function forkRepo(
token: string,
sourceRepo: string,
newName?: string,
): Promise<GhRepo> {
const [owner, repo] = sourceRepo.split("/");
if (!owner || !repo) throw new Error(`Bad source repo "${sourceRepo}"`);
return gh<GhRepo>(`/repos/${owner}/${repo}/forks`, token, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(newName ? { name: newName } : {}),
});
}
export const getRepo = (token: string, fullName: string) =>
gh<GhRepo>(`/repos/${fullName}`, token);
export async function commitFile(
token: string,
fullRepo: string,
path: string,
content: string,
message: string,
branch = "main",
): Promise<void> {
let sha: string | undefined;
try {
const existing = await gh<{ sha: string }>(
`/repos/${fullRepo}/contents/${encodeURIComponent(path)}?ref=${branch}`,
token,
);
sha = existing.sha;
} catch (e) {
if (!(e instanceof GitHubApiError && e.status === 404)) throw e;
}
await gh(`/repos/${fullRepo}/contents/${encodeURIComponent(path)}`, token, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
message,
content: Buffer.from(content).toString("base64"),
branch,
...(sha ? { sha } : {}),
}),
});
}