def create_teams( teams: Iterable[plug.StudentTeam], permission: plug.TeamPermission, api: plug.PlatformAPI, ) -> Iterable[plug.Team]: """Create teams. Args: teams: An iterable of teams to create. permission: The permission to assign to the team with respect to its repositories. api: A platform API instance. Returns: An iterable of wrappers around created teams. """ teams = list(teams) existing_teams_dict = { existing.name: existing for existing in api.get_teams({t.name for t in teams}) } for required_team in teams: team = existing_teams_dict.get(required_team.name) or api.create_team( required_team.name, members=required_team.members, permission=permission, ) existing_members = set(team.members) new_members = set(required_team.members) - existing_members api.assign_members(team, new_members, permission) # FIXME the returned team won't have the correct members if any new # ones are added. This should be fixed by disconnecting members # from teams, and having an api call "get_team_members" yield team
def command(self, api: plug.PlatformAPI) -> Optional[plug.Result]: teachers_team = _get_or_create_team(TEACHERS_TEAM_NAME, api) existing_members = teachers_team.members new_members = list(set(self.teachers) - set(existing_members)) api.assign_members(teachers_team, new_members, permission=plug.TeamPermission.PULL) for repo in plug.cli.io.progress_bar( api.get_repos(), desc="Granting read access to repos"): api.assign_repo( repo=repo, team=teachers_team, permission=plug.TeamPermission.PULL, ) msg = (f"Added {', '.join(new_members)} to the '{TEACHERS_TEAM_NAME}' " "team") return plug.Result(name="add-teachers", status=plug.Status.SUCCESS, msg=msg)