forked from mozilla/github-org-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_member_from_org.py
executable file
·100 lines (82 loc) · 2.81 KB
/
remove_member_from_org.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
# ## Remove a login from org
#
# The goal is to get the specified login out of the org in the cleanest way
# possible.
#
# Currently includes:
# - handling the "login is an owner" case
# - handling the "login is an outside collaborator" case
#
# Not yet handled:
# - keeping track of when they are team maintainers
import logging
import argparse
import github3
from client import get_github3_client
# hack until https://github.com/sigmavirus24/github3.py/pull/675 merged
from github3.exceptions import ForbiddenError # NOQA
if not hasattr(github3.orgs.Organization, "invitations"):
raise NotImplementedError(
"Your version of github3.py does not support "
"invitations. Try "
"https://github.com/hwine/github3.py/tree/invitations"
) # NOQA
logger = logging.getLogger(__name__)
def remove_login_from_org(login, org_name):
org = gh.organization(org_name)
user = org.is_member(login)
if user:
# just remove - no longer any reason to check for type of membership
if not dry_run:
if org.remove_membership(login):
logger.info(f"removed {login} from {org_name}")
else:
logger.error(f"ERROR removing {login} from {org_name}")
# remove any outside collaborator settings
# HACK - no method, so hack the URL and send it directly
oc_url = org._json_data["issues_url"].replace("issues", "outside_collaborators")
delete_url = oc_url + "/" + login
if not dry_run:
response = org._delete(delete_url)
if response.status_code not in [
204,
]:
logger.error(
"ERROR: bad result ({}) from {}".format(
response.status_code, delete_url
)
)
logger.info(f"removed {login} as outside collaborator via {delete_url}")
def parse_args():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"orgs",
help="organizations to operate on",
default=[
"mozilla",
],
nargs="*",
)
parser.add_argument("--login", "-u", help="GitHub user to remove", required=True)
parser.add_argument(
"--dry-run", "-n", help="Do not make changes", action="store_false"
)
parser.add_argument(
"--cwd", "-R", help="repo to use", dest="repos", action="append"
)
# done with per-run setup
args = parser.parse_args()
return args
def main():
args = parse_args()
global dry_run
dry_run = args.dry_run
global gh
gh = get_github3_client()
for org in args.orgs:
remove_login_from_org(args.login, org)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(message)s")
logging.getLogger("github3").setLevel(logging.WARNING)
main()