-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
68 lines (65 loc) · 1.89 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
58
59
60
61
62
63
64
65
66
67
68
/**
* An octoherd script to watch, unwatch or ignore repositories
*
* @param {import('@octoherd/cli').Octokit} octokit
* @param {import('@octoherd/cli').Repository} repository
* @param {{unwatch?: boolean, ignore?: boolean}} options Custom user options passed to the CLI
*/
export async function script(octokit, repository, { unwatch, ignore }) {
if (unwatch && ignore) {
throw new Error("Combination of flags not allowed. Use either --unwatch or --ignore, not both")
}
const subscribe = !ignore && !unwatch
const method = unwatch ? "DELETE" : "PUT";
const id = repository.id;
const owner = repository.owner.login;
const repo = repository.name;
// https://docs.github.com/en/rest/reference/activity#get-a-repository-subscription
const { subscribed, ignored } = await octokit
.request("GET /repos/{owner}/{repo}/subscription", {
owner,
repo,
})
.then(
({ data: { subscribed, ignored } }) => ({ subscribed, ignored }),
() => ({ subscribed: false, ignored: false })
);
const unsubscribed = !subscribed && !ignored
octokit.log.debug(
{
change: 0,
},
"subscribed: %s, ignored: %s, unwatch: %s, ignore: %s",
subscribed, ignored, !!unwatch, !!ignore
);
if ((subscribed && subscribe) || (ignored && ignore) || (unsubscribed && unwatch)) {
octokit.log.debug(
{
change: 0,
},
"No change for %s",
repository.html_url
);
return;
}
// https://docs.github.com/en/rest/reference/activity#set-a-repository-subscription
// https://docs.github.com/en/rest/reference/activity#delete-a-repository-subscription
let bodyReq
if (!unwatch) {
bodyReq = ignore ? { ignored: true } : { subscribed: true }
}
await octokit.request("/repos/{owner}/{repo}/subscription", {
method,
owner,
repo,
...bodyReq
});
octokit.log.info(
{
change: unwatch ? -1 : 1,
},
"Subscription %s %s",
unwatch ? "removed from" : "added to",
repository.html_url
);
}