Skip to content

Commit

Permalink
Updating org_add_user to handle errors more expressively and use args…
Browse files Browse the repository at this point in the history
… slightly differently to help with scripting (MoCo-GHE-Admin#115)
  • Loading branch information
cknowles-moz authored Aug 2, 2023
1 parent 5a7db21 commit 0370b5b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,17 @@ optional arguments:

## `org_add_user.py`
```
usage: org_add_user.py [-h] [--pat-key PATKEY] [--token TOKEN] [--teams TEAMS [TEAMS ...]] [--owner] org username
usage: org_add_user.py [-h] [--pat-key PATKEY] [--token TOKEN] [--org ORG] [--user USERNAME] [--teams TEAMS [TEAMS ...]] [--owner]
Give a username, an org, and a team list and add the user to the org. NOTE: if the org is SAML'd you'll probably need to provision
the user in your IdP system(s)
positional arguments:
org The org to work with
username GH user ID to add
options:
-h, --help show this help message and exit
--pat-key PATKEY key in .gh_pat.toml of the PAT to use
--token TOKEN use this PAT to access resources
--org ORG The org to work with
--user USERNAME GH user ID to add
--teams TEAMS [TEAMS ...]
list of team slugs
--owner Should they be an owner
Expand Down
20 changes: 15 additions & 5 deletions org_add_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
Given a username an org, and a list of teams, send an invite to that user for the org/team
"""

import logging

from github3 import exceptions as gh_exceptions
from github3 import login

Expand All @@ -21,8 +23,8 @@ def parse_args():
parser = utils.GH_ArgParser(
description="Give a username, an org, and a team list and add the user to the org. NOTE: if the org is SAML'd you'll probably need to provision the user in your IdP system(s)"
)
parser.add_argument("org", type=str, help="The org to work with", action="store")
parser.add_argument("username", help="GH user ID to add")
parser.add_argument("--org", type=str, help="The org to work with", action="store")
parser.add_argument("--user", help="GH user ID to add", dest="username")
parser.add_argument("--teams", help="list of team slugs", nargs="+")
parser.add_argument("--owner", help="Should they be an owner", action="store_true")
args = parser.parse_args()
Expand All @@ -37,20 +39,20 @@ def main():
org = gh_sess.organization(args.org)
except gh_exceptions.NotFoundError:
print(f"Organization {args.org} is not found")
exit()
exit(1)
try:
user_id = gh_sess.user(args.username).id
except gh_exceptions.NotFoundError:
print(f"User {args.username} is not found")
exit()
exit(1)
teamlist = []
if args.teams is not None:
try:
for teamslug in args.teams:
teamlist.append(org.team_by_name(teamslug).id)
except gh_exceptions.NotFoundError:
print("Teams not resolving - please verify the team-slug")
exit()
exit(1)
# For some reason the module uses different terms for the member privilege if you're not adding a team
# and also (later in the code) you'll see that it uses a different method to invite the teamless.
if args.owner:
Expand All @@ -62,11 +64,19 @@ def main():

try:
if args.teams is not None:
# github3 has a problem where it detects the 201 as a concern rather than SUCCESS - and they've
# not been responsive to requests to look at it.
oldlevel = logging.getLogger("github3").level
logging.getLogger("github3").setLevel(logging.ERROR)
org.invite(team_ids=teamlist, invitee_id=user_id, role=priv)
logging.getLogger("github3").setLevel(oldlevel)
else:
org.add_or_update_membership(username=args.username, role=priv)
except Exception as error:
print(f"An exception on creation occurred: {error}")
exit(1)
else:
print(f"Invited user {args.username} to org {args.org}.")


if __name__ == "__main__":
Expand Down

0 comments on commit 0370b5b

Please sign in to comment.