def run(dry_run): base_url = os.environ.get('GITHUB_API', 'https://api.github.com') desired_state = fetch_desired_state() for github_org_name, desired_github_usernames in desired_state.items(): config = get_config(desired_org_name=github_org_name) token = config['github'][github_org_name]['token'] gh = Github(token, base_url=base_url) raw_gh = RawGithubApi(token) gh_org = gh.get_organization(github_org_name) gh_org_members = gh_org.get_members(role='admin') current_github_usernames = [m.login for m in gh_org_members] invitations = raw_gh.org_invitations(github_org_name) current_github_usernames.extend(invitations) current_github_usernames = \ [m.lower() for m in current_github_usernames] desired_github_usernames = \ [m.lower() for m in desired_github_usernames] for github_username in desired_github_usernames: if github_username not in current_github_usernames: logging.info(['add_owner', github_org_name, github_username]) if not dry_run: gh_user = gh.get_user(github_username) gh_org.add_to_members(gh_user, 'admin')
def __init__(self, config): for org_name, org_config in config["github"].items(): token = org_config["token"] managed_teams = org_config.get("managed_teams", None) self._orgs[org_name] = ( Github(token, base_url=GH_BASE_URL), RawGithubApi(token), managed_teams, )
def _accept_invitations( github: raw_github_api.RawGithubApi, code_components: CodeComponents, dry_run: bool ) -> set[str]: accepted_invitations = set() urls = code_components.urls known_orgs = code_components.known_orgs for i in github.repo_invitations(): invitation_id = i["id"] invitation_url = i["html_url"] url = os.path.dirname(invitation_url) accept = url in urls or any(url.startswith(org) for org in known_orgs) if accept: logging.info(["accept", url]) accepted_invitations.add(url) if not dry_run: github.accept_repo_invitation(invitation_id) else: logging.debug(["skipping", url]) return accepted_invitations
def run(dry_run): base_url = os.environ.get("GITHUB_API", "https://api.github.com") config = get_config() desired_state = fetch_desired_state() for github_org_name, desired_github_usernames in desired_state.items(): token = config["github"][github_org_name]["token"] gh = Github(token, base_url=base_url) raw_gh = RawGithubApi(token) gh_org, current_github_usernames = get_current_github_usernames( github_org_name, gh, raw_gh) current_github_usernames = [ m.lower() for m in current_github_usernames ] desired_github_usernames = [ m.lower() for m in desired_github_usernames ] for github_username in desired_github_usernames: if github_username not in current_github_usernames: logging.info(["add_owner", github_org_name, github_username]) if not dry_run: gh_user = gh.get_user(github_username) gh_org.add_to_members(gh_user, "admin")
def __init__(self, config): for org_name, org_config in config['github'].items(): token = org_config['token'] managed_teams = org_config.get('managed_teams', None) self._orgs[org_name] = (Github(token, base_url=GH_BASE_URL), RawGithubApi(token), managed_teams)