|
| 1 | +"""A GitHub Action to suggest removal of non-organization members from CODEOWNERS files.""" |
| 2 | + |
| 3 | +import uuid |
| 4 | + |
| 5 | +import auth |
| 6 | +import env |
| 7 | +import github3 |
| 8 | + |
| 9 | + |
| 10 | +def main(): # pragma: no cover |
| 11 | + """Run the main program""" |
| 12 | + |
| 13 | + # Get the environment variables |
| 14 | + ( |
| 15 | + organization, |
| 16 | + repository_list, |
| 17 | + token, |
| 18 | + ghe, |
| 19 | + exempt_repositories_list, |
| 20 | + dry_run, |
| 21 | + title, |
| 22 | + body, |
| 23 | + commit_message, |
| 24 | + ) = env.get_env_vars() |
| 25 | + |
| 26 | + # Auth to GitHub.com or GHE |
| 27 | + github_connection = auth.auth_to_github(token, ghe) |
| 28 | + pull_count = 0 |
| 29 | + eligble_for_pr_count = 0 |
| 30 | + |
| 31 | + # Get the repositories from the organization or list of repositories |
| 32 | + repos = get_repos_iterator(organization, repository_list, github_connection) |
| 33 | + |
| 34 | + for repo in repos: |
| 35 | + # Check if the repository is in the exempt_repositories_list |
| 36 | + if repo.full_name in exempt_repositories_list: |
| 37 | + print(f"Skipping {repo.full_name} as it is in the exempt_repositories_list") |
| 38 | + continue |
| 39 | + |
| 40 | + # Check to see if repository is archived |
| 41 | + if repo.archived: |
| 42 | + print(f"Skipping {repo.full_name} as it is archived") |
| 43 | + continue |
| 44 | + |
| 45 | + # Check to see if repository has a CODEOWNERS file |
| 46 | + file_changed = False |
| 47 | + codeowners_file_contents = None |
| 48 | + codeowners_filepath = None |
| 49 | + try: |
| 50 | + if repo.file_contents(".github/CODEOWNERS").size > 0: |
| 51 | + codeowners_file_contents = repo.file_contents(".github/CODEOWNERS") |
| 52 | + codeowners_filepath = ".github/CODEOWNERS" |
| 53 | + except github3.exceptions.NotFoundError: |
| 54 | + pass |
| 55 | + try: |
| 56 | + if repo.file_contents("CODEOWNERS").size > 0: |
| 57 | + codeowners_file_contents = repo.file_contents("CODEOWNERS") |
| 58 | + codeowners_filepath = "CODEOWNERS" |
| 59 | + except github3.exceptions.NotFoundError: |
| 60 | + pass |
| 61 | + try: |
| 62 | + if repo.file_contents("docs/CODEOWNERS").size > 0: |
| 63 | + codeowners_file_contents = repo.file_contents("docs/CODEOWNERS") |
| 64 | + codeowners_filepath = "docs/CODEOWNERS" |
| 65 | + except github3.exceptions.NotFoundError: |
| 66 | + pass |
| 67 | + |
| 68 | + if not codeowners_file_contents: |
| 69 | + print(f"Skipping {repo.full_name} as it does not have a CODEOWNERS file") |
| 70 | + continue |
| 71 | + |
| 72 | + # Extract the usernames from the CODEOWNERS file |
| 73 | + usernames = get_usernames_from_codeowners(codeowners_file_contents) |
| 74 | + |
| 75 | + for username in usernames: |
| 76 | + # Check to see if the username is a member of the organization |
| 77 | + if not github_connection.organization(organization).has_member(username): |
| 78 | + print( |
| 79 | + f"\t{username} is not a member of {organization}. Suggest removing them from {repo.full_name}" |
| 80 | + ) |
| 81 | + if not dry_run: |
| 82 | + # Remove that username from the codeowners_file_contents |
| 83 | + file_changed = True |
| 84 | + codeowners_file_contents = codeowners_file_contents.decoded.replace( |
| 85 | + f"@{username}", "" |
| 86 | + ) |
| 87 | + |
| 88 | + # Update the CODEOWNERS file if usernames were removed |
| 89 | + if file_changed: |
| 90 | + eligble_for_pr_count += 1 |
| 91 | + try: |
| 92 | + pull = commit_changes( |
| 93 | + title, |
| 94 | + body, |
| 95 | + repo, |
| 96 | + codeowners_file_contents, |
| 97 | + commit_message, |
| 98 | + codeowners_filepath, |
| 99 | + ) |
| 100 | + pull_count += 1 |
| 101 | + print(f"\tCreated pull request {pull.html_url}") |
| 102 | + except github3.exceptions.NotFoundError: |
| 103 | + print("\tFailed to create pull request. Check write permissions.") |
| 104 | + continue |
| 105 | + |
| 106 | + # Report the statistics from this run |
| 107 | + print(f"Found {eligble_for_pr_count} users to remove") |
| 108 | + print(f"Created {pull_count} pull requests successfully") |
| 109 | + |
| 110 | + |
| 111 | +def get_repos_iterator(organization, repository_list, github_connection): |
| 112 | + """Get the repositories from the organization or list of repositories""" |
| 113 | + repos = [] |
| 114 | + if organization and not repository_list: |
| 115 | + repos = github_connection.organization(organization).repositories() |
| 116 | + else: |
| 117 | + # Get the repositories from the repository_list |
| 118 | + for repo in repository_list: |
| 119 | + repos.append( |
| 120 | + github_connection.repository(repo.split("/")[0], repo.split("/")[1]) |
| 121 | + ) |
| 122 | + |
| 123 | + return repos |
| 124 | + |
| 125 | + |
| 126 | +def get_usernames_from_codeowners(codeowners_file_contents): |
| 127 | + """Extract the usernames from the CODEOWNERS file""" |
| 128 | + usernames = [] |
| 129 | + for line in codeowners_file_contents.decoded.splitlines(): |
| 130 | + # skip comments |
| 131 | + if line.startswith("#"): |
| 132 | + continue |
| 133 | + # skip empty lines |
| 134 | + if not line.strip(): |
| 135 | + continue |
| 136 | + # If the line has an @ symbol, grab the word with the @ in it and add it to the list |
| 137 | + if "@" in line: |
| 138 | + usernames.append(line.split("@")[1].split()[0]) |
| 139 | + return usernames |
| 140 | + |
| 141 | + |
| 142 | +def commit_changes( |
| 143 | + title, body, repo, codeowners_file_contents, commit_message, codeowners_filepath |
| 144 | +): |
| 145 | + """Commit the changes to the repo and open a pull reques and return the pull request object""" |
| 146 | + default_branch = repo.default_branch |
| 147 | + # Get latest commit sha from default branch |
| 148 | + default_branch_commit = repo.ref("heads/" + default_branch).object.sha |
| 149 | + front_matter = "refs/heads/" |
| 150 | + branch_name = "codeowners-" + str(uuid.uuid4()) |
| 151 | + repo.create_ref(front_matter + branch_name, default_branch_commit) |
| 152 | + repo.create_file( |
| 153 | + path=codeowners_filepath, |
| 154 | + message=commit_message, |
| 155 | + content=codeowners_file_contents.encode(), # Convert to bytes object |
| 156 | + branch=branch_name, |
| 157 | + ) |
| 158 | + |
| 159 | + pull = repo.create_pull( |
| 160 | + title=title, body=body, head=branch_name, base=repo.default_branch |
| 161 | + ) |
| 162 | + return pull |
| 163 | + |
| 164 | + |
| 165 | +if __name__ == "__main__": # pragma: no cover |
| 166 | + main() |
0 commit comments