-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
57 lines (51 loc) · 1.14 KB
/
script.js
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
// @ts-check
/**
* Replace all repository topics
*
* @param {import('@octoherd/cli').Octokit} octokit
* @param {import('@octoherd/cli').Repository} repository
* @param {{ topics?: string[] }} options
*/
export async function script(octokit, repository, options = {}) {
const owner = repository.owner.login;
const repo = repository.name;
let topics = options.topics || []
if (!Array.isArray(topics)) {
topics = [topics]
}
if (repository.archived) {
octokit.log.warn(`${repository.html_url} is archived`);
return;
}
try {
await octokit.request(
"PUT /repos/{owner}/{repo}/topics",
{
owner,
repo,
names: topics,
mediaType: {
previews: ["mercy"]
}
}
);
octokit.log.info(`Topics ${topics.length? 'added in' : 'removed from'} repository: ${repository.html_url}`);
} catch (e) {
let reason;
switch (e.status) {
case 404:
reason = 'Resource not found'
break;
case 415:
reason = 'Unsupported Media Type'
break;
case 422:
reason = '422 Unprocessable Entity'
break;
default:
reason = e
break;
}
octokit.log.warn(`${reason}: ${repository.html_url}`);
}
}