-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunwatch.py
66 lines (57 loc) · 2.08 KB
/
unwatch.py
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
import argparse
import json
import os
# third imports
import requests
def read_safelist():
safelist = []
if os.path.exists('safelist.txt'):
with open('safelist.txt') as txt_file:
safelist.extend(repo.strip() for repo in txt_file.readlines())
if os.path.exists('safelist.json'):
with open('safelist.json') as json_file:
safelist.extend(repo.strip() for repo in json.load(json_file))
return set([repo for repo in safelist if repo])
def repositories(org, token):
url = 'https://api.github.com/user/subscriptions'
params = {'page': 1, 'per_page': 100}
headers = {'Authorization': f'token {token}'}
repos = []
while True:
res = requests.get(url, params=params, headers=headers)
page = res.json()
repos.extend(page)
if len(page) < 100:
break
params['page'] += 1
return [repo for repo in repos if repo['owner']['login'] == org]
def unwatch_org(org, token):
repos = repositories(org, token)
safelist = read_safelist()
headers = {'Authorization': f'token {token}'}
print(f'found {len(repos)} repositories')
total = 0
for repo in repos:
if repo['name'] not in safelist:
res = requests.delete(repo['subscription_url'], headers=headers)
if res.status_code > 399:
print(f'failed to unwatch {repo["name"]}')
continue
total += 1
print(f'successfully unwatch {repo["name"]}')
print(f'successfully unwatch {total} repositories')
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Unwatch/unsubscribe in all repositories of one '
'organization ignoring the repositories in the safelist'
)
parser.add_argument('org', help='Organization')
parser.add_argument(
'--token',
default=os.environ.get('GITHUB_TOKEN'),
help='Authentication token'
)
args = parser.parse_args()
if args.token is None:
raise SystemExit('necessary the authentication token')
unwatch_org(args.org, args.token)