Last active
March 2, 2023 19:08
-
-
Save bvanhou/396dc85368a8ea6d3c6272728adad9ce to your computer and use it in GitHub Desktop.
GitHub : Replicate Teams to Repository
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from github import Github, GithubException | |
| def replicate_teams(): | |
| # Access token to authenticate with GitHub API | |
| access_token = "<insert access token here>" | |
| # Name of the source and target organizations and repositories | |
| source_org_name = "source_org" | |
| source_repo_name = "source_repo" | |
| target_org_name = "target_org" | |
| target_repo_name = "target_repo" | |
| # Blacklist of teams to ignore | |
| team_blacklist = ["TeamA", "TeamB"] | |
| # Initialize the GitHub API client | |
| g = Github(access_token) | |
| # Get the source and target organizations and repositories | |
| source_org = g.get_organization(source_org_name) | |
| source_repo = source_org.get_repo(source_repo_name) | |
| target_org = g.get_organization(target_org_name) | |
| target_repo = target_org.get_repo(target_repo_name) | |
| # Iterate over the teams in the source repository | |
| for source_team in source_repo.get_teams(): | |
| # Check if the team is in the blacklist | |
| if source_team.name in team_blacklist: | |
| print(f"Ignoring blacklisted team {source_team.name}") | |
| continue | |
| # Create the team in the target repository | |
| target_team = None | |
| try: | |
| target_team = target_org.create_team(source_team.name, description=source_team.description) | |
| print(f"Created team {target_team.name}") | |
| except: | |
| target_team = target_org.get_team_by_slug(source_team.slug) | |
| print(f"Team {target_team.name} already exists in {target_org_name}") | |
| # Check if the team has permission on the target repository | |
| permission = target_team.get_repo_permission(target_repo) | |
| if permission is None: | |
| # If the team doesn't have permission, add it with the appropriate role | |
| source_role = convert_permission_to_role(source_team.get_repo_permission(source_repo)) | |
| target_team.update_team_repository(target_repo, source_role) | |
| print(f"Added {target_team.name} to {target_repo_name} with role {source_role}") | |
| else: | |
| # If the team already has permission, check if the role needs to be updated | |
| source_role = convert_permission_to_role(source_team.get_repo_permission(source_repo)) | |
| target_role = convert_permission_to_role(permission) | |
| if source_role != target_role: | |
| target_team.update_team_repository(target_repo, source_role) | |
| print(f"Updated {target_team.name} role to {source_role} in {target_repo_name}") | |
| # Add the team members to the target team | |
| for source_member in source_team.get_members(): | |
| if target_team.has_in_members(source_member): | |
| print(f"Team {target_team.name} in {target_org_name} already has member {source_member.login}") | |
| continue | |
| try: | |
| target_team.add_membership(source_member, role="member") | |
| print(f"Added {source_member.login} to team {target_team.name}") | |
| except GithubException as exception: | |
| print(f"Could not add {source_member.login} to team {target_team.name}") | |
| def convert_permission_to_role(permission): | |
| if permission.admin: | |
| return "admin" | |
| elif permission.maintain: | |
| return "maintain" | |
| elif permission.triage: | |
| return "triage" | |
| elif permission.push: | |
| return "write" | |
| else: | |
| return "read" | |
| if __name__ == "__main__": | |
| replicate_teams() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment