-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.ts
executable file
·99 lines (88 loc) · 2.41 KB
/
api.ts
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
import { readSecrets } from "$sb/lib/secrets_page.ts";
export async function getToken(): Promise<string | undefined> {
try {
const [token] = await readSecrets(["githubToken"]);
return token;
} catch {
console.error("No github-config page found, using default config");
return undefined;
}
}
export class GithubApi {
constructor(private token?: string) {}
async apiCall(url: string, options: any = {}): Promise<any> {
const res = await fetch(url, {
...options,
headers: {
Authorization: this.token ? `token ${this.token}` : undefined,
},
});
if (res.status < 200 || res.status >= 300) {
throw new Error(await res.text());
}
return res.json();
}
listEvents(username: string): Promise<any[]> {
return this.apiCall(
`https://api.github.com/users/${username}/events?per_page=100`,
);
}
listPulls(
repo: string,
state = "all",
sort = "updated",
): Promise<any[]> {
return this.apiCall(
`https://api.github.com/repos/${repo}/pulls?state=${state}&sort=${sort}&direction=desc&per_page=100`,
);
}
searchIssues(
query: string,
sort = "",
): Promise<{ items: any[] }> {
query = encodeURIComponent(query);
return this.apiCall(
`https://api.github.com/search/issues?q=${query}&sort=${sort}&direction=desc&per_page=100`,
);
}
listNotifications(): Promise<any[]> {
return this.apiCall(`https://api.github.com/notifications?per_page=100`);
}
async createGist(
description: string,
isPublic: boolean,
files: Record<string, { content: string }>,
): Promise<string> {
const result = await this.apiCall(`https://api.github.com/gists`, {
method: "POST",
body: JSON.stringify({
description,
public: isPublic,
files,
}),
});
return result.id;
}
updateGist(
id: string,
description: string,
isPublic: boolean,
files: Record<string, { content: string }>,
) {
return this.apiCall(`https://api.github.com/gists/${id}`, {
method: "PATCH",
body: JSON.stringify({
description,
public: isPublic,
files,
}),
});
}
async getGist(id: string): Promise<Record<string, any>> {
const resp = await this.apiCall(`https://api.github.com/gists/${id}`);
return resp.files;
}
static async fromConfig(): Promise<GithubApi> {
return new GithubApi(await getToken());
}
}