forked from mozilla/github-org-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo-admins
executable file
·144 lines (111 loc) · 3.65 KB
/
repo-admins
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
"""Report on the non-owner admins of the specified repo."""
import argparse
import logging
import argcomplete
try:
from functools import lru_cache
except ImportError:
# add a do nothing for py2
def lru_cache(param):
def identity(func):
return func
return identity
from client import get_github3_client
VERBOSE = False
logger = logging.getLogger(__name__)
def unpack_repo(owner_repo, default=None):
"""unpack a repostitory specification, which might have the owner login in
prepended.
>>> unpack_repo('user/repo')
('user', 'repo')
>>> unpack_repo('user/repo', 'someone_else')
('user', 'repo')
>>> unpack_repo('repo', 'owner')
('owner', 'repo')
"""
parts = owner_repo.split("/", 1)
if len(parts) == 2:
owner, repo = parts
else:
owner, repo = default, parts[0]
return owner, repo
# singleton
static_gh = None
def gh():
global static_gh
if not static_gh:
static_gh = get_github3_client()
return static_gh
def parse_args(args=None):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"repos", nargs="+", help="Repositories to check. Can be owner/repo or" " repo"
)
parser.add_argument(
"--org",
default="mozilla",
help="Org to use for unqualified repos. Default" " mozilla",
)
parser.add_argument(
"--verbose", action="store_true", help="print logins for all changes"
)
parser.add_argument("--debug", help="include github3 output", action="store_true")
parser.add_argument(
"--email", help="output email addresses only", action="store_true"
)
argcomplete.autocomplete(parser)
args = parser.parse_args(args)
if args.debug:
logger.setLevel(logging.DEBUG)
logging.getLogger("github3").setLevel(logging.DEBUG)
global VERBOSE
VERBOSE = args.verbose
return args
@lru_cache(16)
def fetch_owners(login):
"""get all org owners."""
owners = set()
org = gh().organization(login)
for user in org.members(role="admin"):
owners.add(user.login)
return owners
def fetch_admins(owner, repo):
"""get all repository admins."""
admins = set()
repo = gh().repository(owner, repo)
for collaborator in repo.collaborators():
if collaborator.permissions["admin"]:
admins.add(collaborator.login)
return admins
def process_repo(owner, repo):
"""Find all repo admins, who are not organization owners."""
owners = fetch_owners(owner)
admins = fetch_admins(owner, repo)
return admins - owners
def email_of(login):
r"""return email of login, and link to find in people.m.o
returned line has email first, then ',' then people.m.o link
email address can be obtained by piping to `cut -d\, -f 1`
ToDo:
- link to iam, so can map verified github login to email
from there.
"""
u = gh().user(login)
pmo_url = f"https://people.mozilla.org/s?query={login}&who=all"
email = u.email if u.email else ""
return f"{email},{pmo_url}"
def main(args=None):
args = parse_args(args=args)
for repo in args.repos:
owner_name, repo_name = unpack_repo(repo, default=args.org)
admins = process_repo(owner_name, repo_name)
print(f"{owner_name}/{repo_name}:")
for login in admins:
output = login if not args.email else email_of(login)
print(output)
if __name__ == "__main__":
logging.basicConfig(level=logging.WARN, format="%(asctime)s %(message)s")
logging.getLogger("github3").setLevel(logging.WARNING)
raise SystemExit(main())