Skip to content

Commit 8a265d0

Browse files
committed
sync org owners before any team syncs
1 parent d443793 commit 8a265d0

File tree

2 files changed

+108
-108
lines changed

2 files changed

+108
-108
lines changed

gh_org_mgr/_gh_org.py

Lines changed: 106 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,112 @@ def consolidate_team_config(self, default_team_configs: dict[str, str]) -> None:
198198

199199
logging.debug("Configuration for team '%s' consolidated to: %s", team_name, team_config)
200200

201+
# --------------------------------------------------------------------------
202+
# Owners
203+
# --------------------------------------------------------------------------
204+
def _get_current_org_owners(self) -> None:
205+
"""Get all owners of the org"""
206+
# Reset the user list, then build up new list
207+
self.current_org_owners = []
208+
for member in self.org.get_members(role="admin"):
209+
self.current_org_owners.append(member)
210+
211+
def _check_configured_org_owners(self) -> bool:
212+
"""Check configured owners and make them lower-case for better
213+
comparison. Returns True if owners are well configured."""
214+
# Add configured owners if they are a list
215+
if isinstance(self.configured_org_owners, list):
216+
# Make all configured users lower-case
217+
self.configured_org_owners = [user.lower() for user in self.configured_org_owners]
218+
else:
219+
logging.warning(
220+
"The organisation owners are not configured as a proper list. Will not handle them."
221+
)
222+
self.configured_org_owners = []
223+
224+
if not self.configured_org_owners:
225+
logging.warning(
226+
"No owners for your GitHub organisation configured. Will not make any "
227+
"change regarding the ownership, and continue with the current owners: %s",
228+
", ".join([user.login for user in self.current_org_owners]),
229+
)
230+
return False
231+
232+
return True
233+
234+
def _is_user_authenticated_user(self, user: NamedUser) -> bool:
235+
"""Check if a given NamedUser is the authenticated user"""
236+
if user.login == self.gh.get_user().login:
237+
return True
238+
return False
239+
240+
def sync_org_owners(self, dry: bool = False, force: bool = False) -> None:
241+
"""Synchronise the organization owners"""
242+
# Get current and configured owners
243+
self._get_current_org_owners()
244+
245+
# Abort owner synchronisation if no owners are configured, or badly
246+
if not self._check_configured_org_owners():
247+
return
248+
249+
# Get differences between the current and configured owners
250+
owners_remove, owners_ok, owners_add = self.compare_two_lists(
251+
self.configured_org_owners, [user.login for user in self.current_org_owners]
252+
)
253+
# Compare configured (lower-cased) owners with lower-cased list of current owners
254+
if not owners_remove and not owners_add:
255+
logging.info("Organization owners are in sync, no changes")
256+
return
257+
258+
logging.debug(
259+
"Organization owners are not in sync. Config: '%s' vs. Current: '%s'",
260+
self.configured_org_owners,
261+
self.current_org_owners,
262+
)
263+
logging.debug(
264+
"Will remove %s, will not change %s, will add %s", owners_remove, owners_ok, owners_add
265+
)
266+
267+
# Add the missing owners
268+
for user in owners_add:
269+
if gh_user := self._resolve_gh_username(user, "<org owners>"):
270+
logging.info("Adding user '%s' as organization owner", gh_user.login)
271+
if not dry:
272+
self.org.add_to_members(gh_user, "admin")
273+
274+
# Remove the surplus owners
275+
for user in owners_remove:
276+
if gh_user := self._resolve_gh_username(user, "<org owners>"):
277+
logging.info(
278+
"User '%s' is not configured as organization owners. "
279+
"Will make them a normal member",
280+
gh_user.login,
281+
)
282+
# Handle authenticated user being the same as the one you want to degrade
283+
if self._is_user_authenticated_user(gh_user):
284+
logging.warning(
285+
"The user '%s' you want to remove from owners is the one you "
286+
"authenticated with. This may disrupt all further operations. "
287+
"Unless you run the program with --force, "
288+
"this operation will not be executed.",
289+
gh_user.login,
290+
)
291+
# Check if user forced this operation
292+
if force:
293+
logging.info(
294+
"You called the program with --force, "
295+
"so it will remove yourself from the owners"
296+
)
297+
else:
298+
continue
299+
300+
# Execute the degradation of the owner
301+
if not dry:
302+
self.org.add_to_members(gh_user, "member")
303+
304+
# Update the current organisation owners
305+
self._get_current_org_owners()
306+
201307
# --------------------------------------------------------------------------
202308
# Teams
203309
# --------------------------------------------------------------------------
@@ -343,112 +449,6 @@ def sync_current_teams_settings(self, dry: bool = False) -> None:
343449
else:
344450
logging.info("Team '%s' settings are in sync, no changes", team.name)
345451

346-
# --------------------------------------------------------------------------
347-
# Owners
348-
# --------------------------------------------------------------------------
349-
def _get_current_org_owners(self) -> None:
350-
"""Get all owners of the org"""
351-
# Reset the user list, then build up new list
352-
self.current_org_owners = []
353-
for member in self.org.get_members(role="admin"):
354-
self.current_org_owners.append(member)
355-
356-
def _check_configured_org_owners(self) -> bool:
357-
"""Check configured owners and make them lower-case for better
358-
comparison. Returns True if owners are well configured."""
359-
# Add configured owners if they are a list
360-
if isinstance(self.configured_org_owners, list):
361-
# Make all configured users lower-case
362-
self.configured_org_owners = [user.lower() for user in self.configured_org_owners]
363-
else:
364-
logging.warning(
365-
"The organisation owners are not configured as a proper list. Will not handle them."
366-
)
367-
self.configured_org_owners = []
368-
369-
if not self.configured_org_owners:
370-
logging.warning(
371-
"No owners for your GitHub organisation configured. Will not make any "
372-
"change regarding the ownership, and continue with the current owners: %s",
373-
", ".join([user.login for user in self.current_org_owners]),
374-
)
375-
return False
376-
377-
return True
378-
379-
def _is_user_authenticated_user(self, user: NamedUser) -> bool:
380-
"""Check if a given NamedUser is the authenticated user"""
381-
if user.login == self.gh.get_user().login:
382-
return True
383-
return False
384-
385-
def sync_org_owners(self, dry: bool = False, force: bool = False) -> None:
386-
"""Synchronise the organization owners"""
387-
# Get current and configured owners
388-
self._get_current_org_owners()
389-
390-
# Abort owner synchronisation if no owners are configured, or badly
391-
if not self._check_configured_org_owners():
392-
return
393-
394-
# Get differences between the current and configured owners
395-
owners_remove, owners_ok, owners_add = self.compare_two_lists(
396-
self.configured_org_owners, [user.login for user in self.current_org_owners]
397-
)
398-
# Compare configured (lower-cased) owners with lower-cased list of current owners
399-
if not owners_remove and not owners_add:
400-
logging.info("Organization owners are in sync, no changes")
401-
return
402-
403-
logging.debug(
404-
"Organization owners are not in sync. Config: '%s' vs. Current: '%s'",
405-
self.configured_org_owners,
406-
self.current_org_owners,
407-
)
408-
logging.debug(
409-
"Will remove %s, will not change %s, will add %s", owners_remove, owners_ok, owners_add
410-
)
411-
412-
# Add the missing owners
413-
for user in owners_add:
414-
if gh_user := self._resolve_gh_username(user, "<org owners>"):
415-
logging.info("Adding user '%s' as organization owner", gh_user.login)
416-
if not dry:
417-
self.org.add_to_members(gh_user, "admin")
418-
419-
# Remove the surplus owners
420-
for user in owners_remove:
421-
if gh_user := self._resolve_gh_username(user, "<org owners>"):
422-
logging.info(
423-
"User '%s' is not configured as organization owners. "
424-
"Will make them a normal member",
425-
gh_user.login,
426-
)
427-
# Handle authenticated user being the same as the one you want to degrade
428-
if self._is_user_authenticated_user(gh_user):
429-
logging.warning(
430-
"The user '%s' you want to remove from owners is the one you "
431-
"authenticated with. This may disrupt all further operations. "
432-
"Unless you run the program with --force, "
433-
"this operation will not be executed.",
434-
gh_user.login,
435-
)
436-
# Check if user forced this operation
437-
if force:
438-
logging.info(
439-
"You called the program with --force, "
440-
"so it will remove yourself from the owners"
441-
)
442-
else:
443-
continue
444-
445-
# Execute the degradation of the owner
446-
if not dry:
447-
self.org.add_to_members(gh_user, "member")
448-
449-
# Update the current organisation owners
450-
self._get_current_org_owners()
451-
452452
# --------------------------------------------------------------------------
453453
# Members
454454
# --------------------------------------------------------------------------

gh_org_mgr/manage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ def main():
116116
# Get current rate limit
117117
org.ratelimit()
118118

119+
# Synchronise organisation owners
120+
org.sync_org_owners(dry=args.dry, force=args.force)
119121
# Create teams that aren't present at Github yet
120122
org.create_missing_teams(dry=args.dry)
121123
# Configure general settings of teams
122124
org.sync_current_teams_settings(dry=args.dry)
123-
# Synchronise organisation owners
124-
org.sync_org_owners(dry=args.dry, force=args.force)
125125
# Synchronise the team memberships
126126
org.sync_teams_members(dry=args.dry)
127127
# Report about organisation members that do not belong to any team

0 commit comments

Comments
 (0)