Skip to content

Commit bd963ec

Browse files
committed
fix: do not crash when to-be-deleted team doesn't exist (any more)
1 parent d24e8f1 commit bd963ec

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

gh_org_mgr/_gh_org.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class GHorg: # pylint: disable=too-many-instance-attributes, too-many-lines
5252
current_repos_collaborators: dict[Repository, dict[str, str]] = field(default_factory=dict)
5353
configured_repos_collaborators: dict[str, dict[str, str]] = field(default_factory=dict)
5454
archived_repos: list[Repository] = field(default_factory=list)
55+
unconfigured_teams: list[Team] = field(default_factory=list)
5556
unconfigured_team_repo_permissions: dict[str, dict[str, str]] = field(default_factory=dict)
5657
stats: OrgChanges = field(default_factory=OrgChanges)
5758

@@ -603,31 +604,38 @@ def sync_teams_members(self, dry: bool = False) -> None: # pylint: disable=too-
603604
team.name,
604605
)
605606

606-
def get_unconfigured_teams(
607+
def get_and_delete_unconfigured_teams(
607608
self, dry: bool = False, delete_unconfigured_teams: bool = False
608609
) -> None:
609610
"""Get all teams that are not configured locally and optionally remove them"""
610611
# Get all teams that are not configured locally
611-
unconfigured_teams: list[Team] = []
612612
for team in self.current_teams:
613613
if team.name not in self.configured_teams:
614-
unconfigured_teams.append(team)
614+
self.unconfigured_teams.append(team)
615615

616-
if unconfigured_teams:
616+
if self.unconfigured_teams:
617617
if delete_unconfigured_teams:
618-
for team in unconfigured_teams:
618+
for team in self.unconfigured_teams:
619619
logging.info("Deleting team '%s' as it is not configured locally", team.name)
620620
self.stats.delete_team(team=team.name, deleted=True)
621621
if not dry:
622-
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+
)
623632
else:
624-
unconfigured_teams_str = [team.name for team in unconfigured_teams]
625633
logging.warning(
626634
"The following teams of your GitHub organisation are not "
627635
"configured locally: %s. Taking no action about these teams.",
628-
", ".join(unconfigured_teams_str),
636+
", ".join([team.name for team in self.unconfigured_teams]),
629637
)
630-
for team in unconfigured_teams:
638+
for team in self.unconfigured_teams:
631639
self.stats.delete_team(team=team.name, deleted=False)
632640

633641
def get_members_without_team(

gh_org_mgr/manage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def main():
145145
org.sync_teams_members(dry=args.dry)
146146
# Report and act on teams that are not configured locally
147147
log_progress("Checking for unconfigured teams...")
148-
org.get_unconfigured_teams(
148+
org.get_and_delete_unconfigured_teams(
149149
dry=args.dry,
150150
delete_unconfigured_teams=cfg_app.get("delete_unconfigured_teams", False),
151151
)

0 commit comments

Comments
 (0)