Skip to content

Commit dcc3385

Browse files
authored
Merge pull request #728 from xcp-ng/gln/build-last-commit-protected-uzyx
Ensure to build the last commit of the expected branch for protected targets
2 parents 2648316 + 4ba9c1e commit dcc3385

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

scripts/koji/koji_build.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616

1717
TIME_FORMAT = '%Y-%m-%d-%H-%M-%S'
1818

19+
# target -> required branch
20+
PROTECTED_TARGETS = {
21+
"v8.2-ci": "8.2",
22+
"v8.2-fasttrack": "8.2",
23+
"v8.2-incoming": "8.2",
24+
"v8.3-ci": "master",
25+
"v8.3-fasttrack": "master",
26+
"v8.3-incoming": "master",
27+
}
28+
1929
@contextmanager
2030
def cd(dir):
2131
"""Change to a directory temporarily. To be used in a with statement."""
@@ -36,10 +46,23 @@ def check_git_repo(dirpath):
3646
with cd(dirpath):
3747
return subprocess.run(['git', 'diff-index', '--quiet', 'HEAD', '--']).returncode == 0
3848

39-
def check_commit_is_available_remotely(dirpath, hash):
49+
def check_commit_is_available_remotely(dirpath, hash, target, warn):
4050
with cd(dirpath):
4151
if not subprocess.check_output(['git', 'branch', '-r', '--contains', hash]):
4252
raise Exception("The current commit is not available in the remote repository")
53+
try:
54+
expected_branch = PROTECTED_TARGETS.get(target)
55+
if (
56+
expected_branch is not None
57+
and not is_remote_branch_commit(dirpath, hash, expected_branch)
58+
):
59+
raise Exception(f"The current commit is not the last commit in the remote branch {expected_branch}.\n"
60+
f"This is required when using the protected target {target}.\n")
61+
except Exception as e:
62+
if warn:
63+
print(f"warning: {e}", flush=True)
64+
else:
65+
raise e
4366

4467
def get_repo_and_commit_info(dirpath):
4568
with cd(dirpath):
@@ -125,6 +148,13 @@ def push_bumped_release(git_repo, target, test_build_id, pre_build_id):
125148
commit = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode().strip()
126149
return commit
127150

151+
def is_remote_branch_commit(git_repo, sha, branch):
152+
with cd(git_repo):
153+
remote_sha = (
154+
subprocess.check_output(['git', 'ls-remote', 'origin', f'refs/heads/{branch}']).decode().strip().split()[0]
155+
)
156+
return sha == remote_sha
157+
128158
def main():
129159
parser = argparse.ArgumentParser(
130160
description='Build a package or chain-build several from local git repos for RPM sources'
@@ -135,6 +165,7 @@ def main():
135165
'a chained build will be started in the order of the arguments')
136166
parser.add_argument('--scratch', action="store_true", help='Perform scratch build')
137167
parser.add_argument('--nowait', action="store_true", help='Do not wait for the build to end')
168+
parser.add_argument('--force', action="store_true", help='Bypass sanity checks')
138169
parser.add_argument(
139170
'--test-build',
140171
metavar="ID",
@@ -176,7 +207,7 @@ def main():
176207
if test_build or pre_build:
177208
hash = push_bumped_release(git_repos[0], target, test_build, pre_build)
178209
else:
179-
check_commit_is_available_remotely(git_repos[0], hash)
210+
check_commit_is_available_remotely(git_repos[0], hash, None if is_scratch else target, args.force)
180211
url = koji_url(remote, hash)
181212
command = (
182213
['koji', 'build']
@@ -194,7 +225,7 @@ def main():
194225
if test_build or pre_build:
195226
hash = push_bumped_release(d, target, test_build, pre_build)
196227
else:
197-
check_commit_is_available_remotely(d, hash)
228+
check_commit_is_available_remotely(d, hash, None if is_scratch else target, args.force)
198229
urls.append(koji_url(remote, hash))
199230
command = ['koji', 'chain-build', target] + (' : '.join(urls)).split(' ') + (['--nowait'] if is_nowait else [])
200231
print(' '.join(command), flush=True)

0 commit comments

Comments
 (0)