예제 #1
0
def sync_all_teams():
    """
    Lookup teams in a GitHub org and synchronize all teams with your user directory
    :return:
    """
    print(f'Syncing all teams: {time.strftime("%A, %d. %B %Y %I:%M:%S %p")}')
    installations = get_app_installations()
    for i in installations():
        print("========================================================")
        print(f"## Processing Organization: {i.account['login']}")
        print("========================================================")
        with app.app_context() as ctx:
            try:
                gh = GitHubApp(ctx.push())
                client = gh.app_installation(installation_id=i.id)
                org = client.organization(i.account["login"])
                for team in org.teams():
                    try:
                        sync_team(
                            client=client,
                            owner=org.login,
                            team_id=team.id,
                            slug=team.slug,
                        )
                    except Exception as e:
                        print(f"Organization: {org.login}")
                        print(f"Unable to sync team: {team.slug}")
                        print(f"DEBUG: {e}")
            except Exception as e:
                print(f"DEBUG: {e}")
            finally:
                ctx.pop()
예제 #2
0
def sync_all_teams():
    """
    Lookup teams in a GitHub org and synchronize all teams with your user directory
    :return:
    """

    print(f'Syncing all teams: {time.strftime("%A, %d. %B %Y %I:%M:%S %p")}')

    installations = get_app_installations()
    custom_map = load_custom_map()
    futures = []
    with ThreadPoolExecutor(max_workers=10) as exe:
        for i in installations():
            print("========================================================")
            print(f"## Processing Organization: {i.account['login']}")
            print("========================================================")
            with app.app_context() as ctx:
                try:
                    gh = GitHubApp(ctx.push())
                    client = gh.app_installation(installation_id=i.id)
                    org = client.organization(i.account["login"])
                    for team in org.teams():
                        futures.append(
                            exe.submit(sync_team_helper, team, custom_map,
                                       client, org))
                except Exception as e:
                    print(f"DEBUG: {e}")
                finally:
                    ctx.pop()
    for future in futures:
        future.result()
예제 #3
0
def sync_all_teams():
    """
    Lookup teams in a GitHub org and synchronize all teams with your user directory
    :return:
    """

    print(f'Syncing all teams: {time.strftime("%A, %d. %B %Y %I:%M:%S %p")}')

    installations = get_app_installations()
    custom_map, _ = load_custom_map()
    futures = []
    install_count = 0
    with ThreadPoolExecutor(max_workers=10) as exe:
        for i in installations():
            install_count += 1
            print("========================================================")
            print(f"## Processing Organization: {i.account['login']}")
            print("========================================================")
            with app.app_context() as ctx:
                try:
                    gh = GitHubApp(ctx.push())
                    client = gh.app_installation(installation_id=i.id)
                    org = client.organization(i.account["login"])
                    for team in org.teams():
                        futures.append(
                            exe.submit(sync_team_helper, team, custom_map,
                                       client, org))
                except Exception as e:
                    print(f"DEBUG: {e}")
                finally:
                    ctx.pop()
    if not install_count:
        raise Exception(
            f"No installation defined for APP_ID {os.getenv('APP_ID')}")
    for future in futures:
        future.result()
    if REMOVE_ORG_MEMBERS_WITHOUT_TEAM:
        remove_org_members_without_team(installations)
    print(
        f'Syncing all teams successful: {time.strftime("%A, %d. %B %Y %I:%M:%S %p")}'
    )
예제 #4
0
def remove_org_members_without_team(installations):
    for i in installations():
        with app.app_context() as ctx:
            try:
                gh = GitHubApp(ctx.push())
                client = gh.app_installation(installation_id=i.id)
                org = client.organization(i.account["login"])
                org_members = [member for member in org.members()]
                team_members = [
                    member for team in org.teams()
                    for member in team.members()
                ]
                remove_members = list(set(org_members) - set(team_members))
                for member in remove_members:
                    print(f"Removing {member}")
                    if not TEST_MODE:
                        org.remove_membership(str(member))
            except Exception as e:
                print(f"DEBUG: {e}")
            finally:
                ctx.pop()
예제 #5
0
def get_app_installations():
    """
    Get a list of installations for this app
    :return:
    """
    with app.app_context() as ctx:
        try:
            c = ctx.push()
            gh = GitHubApp(c)
            installations = gh.app_client.app_installations
        finally:
            ctx.pop()
    return installations
예제 #6
0
import atexit
import os
import time
import json
import github3
from distutils.util import strtobool

from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger
from flask import Flask

from githubapp import GitHubApp, DirectoryClient, CRON_INTERVAL, TEST_MODE

app = Flask(__name__)
github_app = GitHubApp(app)
directory = DirectoryClient()
addUserAsMember = os.environ.get("ADD_MEMBER", False)

# Schedule a full sync
scheduler = BackgroundScheduler(daemon=True)
scheduler.start()
atexit.register(lambda: scheduler.shutdown(wait=False))


@github_app.on("team.created")
def sync_new_team():
    """
    Sync a new team when it is created
    :return:
    """
    owner = github_app.payload["organization"]["login"]