|
7 | 7 | import re
|
8 | 8 | import sys
|
9 | 9 | from contextlib import suppress
|
| 10 | +from subprocess import check_output |
10 | 11 |
|
11 | 12 | import github as gh
|
12 | 13 | from github.Repository import Repository
|
13 | 14 |
|
14 | 15 | BRANCHES = ['master']
|
15 | 16 | CODEOWNERS = '.github/CODEOWNERS'
|
| 17 | +USER = check_output(['git', 'config', 'user.name']).decode().strip() |
| 18 | +EMAIL = check_output(['git', 'config', 'user.email']).decode().strip() |
| 19 | +assert USER and len(USER.splitlines()) == 1 |
| 20 | +assert EMAIL and len(EMAIL.splitlines()) == 1 |
| 21 | +MESSAGE = f"""Set team owner |
16 | 22 |
|
| 23 | +Signed-off-by: {USER} <{EMAIL}> |
| 24 | +""" |
17 | 25 |
|
18 | 26 | def to_gh_team(maintainer: str):
|
19 | 27 | return '@xcp-ng-rpms/' + re.sub(r'\W+', '-', maintainer.lower())
|
@@ -42,17 +50,23 @@ def set_gh_code_owners(repo: Repository, rpm, force: bool) -> bool:
|
42 | 50 | ok = True
|
43 | 51 | for branch in BRANCHES:
|
44 | 52 | current_content = None
|
| 53 | + current_content_sha = '' |
45 | 54 | with suppress(gh.UnknownObjectException):
|
46 |
| - current_content = repo.get_contents(CODEOWNERS, branch).decoded_content.decode() # type: ignore |
47 |
| - if current_content is None or (force and current_content != content): |
48 |
| - action = "creating" if current_content is None else "updating" |
49 |
| - print(f'{action} {pkg} CODEOWNERS file in {branch}...', end='', file=sys.stderr) |
50 |
| - repo.create_file(CODEOWNERS, "set team owner", content, branch) |
| 55 | + gh_content = repo.get_contents(CODEOWNERS, branch) |
| 56 | + current_content_sha = gh_content.sha # type: ignore |
| 57 | + current_content = gh_content.decoded_content.decode() # type: ignore |
| 58 | + if current_content is None: |
| 59 | + print(f'creating {pkg} CODEOWNERS file in {branch}...', end='', file=sys.stderr, flush=True) |
| 60 | + repo.create_file(CODEOWNERS, MESSAGE, content, branch) |
| 61 | + print(' done', file=sys.stderr) |
| 62 | + elif force and current_content != content: |
| 63 | + print(f'updating {pkg} CODEOWNERS file in {branch}...', end='', file=sys.stderr, flush=True) |
| 64 | + repo.update_file(CODEOWNERS, MESSAGE, content, current_content_sha, branch) |
51 | 65 | print(' done', file=sys.stderr)
|
52 | 66 | elif current_content == content:
|
53 |
| - print(f'{pkg} CODEOWNERS is already OK in {branch}', file=sys.stderr) |
| 67 | + print(f'{pkg} CODEOWNERS is already OK in {branch}', file=sys.stderr, flush=True) |
54 | 68 | else:
|
55 |
| - print(f'error: {pkg} CODEOWNERS is not synced in {branch}', file=sys.stderr) |
| 69 | + print(f'error: {pkg} CODEOWNERS is not synced in {branch}', file=sys.stderr, flush=True) |
56 | 70 | print(diff(current_content, content))
|
57 | 71 | ok = False
|
58 | 72 | return ok
|
|
0 commit comments