Skip to content

Commit 0ff8bf7

Browse files
Add branch prefix[RHELDST-36928] (#139)
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 a6f5f29 commit 0ff8bf7

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
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: 13 additions & 3 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,7 +86,6 @@ 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
88-
8989
default_branch = f"{prefix}{default_version}"
9090

9191
sha1 = None
@@ -141,12 +141,22 @@ def _pre_load(self):
141141
LOG.debug("Loading config files from all branches")
142142

143143
for branch, sha1 in self._branches.items():
144-
if not re.match(PREFIX_VERSION_RE, branch):
144+
branch_regex = re.match(PREFIX_VERSION_RE, branch)
145+
if not branch_regex:
145146
LOG.warning(
146147
"Skipping branch %s (name does not match with required format)",
147148
branch,
148149
)
149150
continue
151+
prefix = branch_regex.group("prefix")
152+
if self._branch_prefix and self._branch_prefix != prefix:
153+
LOG.warning(
154+
"Skipping branch %s \
155+
(branch does not match with cdn definition branch %s)",
156+
prefix,
157+
self._branch_prefix,
158+
)
159+
continue
150160
page = 1
151161
while True:
152162
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)