|
8 | 8 | import sys |
9 | 9 | from dataclasses import asdict, dataclass, field |
10 | 10 |
|
11 | | -from github import ( |
12 | | - Auth, |
13 | | - Github, |
| 11 | +from github import Auth, Github, GithubIntegration |
| 12 | +from github.GithubException import ( |
| 13 | + BadCredentialsException, |
14 | 14 | GithubException, |
15 | | - GithubIntegration, |
16 | 15 | UnknownObjectException, |
17 | 16 | ) |
18 | | -from github.GithubException import BadCredentialsException |
19 | 17 | from github.NamedUser import NamedUser |
20 | 18 | from github.Organization import Organization |
21 | 19 | from github.Repository import Repository |
@@ -54,6 +52,7 @@ class GHorg: # pylint: disable=too-many-instance-attributes, too-many-lines |
54 | 52 | current_repos_collaborators: dict[Repository, dict[str, str]] = field(default_factory=dict) |
55 | 53 | configured_repos_collaborators: dict[str, dict[str, str]] = field(default_factory=dict) |
56 | 54 | archived_repos: list[Repository] = field(default_factory=list) |
| 55 | + unconfigured_teams: list[Team] = field(default_factory=list) |
57 | 56 | unconfigured_team_repo_permissions: dict[str, dict[str, str]] = field(default_factory=dict) |
58 | 57 | stats: OrgChanges = field(default_factory=OrgChanges) |
59 | 58 |
|
@@ -296,7 +295,18 @@ def create_missing_teams(self, dry: bool = False): |
296 | 295 | for team, attributes in self.configured_teams.items(): |
297 | 296 | if team not in existent_team_names: |
298 | 297 | if parent := attributes.get("parent"): # type: ignore |
299 | | - parent_id = self.org.get_team_by_slug(sluggify_teamname(parent)).id |
| 298 | + try: |
| 299 | + parent_id = self.org.get_team_by_slug(sluggify_teamname(parent)).id |
| 300 | + except UnknownObjectException: |
| 301 | + if dry: |
| 302 | + logging.debug( |
| 303 | + "For team %s, the configured parent team's ('%s') ID wasn't found, " |
| 304 | + "probably because it should be created but it's a dry-run. " |
| 305 | + "We set a default ID of 424242", |
| 306 | + team, |
| 307 | + parent, |
| 308 | + ) |
| 309 | + parent_id = 424242 |
300 | 310 |
|
301 | 311 | logging.info("Creating team '%s' with parent ID '%s'", team, parent_id) |
302 | 312 | self.stats.create_team(team) |
@@ -594,31 +604,38 @@ def sync_teams_members(self, dry: bool = False) -> None: # pylint: disable=too- |
594 | 604 | team.name, |
595 | 605 | ) |
596 | 606 |
|
597 | | - def get_unconfigured_teams( |
| 607 | + def get_and_delete_unconfigured_teams( |
598 | 608 | self, dry: bool = False, delete_unconfigured_teams: bool = False |
599 | 609 | ) -> None: |
600 | 610 | """Get all teams that are not configured locally and optionally remove them""" |
601 | 611 | # Get all teams that are not configured locally |
602 | | - unconfigured_teams: list[Team] = [] |
603 | 612 | for team in self.current_teams: |
604 | 613 | if team.name not in self.configured_teams: |
605 | | - unconfigured_teams.append(team) |
| 614 | + self.unconfigured_teams.append(team) |
606 | 615 |
|
607 | | - if unconfigured_teams: |
| 616 | + if self.unconfigured_teams: |
608 | 617 | if delete_unconfigured_teams: |
609 | | - for team in unconfigured_teams: |
| 618 | + for team in self.unconfigured_teams: |
610 | 619 | logging.info("Deleting team '%s' as it is not configured locally", team.name) |
611 | 620 | self.stats.delete_team(team=team.name, deleted=True) |
612 | 621 | if not dry: |
613 | | - team.delete() |
| 622 | + try: |
| 623 | + team.delete() |
| 624 | + except UnknownObjectException as e: |
| 625 | + logging.info( |
| 626 | + "Team '%s' could not be deleted, probably because it was already " |
| 627 | + "deleted as part of a parent team. " |
| 628 | + "Error: %s", |
| 629 | + team.name, |
| 630 | + e, |
| 631 | + ) |
614 | 632 | else: |
615 | | - unconfigured_teams_str = [team.name for team in unconfigured_teams] |
616 | 633 | logging.warning( |
617 | 634 | "The following teams of your GitHub organisation are not " |
618 | 635 | "configured locally: %s. Taking no action about these teams.", |
619 | | - ", ".join(unconfigured_teams_str), |
| 636 | + ", ".join([team.name for team in self.unconfigured_teams]), |
620 | 637 | ) |
621 | | - for team in unconfigured_teams: |
| 638 | + for team in self.unconfigured_teams: |
622 | 639 | self.stats.delete_team(team=team.name, deleted=False) |
623 | 640 |
|
624 | 641 | def get_members_without_team( |
|
0 commit comments