-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsuspend_non_users.py
76 lines (63 loc) · 2.57 KB
/
suspend_non_users.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
67
68
69
70
71
72
73
74
75
76
import os
import sys
from github3 import enterprise_login
from requests import put
class Suspender:
def __init__(self, url, access_token, should_verify=False):
self.ghe_url = url
self.token = access_token
self.should_verify = should_verify
self.hub = enterprise_login(token=access_token, url=url)
def find_non_users(self):
def _find_non_users_gen():
for user in self.hub.all_users():
if user.type != 'Organization':
user_detail = self.hub.user(user.login)
if not self.is_suspended(user_detail) and \
self.user_has_no_activity(user_detail):
yield user.login
return _find_non_users_gen()
def is_suspended(self, user_detail):
return user_detail.as_dict().get('suspended_at') is not None
def user_has_no_activity(self, user_detail):
return 0 == (user_detail.disk_usage +
user_detail.public_repos_count +
user_detail.public_gists +
user_detail.total_private_repos +
user_detail.total_private_gists)
def suspend_user(self, user):
return put(self.ghe_url + ('/' if self.ghe_url[-1] != '/' else '')
+ 'api/v3/'
+ 'users/{0}/suspended'.format(user)
+ '?access_token={0}'.format(self.token))
def suspend_non_users(self):
for user in self.find_non_users():
if self.should_verify:
print 'Should {0} be suspended? [Y/n]'.format(user)
answer = raw_input()
if answer == 'y' or answer == 'Y':
self.suspend_user(user)
print 'Suspended {0}'.format(user)
else:
print 'Suspended {0}'.format(user)
self.suspend_user(user)
if __name__ == '__main__':
should_verify = False
usage = 'usage: suspend_non_users.py <ghe_url> [option]\n\nOptions:\n-i\tVerify before suspending each user'
if len(sys.argv) < 2:
print usage
sys.exit(1)
ghe_url = sys.argv[1]
token = os.environ.get('GHE_ACCESS_TOKEN')
if len(sys.argv) == 3:
if sys.argv[2] == '-i':
should_verify = True
else:
print usage
sys.exit(1)
if token:
s = Suspender(ghe_url, token, should_verify)
s.suspend_non_users()
else:
sys.stderr.write('Please provide your access token in $GHE_ACCESS_TOKEN\n')
sys.exit(1)