-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
54 lines (54 loc) · 1.49 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
/**
* stars or unstars the passed repository based on the `unstar` option
*
* @param {import('@octoherd/cli').Octokit} octokit
* @param {import('@octoherd/cli').Repository} repository
* @param { {unstar?: boolean} } options Custom user options passed to the CLI
*/
export async function script(octokit, repository, options) {
const method = options.unstar ? "DELETE" : "PUT";
const id = repository.id;
const owner = repository.owner.login;
const repo = repository.name;
// https://docs.github.com/en/rest/reference/activity#check-if-a-repository-is-starred-by-the-authenticated-user
const isStarred = await octokit
.request("GET /user/starred/{owner}/{repo}", {
owner,
repo,
})
.then(
() => true,
() => false
);
if ((options.unstar && !isStarred) || (!options.unstar && isStarred)) {
octokit.log.debug(
{
change: 0,
id,
owner,
repo,
},
"No change for %s",
repository.html_url
);
return;
}
// https://docs.github.com/en/rest/reference/activity#star-a-repository-for-the-authenticated-user
// https://docs.github.com/en/rest/reference/activity#unstar-a-repository-for-the-authenticated-user
await octokit.request("/user/starred/{owner}/{repo}", {
method,
owner,
repo,
});
octokit.log.info(
{
change: options.unstar ? -1 : 1,
id,
owner,
repo,
},
"Star %s %s",
options.unstar ? "removed from" : "added to",
repository.html_url
);
}