Skip to content

Commit 230c35a

Browse files
committed
add additional error handling and only fetch github info once
1 parent 6ce031e commit 230c35a

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

normalize_repos/normalize_repos.py

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
# Standard library
1010
import logging
11+
import sys
12+
import traceback
1113

1214
# Third-party
1315
from github import UnknownObjectException
@@ -26,11 +28,24 @@
2628
log.reset_handler()
2729

2830

31+
class ScriptError(Exception):
32+
def __init__(self, message, code=None):
33+
self.code = code if code else 1
34+
message = "({}) {}".format(self.code, message)
35+
super(ScriptError, self).__init__(message)
36+
37+
2938
def get_cc_repos(github):
3039
cc = get_cc_organization(github)
3140
return cc.get_repos()
3241

3342

43+
def set_repo_labels(repos):
44+
logger.log(logging.INFO, "Syncing labels...")
45+
set_labels(repos, *get_labels())
46+
logger.log(log.SUCCESS, "done.")
47+
48+
3449
def is_engineering_project(repo):
3550
try:
3651
contents = repo.get_contents(".cc-metadata.yml")
@@ -65,15 +80,34 @@ def update_branch_protection(repo):
6580
print(f'Skipping branch protection for exempt repo: "{repo.name}"')
6681

6782

68-
if __name__ == "__main__":
69-
logger.log(logging.INFO, "Starting normalization")
70-
logger.log(logging.INFO, "Syncing labels...")
71-
set_labels(*get_labels())
72-
logger.log(log.SUCCESS, "done.")
83+
def update_branches(repos):
84+
for repo in repos:
85+
# TODO: Set up automatic deletion of merged branches
86+
update_branch_protection(repo)
87+
7388

89+
def main():
90+
logger.log(logging.INFO, "Starting normalization")
7491
github = set_up_github_client()
7592
repos = get_cc_repos(github)
93+
set_repo_labels(repos)
94+
update_branches(repos)
7695

77-
for repo in repos:
78-
# TODO: Set up automatic deletion of merged branches
79-
update_branch_protection(repo)
96+
97+
if __name__ == "__main__":
98+
try:
99+
main()
100+
except SystemExit as e:
101+
sys.exit(e.code)
102+
except KeyboardInterrupt:
103+
logger.log(logging.INFO, "Halted via KeyboardInterrupt.")
104+
sys.exit(130)
105+
except ScriptError:
106+
error_type, error_value, error_traceback = sys.exc_info()
107+
logger.log(logging.CRITICAL, f"{error_value}")
108+
sys.exit(error_value.code)
109+
except Exception:
110+
logger.log(
111+
logging.ERROR, "Unhandled exception: {traceback.print_exc()}"
112+
)
113+
sys.exit(1)

normalize_repos/set_labels.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import logging
33

44
# Local/library specific
5-
from utils import set_up_github_client, get_cc_organization
65
import log
76

87

@@ -69,22 +68,13 @@ def map_repo_to_labels(repo, final_labels, non_destructive=True):
6968
logger.log(log.SUCCESS, "done.")
7069

7170

72-
def set_labels(standard_labels, repo_specific_labels):
71+
def set_labels(repos, standard_labels, repo_specific_labels):
7372
"""
7473
Set labels on all repos for the organisation. This is the main entrypoint of
7574
the module.
7675
"""
7776

78-
logger.log(logging.INFO, "Setting up...")
79-
client = set_up_github_client()
80-
organization = get_cc_organization(client)
81-
logger.log(log.SUCCESS, "done.")
82-
83-
logger.log(logging.INFO, "Fetching repos...")
84-
repos = list(organization.get_repos())
85-
logger.log(log.SUCCESS, f"done. Found {len(repos)} repos.")
86-
87-
for repo in repos:
77+
for repo in list(repos):
8878
logger.log(logging.INFO, f"Getting labels for repo '{repo.name}'...")
8979
labels = standard_labels + repo_specific_labels.get(repo.name, [])
9080
logger.log(log.SUCCESS, f"done. Found {len(labels)} labels.")

0 commit comments

Comments
 (0)