forked from pbkit/pbkit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
152 lines (137 loc) · 3.79 KB
/
index.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
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
import * as path from "https://deno.land/std@0.93.0/path/mod.ts";
import { parse as parseYaml } from "https://deno.land/std@0.93.0/encoding/yaml.ts";
import { getHomeDir } from "../env.ts";
export interface GhHosts {
[hostname: string]: GhHost;
}
export interface GhHost {
user: string;
oauth_token: string;
git_protocol: "ssh" | "https";
}
export function getDefaultGhConfigPath(configFile = ".") {
const ghConfigPath = path.resolve(getHomeDir(), ".config/gh", configFile);
return ghConfigPath;
}
export async function readGhHosts(
hostsFilePath = getDefaultGhConfigPath("hosts.yml"),
): Promise<GhHosts> {
const hostsFile = await Deno.readTextFile(hostsFilePath);
return parseYaml(hostsFile) as GhHosts;
}
export async function getToken(): Promise<string> {
try {
const ghHosts = await readGhHosts();
const token = ghHosts["github.com"].oauth_token;
if (token) return token;
} catch {}
throw new GithubNotLoggedInError();
}
export interface GhCommit {
sha: string;
url: string;
}
export interface GhRev {
name: string;
commit: GhCommit;
}
export interface GhTag extends GhRev {
zipball_url: string;
tarball_url: string;
node_id: string;
}
export interface GhBranch extends GhRev {
protected: string;
protection: {
required_status_checks: {
enforcement_level: string;
contexts: string[];
};
};
protected_url: string;
}
export interface GhRepo {
name: string;
}
export interface FetchRepoConfig {
token?: string;
user: string;
repo: string;
}
export async function fetchRepo(config: FetchRepoConfig): Promise<GhRepo> {
const res = await fetchRepoBase(config, "");
return await res.json() as GhRepo;
}
export interface FetchCommitStatusConfig extends FetchRepoConfig {
rev: string;
}
export async function fetchCommitStatus(
config: FetchCommitStatusConfig,
): Promise<GhCommit> {
const { rev } = config;
const res = await fetchRepoBase(config, `/commits/${rev}/status`);
return await res.json() as GhCommit;
}
export interface FetchTagsConfig extends FetchRepoConfig {}
export async function fetchTags(config: FetchTagsConfig): Promise<GhTag[]> {
const res = await fetchRepoBase(config, "/tags");
return await res.json() as GhTag[];
}
export interface FetchBranchesConfig extends FetchRepoConfig {}
export async function fetchBranches(
config: FetchBranchesConfig,
): Promise<GhBranch[]> {
const res = await fetchRepoBase(config, "/branches");
return await res.json() as GhBranch[];
}
export interface FetchArchiveConfig extends FetchRepoConfig {
type: "tgz" | "zip";
rev: string;
}
export async function fetchArchive(
config: FetchArchiveConfig,
): Promise<Response> {
const { type, rev } = config;
const archiveType = type === "tgz" ? "tarball" : "zipball";
return await fetchRepoBase(config, `/${archiveType}/${rev}`);
}
export class GithubNotLoggedInError extends Error {
constructor() {
super("Login required.");
}
}
export class GithubRepoNotFoundError extends Error {
constructor(user: string, repo: string) {
super(`Repository ${user}/${repo} is not found.`);
}
}
async function fetchRepoBase(
config: FetchRepoConfig,
apiPath: string,
): Promise<Response> {
const { token, user, repo } = config;
const headers = getFetchHeaders(token);
const res = await fetch(
`https://api.github.com/repos/${user}/${repo}${apiPath}`,
{ headers },
);
if (!res.ok) {
switch (res.status) {
case 401:
throw new GithubNotLoggedInError();
case 403:
case 404:
throw new GithubRepoNotFoundError(user, repo);
default:
throw res;
}
}
return res;
}
function getFetchHeaders(token?: string) {
const headers: Record<string, string> = {
Accept: "application/vnd.github.v3.raw",
};
if (token) headers.Authorization = "token " + token;
return headers;
}