Skip to content

Commit

Permalink
refactor: improve configured owner handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mxmehl committed Aug 21, 2024
1 parent 4e696cd commit 4205a64
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
22 changes: 10 additions & 12 deletions gh_org_mgr/_gh_org.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,19 @@ def _get_current_org_owners(self) -> None:
for member in self.org.get_members(role="admin"):
self.current_org_owners.append(member)

def _get_configured_org_owners(self, cfg_org_owners: list[str] | str | None) -> None:
"""Import configured owners for the organization from the org configuration"""
def _check_configured_org_owners(self) -> bool:
"""Check configured owners and make them lower-case for better
comparison. Returns True if owners are well configured."""
# Add configured owners if they are a list
if isinstance(cfg_org_owners, list):
# Import users to dataclass attribute, lower-case
for user in cfg_org_owners:
self.configured_org_owners.append(user.lower())
if isinstance(self.configured_org_owners, list):
# Make all configured users lower-case
self.configured_org_owners = [user.lower() for user in self.configured_org_owners]
else:
logging.warning(
"The organisation owners are not configured as a proper list. Will not handle them."
)
self.configured_org_owners = []

def _check_non_empty_configured_owners(self) -> bool:
"""Handle if there are no configured owners. Returns True if owners are configured."""
if not self.configured_org_owners:
logging.warning(
"No owners for your GitHub organisation configured. Will not make any "
Expand All @@ -216,14 +215,13 @@ def _is_user_authenticated_user(self, user: NamedUser) -> bool:
return True
return False

def sync_org_owners(self, cfg_org_owners: list, dry: bool = False, force: bool = False) -> None:
def sync_org_owners(self, dry: bool = False, force: bool = False) -> None:
"""Synchronise the organization owners"""
# Get current and configured owners
self._get_current_org_owners()
self._get_configured_org_owners(cfg_org_owners=cfg_org_owners)

# Abort owner synchronisation if there are no configured owners
if not self._check_non_empty_configured_owners():
# Abort owner synchronisation if no owners are configured, or badly
if not self._check_configured_org_owners():
return

# Get differences between the current and configured owners
Expand Down
5 changes: 2 additions & 3 deletions gh_org_mgr/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def main():
"No GitHub organisation name configured in organisation settings. Cannot continue"
)
sys.exit(1)
org.configured_org_owners = cfg_org.get("org_owners")

# Login to GitHub with token, get GitHub organisation
org.login(cfg_org.get("org_name", ""), cfg_app.get("github_token", ""))
Expand All @@ -115,9 +116,7 @@ def main():
# Create teams that aren't present at Github yet
org.create_missing_teams(dry=args.dry)
# Synchronise organisation owners
org.sync_org_owners(
cfg_org_owners=cfg_org.get("org_owners"), dry=args.dry, force=args.force
)
org.sync_org_owners(dry=args.dry, force=args.force)
# Synchronise the team memberships
org.sync_teams_members(dry=args.dry)
# Report about organisation members that do not belong to any team
Expand Down

0 comments on commit 4205a64

Please sign in to comment.