forked from MoCo-GHE-Admin/github-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorg_comms_team.py
executable file
·103 lines (90 loc) · 3.55 KB
/
org_comms_team.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
101
102
103
#!/usr/bin/env python
"""
Script for creating the "comms-everyone" team in an org
This is a stopgap measure to try and communicate with people.
Unless watches are setup JUST so, people won't see this.
Mainly used prior to SAML enable/enforcement - so there's chances that
SAML will be in a splitbrain mode
"""
from github3 import exceptions as gh_exceptions
from github3 import login
from github_scripts import utils
def parse_args():
"""
Go through the command line.
If no token is specified prompt for it.
:return: Returns the parsed CLI datastructures.
"""
parser = utils.GH_ArgParser(
description="Go into an org, create a team named for the --team-name and add all members to it, OR if --users is specified - add that list of users. Specify --remove to invert the operation"
)
parser.add_argument("org", help="organization to do this to", action="store")
parser.add_argument(
"--team-name",
dest="team_name",
help="name of the team to create, defaults to 'everybody-temp-comms'",
action="store",
default="everybody-temp-comms",
)
parser.add_argument("--users", nargs="+", help="List of users to add to the team")
parser.add_argument(
"--remove",
help="Remove the specified users from the team rather than add",
action="store_true",
)
args = parser.parse_args()
return args
def find_team(org, team_name):
"""
go through the organization's teams, looking for team_name if it exists,
return the integer ID, otherwise -1
:param org: The initialized organization object
:param team_name: string of the team_name
:return: integer ID if found, -1 if not.
"""
teams = org.teams()
for team in teams:
if team.name == team_name:
return team.id
return -1
def main():
"""
Get the args, get into GH, and create the team
"""
args = parse_args()
gh_sess = login(token=args.token)
org = gh_sess.organization(args.org)
# Let's see if the team exists
team_found = find_team(org, args.team_name)
if team_found > 0:
team = org.team(team_found)
else:
team = org.create_team(name=args.team_name)
if args.users is not None:
for member in args.users:
try:
if args.remove:
team.revoke_membership(username=member)
print(f"Removed {member} from the team")
else:
team.add_or_update_membership(username=member)
print(f"Added {member} to the team")
except gh_exceptions.UnprocessableEntity:
print(f"User {member} doesn't appear to be addable (SAML? misspelled?) Skipping.")
else:
member_list = org.members()
for member in member_list:
# Note, this call will fail if SAML is enforced, but the user isn't SAMLd.
# This is precisely NOT the use case for this program, so "Note it and move on"
try:
if args.remove:
team.revoke_membership(username=member.login)
print(f"Removed {member.login} from the team")
else:
team.add_or_update_membership(username=member.login)
print(f"Added {member.login} to the team")
except gh_exceptions.UnprocessableEntity:
print(f"User {member.login} doesn't appear to be addable (SAML?) Skipping.")
print(f"Group named {args.team_name} created or updated in org {args.org}")
if __name__ == "__main__":
main()