def _add_all_repos_to_team( github: GitHub, organization_name: str, team_name: str, permission: RepoPermission=RepoPermission.ADMIN ): '''Add all repos found in `organization_name` to the given `team_name`''' # passed GitHub object must have org admin authorization to assign team to repo with admin rights organization = github.organization(organization_name) team = _retrieve_team_by_name_or_none(organization, team_name) if not team: ci.util.fail("Team {name} does not exist".format(name=team_name)) for repo in organization.repositories(): if team.has_repository(repo.full_name): ci.util.verbose("Team {teamnname} already assigned to repo {reponame}".format( teamnname=team_name, reponame=repo.full_name )) continue team.add_repository(repository=repo.full_name, permission=permission.value) ci.util.info("Added team {teamname} to repository {reponame}".format( teamname=team_name, reponame=repo.full_name ))
def _create_team(github: GitHub, organization_name: str, team_name: str): # passed GitHub object must have org. admin authorization to create a team organization = github.organization(organization_name) team = _retrieve_team_by_name_or_none(organization, team_name) if team: ci.util.verbose("Team {name} already exists".format(name=team_name)) return try: organization.create_team(name=team_name) ci.util.info("Team {name} created".format(name=team_name)) except ForbiddenError as err: ci.util.fail( "{err} Cannot create team {name} in org {org} due to missing privileges" .format(err=err, name=team_name, org=organization_name))
def _add_user_to_team(github: GitHub, organization_name: str, team_name: str, user_name: str): # passed GitHub object must have org. admin authorization to add a user to a team organization = github.organization(organization_name) team = _retrieve_team_by_name_or_none(organization, team_name) if not team: ci.util.fail(f"Team '{team_name}' does not exist") if team.is_member(user_name): ci.util.verbose( f"'{user_name}' is already assigned to team '{team_name}'") return if team.add_member(username=user_name): ci.util.info(f"Added '{user_name}' to team '{team_name}'") else: ci.util.fail( f"Could not add '{user_name}' to team '{team_name}'. Check for missing privileges" )