|
| 1 | +from typing import Any |
| 2 | +from actions_toolkit import core as actions_toolkit |
| 3 | + |
| 4 | +from github.Repository import Repository |
| 5 | + |
| 6 | +from repo_manager.utils import get_organization |
| 7 | +from repo_manager.schemas.collaborator import Collaborator |
| 8 | + |
| 9 | + |
| 10 | +def diff_option(key: str, expected: Any, repo_value: Any) -> str | None: |
| 11 | + if expected is not None: |
| 12 | + if expected != repo_value: |
| 13 | + return f"{key} -- Expected: {expected} Found: {repo_value}" |
| 14 | + return None |
| 15 | + |
| 16 | + |
| 17 | +def check_collaborators( |
| 18 | + repo: Repository, collaborators: list[Collaborator] |
| 19 | +) -> tuple[bool, dict[str, list[str] | dict[str, Any]]]: |
| 20 | + """Checks a repo's environments vs our expected settings |
| 21 | +
|
| 22 | + Args: |
| 23 | + repo (Repository): [description] |
| 24 | + environments (List[Environment]): [description] |
| 25 | +
|
| 26 | + Returns: |
| 27 | + Tuple[bool, Optional[List[str]]]: [description] |
| 28 | + """ |
| 29 | + |
| 30 | + diff = {} |
| 31 | + |
| 32 | + expected_collab_usernames = { |
| 33 | + collaborator.name |
| 34 | + for collaborator in filter( |
| 35 | + lambda collaborator: collaborator.type == "User" and collaborator.exists, |
| 36 | + collaborators, |
| 37 | + ) |
| 38 | + } |
| 39 | + expected_collab_teamnames = { |
| 40 | + collaborator.name |
| 41 | + for collaborator in filter( |
| 42 | + lambda collaborator: collaborator.type == "Team" and collaborator.exists, |
| 43 | + collaborators, |
| 44 | + ) |
| 45 | + } |
| 46 | + repo_collab_users = repo.get_collaborators() |
| 47 | + repo_collab_teams = repo.get_teams() # __get_teams__(repo) |
| 48 | + repo_collab_usernames = {collaborator.login for collaborator in repo_collab_users} |
| 49 | + repo_collab_teamnames = {collaborator.slug for collaborator in repo_collab_teams} |
| 50 | + |
| 51 | + missing_users = list(expected_collab_usernames - repo_collab_usernames) |
| 52 | + missing_teams = list(expected_collab_teamnames - repo_collab_teamnames) |
| 53 | + if len(missing_users) + len(missing_teams) > 0: |
| 54 | + diff["missing"] = {} |
| 55 | + if len(missing_users) > 0: |
| 56 | + diff["missing"]["Users"] = missing_users |
| 57 | + if len(missing_teams) > 0: |
| 58 | + diff["missing"]["Teams"] = missing_teams |
| 59 | + |
| 60 | + extra_users = list( |
| 61 | + repo_collab_usernames.intersection( |
| 62 | + collaborator.name |
| 63 | + for collaborator in filter( |
| 64 | + lambda collaborator: collaborator.type == "User" and not collaborator.exists, |
| 65 | + collaborators, |
| 66 | + ) |
| 67 | + ) |
| 68 | + ) |
| 69 | + extra_teams = list( |
| 70 | + repo_collab_teamnames.intersection( |
| 71 | + collaborator.name |
| 72 | + for collaborator in filter( |
| 73 | + lambda collaborator: collaborator.type == "Team" and not collaborator.exists, |
| 74 | + collaborators, |
| 75 | + ) |
| 76 | + ) |
| 77 | + ) |
| 78 | + if len(extra_users) + len(extra_teams) > 0: |
| 79 | + diff["extra"] = {} |
| 80 | + if len(extra_users) > 0: |
| 81 | + diff["extra"]["Users"] = extra_users |
| 82 | + if len(extra_teams) > 0: |
| 83 | + diff["extra"]["Teams"] = extra_teams |
| 84 | + |
| 85 | + collaborators_to_check_values_on = {} |
| 86 | + collaborators_to_check_values_on["Users"] = list(expected_collab_usernames.intersection(repo_collab_usernames)) |
| 87 | + collaborators_to_check_values_on["Teams"] = list(expected_collab_teamnames.intersection(repo_collab_teamnames)) |
| 88 | + config_collaborator_dict = {collaborator.name: collaborator for collaborator in collaborators} |
| 89 | + repo_collab_dict = {"Users": {}, "Teams": {}} |
| 90 | + repo_collab_dict["Users"] = {collaborator.login: collaborator for collaborator in repo_collab_users} |
| 91 | + repo_collab_dict["Teams"] = {collaborator.slug: collaborator for collaborator in repo_collab_teams} |
| 92 | + perm_diffs = {"Users": {}, "Teams": {}} |
| 93 | + for collaborator_type in collaborators_to_check_values_on.keys(): |
| 94 | + for collaborator_name in collaborators_to_check_values_on[collaborator_type]: |
| 95 | + if collaborator_type == "Users": |
| 96 | + repo_value = getattr( |
| 97 | + repo_collab_dict[collaborator_type][collaborator_name].permissions, |
| 98 | + config_collaborator_dict[collaborator_name].permission, |
| 99 | + None, |
| 100 | + ) |
| 101 | + else: |
| 102 | + repo_value = ( |
| 103 | + getattr( |
| 104 | + repo_collab_dict[collaborator_type][collaborator_name], |
| 105 | + "permission", |
| 106 | + None, |
| 107 | + ) |
| 108 | + == config_collaborator_dict[collaborator_name].permission |
| 109 | + ) |
| 110 | + if repo_value is not True: |
| 111 | + perm_diffs[collaborator_type][collaborator_name] = diff_option( |
| 112 | + config_collaborator_dict[collaborator_name].permission, |
| 113 | + True, |
| 114 | + repo_value, |
| 115 | + ) |
| 116 | + |
| 117 | + if len(perm_diffs["Users"]) == 0: |
| 118 | + perm_diffs.pop("Users") |
| 119 | + |
| 120 | + if len(perm_diffs["Teams"]) == 0: |
| 121 | + perm_diffs.pop("Teams") |
| 122 | + |
| 123 | + if len(perm_diffs) > 0: |
| 124 | + diff["diff"] = perm_diffs |
| 125 | + |
| 126 | + if len(diff) > 0: |
| 127 | + return False, diff |
| 128 | + |
| 129 | + return True, None |
| 130 | + |
| 131 | + |
| 132 | +def update_collaborators(repo: Repository, collaborators: list[Collaborator], diffs: dict[str, Any]) -> set[str]: |
| 133 | + """Updates a repo's environments to match the expected settings |
| 134 | +
|
| 135 | + Args: |
| 136 | + repo (Repository): [description] |
| 137 | + environments (List[environment]): [description] |
| 138 | + diffs (Dictionary[string, Any]): List of all the summarized differences by environment name |
| 139 | +
|
| 140 | + Returns: |
| 141 | + set[str]: [description] |
| 142 | + """ |
| 143 | + errors = [] |
| 144 | + users_dict = { |
| 145 | + collaborator.name: collaborator |
| 146 | + for collaborator in filter( |
| 147 | + lambda collaborator: collaborator.type == "User" and collaborator.name, |
| 148 | + collaborators, |
| 149 | + ) |
| 150 | + } |
| 151 | + teams_dict = { |
| 152 | + collaborator.name: collaborator |
| 153 | + for collaborator in filter( |
| 154 | + lambda collaborator: collaborator.type == "Team" and collaborator.name, |
| 155 | + collaborators, |
| 156 | + ) |
| 157 | + } |
| 158 | + |
| 159 | + def switch(collaborator: Collaborator, diff_type: str) -> None: |
| 160 | + if diff_type == "missing": |
| 161 | + if collaborator.type == "User": |
| 162 | + repo.add_to_collaborators(collaborator.name, collaborator.permission) |
| 163 | + elif collaborator.type == "Team": |
| 164 | + get_organization().get_team_by_slug(collaborator.name).update_team_repository( |
| 165 | + repo, collaborator.permission |
| 166 | + ) |
| 167 | + actions_toolkit.info(f"Added collaborator {collaborator.name} with permission {collaborator.permission}.") |
| 168 | + elif diff_type == "extra": |
| 169 | + if collaborator.type == "User": |
| 170 | + repo.remove_from_collaborators(collaborator.name) |
| 171 | + elif collaborator.type == "Team": |
| 172 | + get_organization().get_team_by_slug(collaborator.name).remove_from_repos(repo) |
| 173 | + else: |
| 174 | + raise Exception(f"Modifying collaborators of type {collaborator.type} not currently supported") |
| 175 | + actions_toolkit.info(f"Removed collaborator {collaborator.name}.") |
| 176 | + elif diff_type == "diff": |
| 177 | + if collaborator.type == "User": |
| 178 | + repo.add_to_collaborators(collaborator.name, collaborator.permission) |
| 179 | + elif collaborator.type == "Team": |
| 180 | + get_organization().get_team_by_slug(collaborator.name).update_team_repository( |
| 181 | + repo, collaborator.permission |
| 182 | + ) |
| 183 | + else: |
| 184 | + raise Exception(f"Modifying collaborators of type {collaborator.type} not currently supported") |
| 185 | + actions_toolkit.info(f"Updated collaborator {collaborator.name} with permission {collaborator.permission}.") |
| 186 | + else: |
| 187 | + errors.append(f"Collaborator {collaborator} not found in expected collaborators") |
| 188 | + |
| 189 | + for diff_type in diffs.keys(): |
| 190 | + for collaborator_type in diffs[diff_type]: |
| 191 | + for collaborator in diffs[diff_type][collaborator_type]: |
| 192 | + if collaborator_type == "Users": |
| 193 | + switch(users_dict[collaborator], diff_type) |
| 194 | + elif collaborator_type == "Teams": |
| 195 | + switch(teams_dict[collaborator], diff_type) |
| 196 | + else: |
| 197 | + raise Exception(f"Modifying collaborators of type {collaborator_type} not currently supported") |
| 198 | + |
| 199 | + return errors |
0 commit comments