|
8 | 8 |
|
9 | 9 | # Standard library
|
10 | 10 | import logging
|
| 11 | +import sys |
| 12 | +import traceback |
11 | 13 |
|
12 | 14 | # Third-party
|
13 | 15 | from github import UnknownObjectException
|
|
26 | 28 | log.reset_handler()
|
27 | 29 |
|
28 | 30 |
|
| 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 | + |
29 | 38 | def get_cc_repos(github):
|
30 | 39 | cc = get_cc_organization(github)
|
31 | 40 | return cc.get_repos()
|
32 | 41 |
|
33 | 42 |
|
| 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 | + |
34 | 49 | def is_engineering_project(repo):
|
35 | 50 | try:
|
36 | 51 | contents = repo.get_contents(".cc-metadata.yml")
|
@@ -65,15 +80,34 @@ def update_branch_protection(repo):
|
65 | 80 | print(f'Skipping branch protection for exempt repo: "{repo.name}"')
|
66 | 81 |
|
67 | 82 |
|
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 | + |
73 | 88 |
|
| 89 | +def main(): |
| 90 | + logger.log(logging.INFO, "Starting normalization") |
74 | 91 | github = set_up_github_client()
|
75 | 92 | repos = get_cc_repos(github)
|
| 93 | + set_repo_labels(repos) |
| 94 | + update_branches(repos) |
76 | 95 |
|
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) |
0 commit comments