|
| 1 | +import os, yaml |
| 2 | +from github import Github, GithubException, Auth, Repository |
| 3 | + |
| 4 | +GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN") |
| 5 | +ORGANIZATION = os.environ.get("GITHUB_ORG") |
| 6 | +REPO_CONFIG = os.environ.get("REPO_CONFIG") |
| 7 | +REPO_NAME = "0-test-AWS-12490" |
| 8 | +TEST_REPO = "github-workflow-common" |
| 9 | + |
| 10 | +# Get access to the organization using GTIHUB_TOKEN. |
| 11 | +auth = Auth.Token(f"{GITHUB_TOKEN}") |
| 12 | +g = Github(auth=auth) |
| 13 | +g.get_user().login |
| 14 | +org = g.get_organization(f"{ORGANIZATION}") |
| 15 | + |
| 16 | +def get_repo(org, repo_name): |
| 17 | + """ |
| 18 | + Fetches a repo from GitHub organization. |
| 19 | + """ |
| 20 | + try: |
| 21 | + repo = org.get_repo(repo_name) |
| 22 | + if repo.name: |
| 23 | + print(f"Repository `{repo_name}` exists within GitHub {org}") |
| 24 | + return repo |
| 25 | + except GithubException as e: |
| 26 | + if e.status == 404: |
| 27 | + print(f"Repository `{repo_name}` does not exists within GitHub {org}") |
| 28 | + return None |
| 29 | + else: |
| 30 | + print(f"Error fetching repository from GitHub {org} - {str(e)}") |
| 31 | + raise e |
| 32 | + |
| 33 | +def create_repository(org, repo_name, description): |
| 34 | + """ |
| 35 | + Creates Github repository. |
| 36 | + """ |
| 37 | + repo = get_repo(org, repo_name) |
| 38 | + if repo == None: |
| 39 | + print (f"Creating private github repository `{repo_name}`") |
| 40 | + create_repo = org.create_repo( |
| 41 | + allow_auto_merge=False, |
| 42 | + allow_merge_commit=True, |
| 43 | + allow_rebase_merge=False, |
| 44 | + allow_squash_merge=False, |
| 45 | + allow_update_branch=False, |
| 46 | + delete_branch_on_merge=True, |
| 47 | + description=description, |
| 48 | + has_issues=True, |
| 49 | + has_wiki=True, |
| 50 | + has_projects=False, |
| 51 | + name=repo_name, |
| 52 | + private=True, |
| 53 | + visibility="internal" |
| 54 | + ) |
| 55 | + else: |
| 56 | + print (f"Update private github repository `{repo_name}`") |
| 57 | + edit_repo = repo.edit( |
| 58 | + allow_auto_merge=False, |
| 59 | + allow_merge_commit=True, |
| 60 | + allow_rebase_merge=False, |
| 61 | + allow_squash_merge=False, |
| 62 | + allow_update_branch=False, |
| 63 | + delete_branch_on_merge=True, |
| 64 | + description=description, |
| 65 | + has_issues=True, |
| 66 | + has_wiki=False, |
| 67 | + has_projects=False, |
| 68 | + name=repo_name, |
| 69 | + private=True, |
| 70 | + visibility="internal" |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +def get_pull_requests(org, repo_name): |
| 75 | + """ |
| 76 | + Get open pull requests for Github repository. |
| 77 | + """ |
| 78 | + pr_list=[] |
| 79 | + repo = get_repo(org, repo_name) |
| 80 | + prs = repo.get_pulls(state='open', sort='created', base='master') |
| 81 | + if repo != None: |
| 82 | + print (f"List of open PRs for github repository `{repo_name}`") |
| 83 | + for pr in prs: |
| 84 | + pr_list.append(pr.number) |
| 85 | + return pr_list |
| 86 | + |
| 87 | +def delete_repository(org, repo_name): |
| 88 | + """ |
| 89 | + Deletes Github repository. |
| 90 | + """ |
| 91 | + repo = get_repo(org, repo_name) |
| 92 | + if repo != None: |
| 93 | + print (f"Deleting github repository `{repo_name}`") |
| 94 | + repo.delete() |
| 95 | + |
| 96 | +def get_open_issues(org, repo_name): |
| 97 | + """ |
| 98 | + Get all issues for a Github repository. |
| 99 | + """ |
| 100 | + print (f"List of open issues for repository `{repo_name}`") |
| 101 | + if repo != None: |
| 102 | + print (f"Issues for repository `{repo_name}`:") |
| 103 | + open_issues = repo.get_issues(state='open') |
| 104 | + for issue in open_issues: |
| 105 | + print(issue) |
| 106 | + |
| 107 | +def get_labels(org, repo_name): |
| 108 | + """ |
| 109 | + Get all labels for a Github repository. |
| 110 | + """ |
| 111 | + repo = get_repo(org, repo_name) |
| 112 | + if repo != None: |
| 113 | + print (f"Labels for repository `{repo_name}`:") |
| 114 | + labels = repo.get_labels() |
| 115 | + for label in labels: |
| 116 | + print(label) |
| 117 | + |
| 118 | +def repo_config(repo_config): |
| 119 | + """ |
| 120 | + Used to create repositories based on YAML config. |
| 121 | + """ |
| 122 | + with open(f"{repo_config}", 'r') as f: |
| 123 | + try: |
| 124 | + repos = yaml.load(f, Loader=yaml.FullLoader) |
| 125 | + f.close() |
| 126 | + except yaml.YAMLError as e: |
| 127 | + print("Invalid YAML", e) |
| 128 | + |
| 129 | + config = list(repos["repositories"].values()) |
| 130 | + for repo in config: |
| 131 | + # print (org, repo["name"], repo["description"]) |
| 132 | + create_repository(org, repo["name"], repo["description"]) |
| 133 | + |
| 134 | +def repo_decom(repo_config): |
| 135 | + """ |
| 136 | + NOTE: This is used for DEMO purposes only. |
| 137 | + To delete repositories based on YAML config. |
| 138 | + For the real repositories this needs to be adjusted. |
| 139 | + """ |
| 140 | + with open(f"{repo_config}", 'r') as f: |
| 141 | + try: |
| 142 | + repos = yaml.load(f, Loader=yaml.FullLoader) |
| 143 | + f.close() |
| 144 | + except yaml.YAMLError as e: |
| 145 | + print("Invalid YAML", e) |
| 146 | + |
| 147 | + config = list(repos["repositories"].values()) |
| 148 | + for repo in config: |
| 149 | + delete_repository(org, repo["name"]) |
| 150 | + |
| 151 | +# Test functions. |
| 152 | +# get_open_issues(org, TEST_REPO) |
| 153 | +# get_labels(org, TEST_REPO) |
| 154 | +# get_pull_requests(org, TEST_REPO) |
| 155 | +# repo_config(REPO_CONFIG) |
| 156 | +repo_decom(REPO_CONFIG) |
0 commit comments