Skip to content

Commit

Permalink
Add non zero exit 2 when diff is found (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickhilber authored Feb 22, 2023
1 parent 2d7e982 commit 6fc4782
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,12 @@ def print_diff(
header_to_add: str,
header_to_remove: str,
header_to_change: str,
) -> None:
) -> bool:
has_diff = False

if len(self.to_add) > 0:
has_diff = True

print(header_to_add)
for entry in self.to_add:
print()
Expand All @@ -886,6 +890,8 @@ def print_diff(
print()

if len(self.to_remove) > 0:
has_diff = True

print(header_to_remove)
for entry in self.to_remove:
print()
Expand All @@ -894,6 +900,8 @@ def print_diff(
print()

if len(self.to_change) > 0:
has_diff = True

print(header_to_change)
for change in self.to_change:
print()
Expand All @@ -904,36 +912,48 @@ def print_diff(

print()

return has_diff

def print_team_members_diff(
*,
team_name: str,
target_fname: str,
target_members: Set[TeamMember],
actual_members: Set[TeamMember],
) -> None:
) -> bool:
has_diff = False
members_diff = Diff.new(
target=target_members,
actual=actual_members,
)

if len(members_diff.to_remove) > 0:
has_diff = True

print(
f"The following members of team '{team_name}' are not specified "
f"in {target_fname}, but are present on GitHub:\n"
)
for member in sorted(members_diff.to_remove):
print(f" {member.user_name}")

print()

if len(members_diff.to_add) > 0:
has_diff = True

print(
f"The following members of team '{team_name}' are specified "
f"in {target_fname}, but are not present on GitHub:\n"
)
for member in sorted(members_diff.to_add):
print(f" {member.user_name}")

print()

return has_diff



def main() -> None:
if "--help" in sys.argv:
Expand All @@ -951,6 +971,7 @@ def main() -> None:
print("See also --help.")
sys.exit(1)

has_changes = False
target_fname = sys.argv[1]
target = Configuration.from_toml_file(target_fname)
org_name = target.organization.name
Expand All @@ -962,7 +983,7 @@ def main() -> None:
target.get_repository_target(r) for r in actual_repos
}
repos_diff = Diff.new(target=target_repos, actual=actual_repos)
repos_diff.print_diff(
has_changes |= repos_diff.print_diff(
f"The following repositories are specified in {target_fname} but not present on GitHub:",
# Even though we generate the targets form the actuals using the default
# settings, it can happen that we match on repository name but not id
Expand All @@ -974,6 +995,7 @@ def main() -> None:

current_org = client.get_organization(org_name)
if current_org != target.organization:
has_changes = True
print("The organization-level settings need to be changed as follows:\n")
print_simple_diff(
actual=current_org.format_toml(),
Expand All @@ -982,15 +1004,15 @@ def main() -> None:

current_members = set(client.get_organization_members(org_name))
members_diff = Diff.new(target=target.members, actual=current_members)
members_diff.print_diff(
has_changes |= members_diff.print_diff(
f"The following members are specified in {target_fname} but not a member of the GitHub organization:",
f"The following members are not specified in {target_fname} but are a member of the GitHub organization:",
f"The following members on GitHub need to be changed to match {target_fname}:",
)

current_teams = set(client.get_organization_teams(org_name))
teams_diff = Diff.new(target=target.teams, actual=current_teams)
teams_diff.print_diff(
has_changes |= teams_diff.print_diff(
f"The following teams specified in {target_fname} are not present on GitHub:",
f"The following teams are not specified in {target_fname} but are present on GitHub:",
f"The following teams on GitHub need to be changed to match {target_fname}:",
Expand All @@ -1004,7 +1026,7 @@ def main() -> None:
team for team in current_teams if team.name in target_team_names
]
for team in existing_desired_teams:
print_team_members_diff(
has_changes |= print_team_members_diff(
team_name=team.name,
target_fname=target_fname,
target_members={
Expand All @@ -1013,6 +1035,9 @@ def main() -> None:
actual_members=set(client.get_team_members(org_name, team)),
)

if has_changes:
sys.exit(2)


if __name__ == "__main__":
main()

0 comments on commit 6fc4782

Please sign in to comment.