-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddOrganizationMembers.py
More file actions
65 lines (49 loc) · 1.89 KB
/
Copy pathAddOrganizationMembers.py
File metadata and controls
65 lines (49 loc) · 1.89 KB
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
#!/usr/bin/env python3
"""
mass add users to organization
python AddOrganizationMembers.py my.xlsx ~/.ssh/orgOauth myorg 4
Oauth permissions
https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28
https://github.com/settings/tokens/new
* fine-grained: Oauth "Metadata" repository permissions (read)
-- but needs to be for org. Classic may be better
* classic: Oauth "write:org" and public_repo (or repo for private) permissions
"""
import pandas
import github
from gitbulk import check_api_limit, connect
from pathlib import Path
from argparse import ArgumentParser
def adder(users: pandas.DataFrame, op, sess) -> None:
members = [m.login for m in op.get_members()]
invited = [m.login for m in op.invitations()]
for u in users.values:
login = u.strip()
if login in members or login in invited:
continue
try:
user = sess.get_user(login)
except github.GithubException:
raise ValueError(f"unknown GitHub username {login}")
op.add_to_members(user)
print(f"invited: {login}")
p = ArgumentParser(description="mass add team members to repos, optionally creating new repos")
p.add_argument("fn", help=".xlsx with group info")
p.add_argument("oauth", help="Oauth file")
p.add_argument("orgname", help="Github Organization")
p.add_argument("col", help="column for GitHub Username", type=int)
P = p.parse_args()
fn = Path(P.fn).expanduser()
match fn.suffix:
case ".xls" | ".xlsx":
users = pandas.read_excel(fn, usecols=[P.col]).squeeze().dropna()
case ".csv":
users = pandas.read_csv(fn, usecols=[P.col]).squeeze().dropna()
case _:
raise ValueError(f"Unknown file type {fn}")
if not users.ndim == 1:
raise ValueError("need to have member names. Check that -col argument matches spreadsheet.")
# %%
op, sess = connect(P.oauth, P.orgname)
check_api_limit(sess)
adder(users, op, sess)