Skip to content

Commit f440fba

Browse files
Split business logic in a sequence of functions
1 parent d58d805 commit f440fba

File tree

1 file changed

+44
-25
lines changed

1 file changed

+44
-25
lines changed

main.py

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,10 @@ class Configuration(NamedTuple):
381381
default_repo_settings: Repository
382382
repos_by_id: Dict[int, Repository]
383383
repos_by_name: Dict[str, Repository]
384+
fname: Optional[str]
384385

385386
@staticmethod
386-
def from_toml_dict(data: Dict[str, Any]) -> Configuration:
387+
def from_toml_dict(data: Dict[str, Any], fname: Optional[str]) -> Configuration:
387388
org = Organization.from_toml_dict(data["organization"])
388389
members = {OrganizationMember.from_toml_dict(m) for m in data["member"]}
389390
teams = {Team.from_toml_dict(m) for m in data["team"]}
@@ -408,13 +409,14 @@ def from_toml_dict(data: Dict[str, Any]) -> Configuration:
408409
default_repo_settings=default_repo_settings,
409410
repos_by_id=repos_by_id,
410411
repos_by_name=repos_by_name,
412+
fname=fname,
411413
)
412414

413415
@staticmethod
414416
def from_toml_file(fname: str) -> Configuration:
415417
with open(fname, "rb") as f:
416418
data = tomllib.load(f)
417-
return Configuration.from_toml_dict(data)
419+
return Configuration.from_toml_dict(data, fname)
418420

419421
def get_repository_target(self, actual: Repository) -> Repository:
420422
"""
@@ -955,48 +957,55 @@ def print_team_members_diff(
955957
return has_diff
956958

957959

958-
def has_changes(target: Configuration, client: GithubClient) -> bool:
959-
has_changes = False
960-
org_name = target.organization.name
961-
962-
actual_repos = set(client.get_organization_repositories(org_name))
960+
def diff_repos(target: Configuration, client: GithubClient) -> bool:
961+
actual_repos = set(client.get_organization_repositories(target.organization.name))
963962
target_repos = set(target.repos_by_id.values()) | {
964963
target.get_repository_target(r) for r in actual_repos
965964
}
966965
repos_diff = Diff.new(target=target_repos, actual=actual_repos)
967-
has_changes |= repos_diff.print_diff(
968-
f"The following repositories are specified in {target_fname} but not present on GitHub:",
966+
967+
return repos_diff.print_diff(
968+
f"The following repositories are specified in {target.fname} but not present on GitHub:",
969969
# Even though we generate the targets form the actuals using the default
970970
# settings, it can happen that we match on repository name but not id
971971
# (when the id in the config file is wrong). Then the repo will be
972972
# missing from the targets.
973-
f"The following repositories are not specified in {target_fname} but present on GitHub:",
974-
f"The following repositories on GitHub need to be changed to match {target_fname}:",
973+
f"The following repositories are not specified in {target.fname} but present on GitHub:",
974+
f"The following repositories on GitHub need to be changed to match {target.fname}:",
975975
)
976976

977-
current_org = client.get_organization(org_name)
978-
if current_org != target.organization:
979-
has_changes = True
977+
978+
def diff_org(target: Configuration, client: GithubClient) -> bool:
979+
current_org = client.get_organization(target.organization.name)
980+
has_change = current_org != target.organization
981+
if has_change:
980982
print("The organization-level settings need to be changed as follows:\n")
981983
print_simple_diff(
982984
actual=current_org.format_toml(),
983985
target=target.organization.format_toml(),
984986
)
987+
return has_change
988+
985989

986-
current_members = set(client.get_organization_members(org_name))
990+
def diff_members(target: Configuration, client: GithubClient) -> bool:
991+
current_members = set(client.get_organization_members(target.organization.name))
987992
members_diff = Diff.new(target=target.members, actual=current_members)
988-
has_changes |= members_diff.print_diff(
989-
f"The following members are specified in {target_fname} but not a member of the GitHub organization:",
990-
f"The following members are not specified in {target_fname} but are a member of the GitHub organization:",
991-
f"The following members on GitHub need to be changed to match {target_fname}:",
993+
return members_diff.print_diff(
994+
f"The following members are specified in {target.fname} but not a member of the GitHub organization:",
995+
f"The following members are not specified in {target.fname} but are a member of the GitHub organization:",
996+
f"The following members on GitHub need to be changed to match {target.fname}:",
992997
)
993998

994-
current_teams = set(client.get_organization_teams(org_name))
999+
1000+
def diff_teams(target: Configuration, client: GithubClient) -> bool:
1001+
has_changes = False
1002+
1003+
current_teams = set(client.get_organization_teams(target.organization.name))
9951004
teams_diff = Diff.new(target=target.teams, actual=current_teams)
9961005
has_changes |= teams_diff.print_diff(
997-
f"The following teams specified in {target_fname} are not present on GitHub:",
998-
f"The following teams are not specified in {target_fname} but are present on GitHub:",
999-
f"The following teams on GitHub need to be changed to match {target_fname}:",
1006+
f"The following teams specified in {target.fname} are not present on GitHub:",
1007+
f"The following teams are not specified in {target.fname} but are present on GitHub:",
1008+
f"The following teams on GitHub need to be changed to match {target.fname}:",
10001009
)
10011010

10021011
# For all the teams which we want to exist, and which do actually exist,
@@ -1009,16 +1018,26 @@ def has_changes(target: Configuration, client: GithubClient) -> bool:
10091018
for team in existing_desired_teams:
10101019
has_changes |= print_team_members_diff(
10111020
team_name=team.name,
1012-
target_fname=target_fname,
1021+
target_fname=target.fname,
10131022
target_members={
10141023
m for m in target.team_memberships if m.team_name == team.name
10151024
},
1016-
actual_members=set(client.get_team_members(org_name, team)),
1025+
actual_members=set(client.get_team_members(target.organization.name, team)),
10171026
)
10181027

10191028
return has_changes
10201029

10211030

1031+
def has_changes(target: Configuration, client: GithubClient) -> bool:
1032+
has_changes = False
1033+
has_changes |= diff_repos(target, client)
1034+
has_changes |= diff_org(target, client)
1035+
has_changes |= diff_members(target, client)
1036+
has_changes |= diff_teams(target, client)
1037+
1038+
return has_changes
1039+
1040+
10221041
def main() -> None:
10231042
if "--help" in sys.argv:
10241043
print(__doc__)

0 commit comments

Comments
 (0)