Skip to content

Commit

Permalink
Merge pull request #3 from SAG-iBit/feature/add-user-to-org
Browse files Browse the repository at this point in the history
Add user to organization if found
  • Loading branch information
kalisb authored Apr 7, 2021
2 parents 6b8f735 + cebba83 commit 6cc329a
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import time
import json
import github3
from distutils.util import strtobool

from apscheduler.schedulers.background import BackgroundScheduler
Expand All @@ -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)
Expand All @@ -29,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"]
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -263,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}")
Expand Down

0 comments on commit 6cc329a

Please sign in to comment.