generated from lukasbach/cli-ts-commander-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
115 lines (100 loc) · 4 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
#!/usr/bin/env node
import * as fs from 'fs';
import * as path from 'path';
import prompts from 'prompts';
import { Octokit as OriginalOctokit } from '@octokit/rest';
import { throttling } from '@octokit/plugin-throttling';
const Octokit = OriginalOctokit.plugin(throttling);
const throttle = {
onRateLimit: (retryAfter: number, options: any, octokit: any) => {
octokit.log.warn(`Request quota exhausted for request ${options.method} ${options.url}`);
if (options.request.retryCount === 10) {
// only retries once
octokit.log.info(`Retrying after ${retryAfter} seconds!`);
return true;
}
},
onAbuseLimit: (retryAfter: number, options: any, octokit: any) => {
// does not retry, only logs a warning
octokit.log.warn(`Abuse detected for request ${options.method} ${options.url}`);
},
};
(async () => {
const { auth, matcher } = await prompts([
{
type: 'text',
name: 'auth',
message:
'Github API Key. You can generate the token at https://github.com/settings/tokens/new. ' +
'The token needs the `user` permission. Make sure the token has access to the organizations, whose ' +
'repos you want to unwatch, which might require you to enable SSO for that organization.',
},
{
type: 'text',
name: 'matcher',
message:
'Provide a regex that matches the repos that you want to unsubscribe. ' +
'Repos are named `organization/repo-name`. You will be able to review the list ' +
'of matched repos before they are unwatched. You can for example use the input "github/*" ' +
'to unsubscribe from all repos within the Github organization.',
},
]);
console.log('Fetching your watched repos from Github...');
const octokit = new Octokit({ auth, throttle });
const watchedRepos: string[] = await octokit.paginate('GET /user/subscriptions', response =>
response.data.map(repo => repo.full_name)
);
const matcherRegex = new RegExp(matcher);
const matchedRepos = watchedRepos.filter(repo => matcherRegex.test(repo));
console.log(
`You are currently watching ${watchedRepos.length} repos, from which the following ${matchedRepos.length} ` +
`repos match your regex: ${matchedRepos.join(', ')}`
);
let unsubscribeRepos: string[] = [];
const { reviewRepos } = await prompts([
{
type: 'select',
name: 'reviewRepos',
message: `Do you want to unsubscribe from all of them, or review your selection?`,
choices: [
{ value: false, title: 'Unsubscribe from all' },
{ value: true, title: 'Review my selection' },
],
},
]);
if (reviewRepos) {
const { unsubscribeReposSelection } = await prompts([
{
type: 'multiselect',
name: 'unsubscribeReposSelection',
message:
`You are currently watching ${watchedRepos.length} repos, ${matchedRepos.length} of which match your regex. ` +
'Choose which of those repositories you want to unsubscribe to.',
choices: matchedRepos.map(repo => ({ title: repo, value: repo, selected: true })),
},
]);
unsubscribeRepos = unsubscribeReposSelection;
} else {
unsubscribeRepos = matchedRepos;
}
console.log(`You've selected the following ${unsubscribeRepos.length} repos: ${unsubscribeRepos.join(', ')}`);
const { confirmation } = await prompts([
{
type: 'confirm',
name: 'confirmation',
message:
`Are you sure you want to unsubscribe from the ${unsubscribeRepos.length} repositories listed above? ` +
'This is the final confirmation, after this the repositories will be unwatched. There is no undo!',
},
]);
if (confirmation) {
console.log('Unwatching repositories...');
for (const fullRepo in unsubscribeRepos) {
const [owner, repo] = fullRepo.split('/');
await octokit.request('DELETE /repos/{owner}/{repo}/subscription', { owner, repo });
console.log(`Unwatched ${fullRepo}`);
}
} else {
console.log('Aborted. Your watched repositories were not changed.');
}
})();