-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathgithub.js
More file actions
158 lines (119 loc) · 3.3 KB
/
Copy pathgithub.js
File metadata and controls
158 lines (119 loc) · 3.3 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
import { createOAuthDeviceAuth } from '@octokit/auth-oauth-device';
import { request } from '@octokit/request';
const CLIENT_ID_PROD = 'ed7c193c5b64ee06192a';
const CLIENT_ID = process.env.DEV ? process.env.CLIENT_ID : CLIENT_ID_PROD;
const CLIENT_TYPE = 'oauth-app';
const CLIENT_SCOPES = ['delete_repo', 'repo', 'codespace'];
const API_PAGINATION = 100;
const API_AFFILIATION = 'owner, collaborator';
async function auth(onVerificationCode) {
const auth = createOAuthDeviceAuth({
clientType: CLIENT_TYPE,
clientId: CLIENT_ID,
scopes: CLIENT_SCOPES,
onVerification: onVerificationCode,
});
const { token } = await auth({ type: 'oauth' });
setToken(token);
return token;
}
async function getRepositories() {
let page = 1;
const repos = [];
while (true) {
const res = await apiCall('GET', '/user/repos', page);
const reposCurrentPage = res.data;
if (reposCurrentPage.length === 0) break;
repos.push(...reposCurrentPage);
page++;
}
return repos;
}
async function getCodespaces() {
let page = 1;
const codespaces = [];
while (true) {
const res = await apiCall('GET', '/user/codespaces', page);
const codespacesCurrentPage = res.data.codespaces;
if (codespacesCurrentPage.length === 0) break;
codespaces.push(...codespacesCurrentPage);
page++;
}
return codespaces;
}
function checkPermissions(authScopes, clientScopes) {
if (authScopes.length < clientScopes.length) {
return false;
}
return clientScopes.every((scope) => {
return authScopes.includes(scope);
});
}
async function deleteRepository(repository) {
const res = await apiCall('DELETE', `/repos/${repository}`);
if (res.status !== 204) return false;
return true;
}
async function archiveRepository(repository) {
const res = await apiCall('PATCH', `/repos/${repository}`, undefined, {
archived: true,
});
if (res.status !== 200) return false;
return true;
}
async function deleteCodespace(codespace) {
const res = await apiCall('DELETE', `/user/codespaces/${codespace}`);
if (res.status !== 204) return false;
return true;
}
function getAuthHeader() {
return `token ${process.env.GITHUB_TOKEN}`;
}
function setToken(token) {
if (!token) return false;
return (process.env.GITHUB_TOKEN = token);
}
async function apiCall(method, endpoint, page, data) {
const query = `${method} ${endpoint}`;
const params = {
headers: { authorization: getAuthHeader() },
affiliation: API_AFFILIATION,
per_page: API_PAGINATION,
page: page,
...data,
};
try {
const res = await request(query, params);
const scopes = (res.headers['x-oauth-scopes'] || '').split(', ');
if (!checkPermissions(scopes, CLIENT_SCOPES)) throw new ScopesError();
return res;
} catch (error) {
if (error.status === 401) throw new AuthError();
throw error;
}
}
class AuthError extends Error {
constructor(message) {
super(message);
this.message = message || 'Unauthorized';
this.code = 401;
}
}
class ScopesError extends Error {
constructor(message) {
super(message);
this.message = message || 'Client and token scopes missmatch';
}
}
export default {
auth,
getRepositories,
getCodespaces,
deleteRepository,
archiveRepository,
deleteCodespace,
checkPermissions,
setToken,
AuthError,
ScopesError,
};