Skip to content

Commit 1b48fe9

Browse files
Add branch prefix
Added comparison of CDN Definition prefix and Git branch name prefix. Skips in pre load and fails in load if they dont not match.
1 parent 0e20210 commit 1b48fe9

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

tests/ubiconfig/test_ubi.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,44 @@ def test_pre_load(mocked_get_branches, mocked_session, files_branch_map, caplog)
465465
)
466466

467467

468+
@patch("requests.Session")
469+
@patch("ubiconfig._impl.loaders._GitlabLoader._get_branches")
470+
def test_pre_load_with_branch_prefix(mocked_get_branches, mocked_session, caplog):
471+
branch_sha1 = OrderedDict(
472+
[
473+
("ubi7.1", "2189cbc2e447f796fe354f8d784d76b0a2620248"),
474+
("ubi7", "c99cb8d7dae2e78e8cc7e720d3f950d1c5a0b51f"),
475+
("test-prefix8.1", "26d24af7859df3c4d361bd33cd57984d03abe206"),
476+
("test-prefix8", "117d4af7859df3c4d361bd33cdab6784d03abe104"),
477+
]
478+
)
479+
mocked_get_branches.return_value = branch_sha1
480+
headers = {"Content-Length": "629", "X-Total-Pages": "1", "X-Per-Page": "20"}
481+
mocked_session.return_value.request.return_value.headers = headers
482+
483+
file_list_ubi7_1 = [
484+
{"name": "rhel-7-server.yaml", "path": "rhel-7-server.yaml"},
485+
]
486+
file_list_ubi7 = [
487+
{"name": "rhel-7-server.yaml", "path": "rhel-7-server.yaml"},
488+
]
489+
490+
mocked_session.return_value.request.return_value.json.side_effect = [
491+
file_list_ubi7_1,
492+
file_list_ubi7,
493+
]
494+
495+
loader = ubi.get_loader(branch_prefix="ubi")
496+
497+
actual_files_branch_map = loader._files_branch_map
498+
499+
assert "rhel-7-server.yaml" in actual_files_branch_map
500+
assert len(actual_files_branch_map["rhel-7-server.yaml"]) == 2
501+
assert actual_files_branch_map["rhel-7-server.yaml"][0][0] == "ubi7.1"
502+
assert actual_files_branch_map["rhel-7-server.yaml"][1][0] == "ubi7"
503+
assert "Skipping branch test-prefix" in caplog.text
504+
505+
468506
def test_ubi_config(ubi7_1_config_file1):
469507
config_dict = yaml.safe_load(ubi7_1_config_file1)
470508
config = UbiConfig.load_from_dict(config_dict, "rhel-atomic-host.yaml", "7.1")

ubiconfig/_impl/loaders/gitlab.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
class GitlabLoader(Loader):
2424
"""Load configuration from a remote repo on gitlab."""
2525

26-
def __init__(self, url):
26+
def __init__(self, url, branch_prefix=None):
2727
"""
2828
:param url: gitlab repo url in form of `https://<host>/<repo>`
2929
"""
3030
self._url = url
3131
self._session = None
32+
self._branch_prefix = branch_prefix
3233
self._repo_api = RepoApi(self._url.rstrip("/"))
3334
self._branches = self._get_branches()
3435
self._files_branch_map = self._pre_load()
@@ -85,6 +86,10 @@ def load(self, file_name, version=None):
8586
match = re.match(PREFIX_VERSION_RE, version) # type: ignore
8687
prefix = match.group("prefix") # type: ignore
8788
default_version = match.group("default_version") # type: ignore
89+
if self._branch_prefix and self._branch_prefix != prefix:
90+
raise ValueError(
91+
f"Branch prefix {prefix} is not matching with cdn-definition prefix {self._branch_prefix}."
92+
)
8893

8994
default_branch = f"{prefix}{default_version}"
9095

@@ -141,12 +146,20 @@ def _pre_load(self):
141146
LOG.debug("Loading config files from all branches")
142147

143148
for branch, sha1 in self._branches.items():
144-
if not re.match(PREFIX_VERSION_RE, branch):
149+
branch_regex = re.match(PREFIX_VERSION_RE, branch)
150+
if not branch_regex:
145151
LOG.warning(
146152
"Skipping branch %s (name does not match with required format)",
147153
branch,
148154
)
149155
continue
156+
prefix = branch_regex.group("prefix")
157+
if self._branch_prefix and self._branch_prefix != prefix:
158+
LOG.warning(
159+
f"Skipping branch {prefix} \
160+
(branch does not match with cdn definition branch {self._branch_prefix})"
161+
)
162+
continue
150163
page = 1
151164
while True:
152165
file_list_api = self._repo_api.get_file_list_api(branch=sha1, page=page)

ubiconfig/ubi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class LoaderError(RuntimeError):
1313
pass
1414

1515

16-
def get_loader(source=None):
16+
def get_loader(source=None, branch_prefix=None):
1717
"""Get a Loader instance which is used to load configurations.
1818
1919
``source`` should be provided as one of the following:
@@ -60,7 +60,7 @@ def get_loader(source=None):
6060
parsed = urlparse(source)
6161
if parsed.netloc:
6262
# It's a URL, use the gitlab loader
63-
return _GitlabLoader(source)
63+
return _GitlabLoader(source, branch_prefix)
6464

6565
# It should be a local path
6666
if not os.path.isdir(source):

0 commit comments

Comments
 (0)