From cde9f2433284d4d7e44c60dec1773f5fc68f70a8 Mon Sep 17 00:00:00 2001 From: Buhleva Date: Wed, 7 Apr 2021 09:37:39 +0300 Subject: [PATCH 1/3] Add user to organization if found add_or_update_membership is adding user to org, but is expacting always 200 - catch 404 response, when user not login --- app.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index b5d4541..f7534c7 100644 --- a/app.py +++ b/app.py @@ -2,6 +2,7 @@ import os import time import json +import github3 from distutils.util import strtobool from apscheduler.schedulers.background import BackgroundScheduler @@ -13,6 +14,7 @@ app = Flask(__name__) github_app = GitHubApp(app) directory = DirectoryClient() +addUserAsMember = os.environ.get("ADD_MEMBER", False) # Schedule a full sync scheduler = BackgroundScheduler(daemon=True) @@ -180,9 +182,13 @@ def execute_sync(org, team, slug, state): else: for user in state["action"]["add"]: # Validate that user is in org - if org.is_member(user): - print(f"Adding {user} to {slug}") - team.add_or_update_membership(user) + if org.is_member(user) or addUserAsMember: + try: + print(f"Adding {user} to {slug}") + team.add_or_update_membership(user) + except github3.exceptions.NotFoundError: + print(f"User: {user} not found") + pass else: print(f"Skipping {user} as they are not part of the org") From 3df8d02c9391be1627bdbd844cb34db46390a548 Mon Sep 17 00:00:00 2001 From: Buhleva Date: Wed, 7 Apr 2021 10:59:53 +0300 Subject: [PATCH 2/3] Reformat --- app.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/app.py b/app.py index f7534c7..f25a1c4 100644 --- a/app.py +++ b/app.py @@ -31,7 +31,7 @@ def sync_new_team(): owner = github_app.payload["organization"]["login"] team_id = github_app.payload["team"]["id"] if os.environ["USER_DIRECTORY"].upper() == "AAD": - ## Azure APIs don't currently support case insensitive searching + # Azure APIs don't currently support case insensitive searching slug = github_app.payload["team"]["name"].replace(" ", "-") else: slug = github_app.payload["team"]["slug"] @@ -134,7 +134,8 @@ def github_team_members(client=None, owner=None, team_id=None, attribute="userna ) else: for member in team.members(): - team_members.append({"username": str(member).casefold(), "email": ""}) + team_members.append( + {"username": str(member).casefold(), "email": ""}) return team_members @@ -168,7 +169,8 @@ def execute_sync(org, team, slug, state): :param state: :return: """ - total_changes = len(state["action"]["remove"]) + len(state["action"]["add"]) + total_changes = len(state["action"]["remove"]) + \ + len(state["action"]["add"]) if len(state["directory"]) == 0: message = f"{os.environ.get('USER_DIRECTORY', 'LDAP').upper()} group returned empty: {slug}" raise ValueError(message) @@ -184,11 +186,11 @@ def execute_sync(org, team, slug, state): # Validate that user is in org if org.is_member(user) or addUserAsMember: try: - print(f"Adding {user} to {slug}") - team.add_or_update_membership(user) + print(f"Adding {user} to {slug}") + team.add_or_update_membership(user) except github3.exceptions.NotFoundError: - print(f"User: {user} not found") - pass + print(f"User: {user} not found") + pass else: print(f"Skipping {user} as they are not part of the org") From cebba83f659c45556f14720355f325ef23e4e959 Mon Sep 17 00:00:00 2001 From: Buhleva Date: Wed, 7 Apr 2021 11:04:01 +0300 Subject: [PATCH 3/3] Reformat python black --- app.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index f25a1c4..a780b36 100644 --- a/app.py +++ b/app.py @@ -134,8 +134,7 @@ def github_team_members(client=None, owner=None, team_id=None, attribute="userna ) else: for member in team.members(): - team_members.append( - {"username": str(member).casefold(), "email": ""}) + team_members.append({"username": str(member).casefold(), "email": ""}) return team_members @@ -169,8 +168,7 @@ def execute_sync(org, team, slug, state): :param state: :return: """ - total_changes = len(state["action"]["remove"]) + \ - len(state["action"]["add"]) + total_changes = len(state["action"]["remove"]) + len(state["action"]["add"]) if len(state["directory"]) == 0: message = f"{os.environ.get('USER_DIRECTORY', 'LDAP').upper()} group returned empty: {slug}" raise ValueError(message) @@ -271,7 +269,10 @@ def sync_all_teams(): for team in org.teams(): try: sync_team( - client=client, owner=org.login, team_id=team.id, slug=team.slug, + client=client, + owner=org.login, + team_id=team.id, + slug=team.slug, ) except Exception as e: print(f"Organization: {org.login}")