Verified Commit 0a72736d authored by David Runge's avatar David Runge
Browse files

gitlab: More generically retrieve types of releases

arch_release_promotion/gitlab.py:
Add `Upstream.get_releases()` to allow retrieval of promoted or
unpromoted releases of an arbitrary upper limit.
Change `Upstream.select_release()` to use `Upstream.get_releases()`.

tests/test_gitlab.py:
Add parametrized test for `Upstream.get_releases()`.
parent e05004bd
......@@ -43,17 +43,7 @@ class Upstream(gitlab.Gitlab):
The tag_name of the selected release
"""
project = self.projects.get(self.name)
release_tags: List[str] = []
for release in project.releases.list():
# only select releases that are not yet promoted
if (
not any(link.name == "Promotion artifact" for link in release.links.list())
and len(release_tags) < max_releases
):
release_tags += [release.tag_name]
release_tags = self.get_releases(max_releases=max_releases, promoted=False)
if release_tags:
selection_string = "".join([f"{index}) {value}\n" for index, value in enumerate(release_tags)])
......@@ -63,6 +53,44 @@ class Upstream(gitlab.Gitlab):
print("There are no releases to promote!")
return None
def get_releases(self, max_releases: int, promoted: bool = False) -> List[str]:
"""Get a list of releases of a project
Parameters
----------
max_releases: int
The maximum amount of releases to list
promoted: bool
Whether to only consider promoted releases (defaults to False)
Returns
-------
List[str]
A list of strings representing release tags of the project
"""
project = self.projects.get(self.name)
release_tags: List[str] = []
for release in project.releases.list():
if promoted:
# only select releases that are promoted
if (
any(link.name == "Promotion artifact" for link in release.links.list())
and len(release_tags) < max_releases
):
release_tags += [release.tag_name]
else:
# only select releases that are not yet promoted
if (
not any(link.name == "Promotion artifact" for link in release.links.list())
and len(release_tags) < max_releases
):
release_tags += [release.tag_name]
return release_tags
def download_release(self, tag_name: str, temp_dir: Path, job_name: str) -> Path:
"""Download the build artifacts of a project's release as a compressed file
......
......@@ -67,6 +67,51 @@ def test_gitlab_select_release(
assert upstream.select_release() == output
@mark.parametrize(
"releases_available, tag_name, link_name, max_releases, promoted, output",
[
(True, "0.1.0", "Build artifacts", 1, False, ["0.1.0"]),
(False, "0.1.0", "Build artifacts", 1, False, []),
(True, "0.1.0", "Promotion artifact", 1, False, []),
(False, "0.1.0", "Promotion artifact", 1, False, []),
(True, "0.1.0", "Promotion artifact", 1, True, ["0.1.0"]),
(True, "0.1.0", "Build artifacts", 1, True, []),
],
)
def test_gitlab_get_releases(
releases_available: bool,
tag_name: str,
link_name: str,
max_releases: bool,
promoted: bool,
output: Optional[str],
) -> None:
upstream = gitlab.Upstream(
url="https://foo.bar-mc.foo",
private_token="THISISAFAKETOKEN",
name="foo/bar",
)
link = Mock()
link.name = link_name
links = Mock()
links.list.return_value = [link]
release = Mock()
release.links = links
release.tag_name = tag_name
releases = Mock()
releases.list.return_value = [release] if releases_available else []
project = Mock()
project.releases = releases
projects = Mock()
projects.get.return_value = project
upstream.projects = projects
assert upstream.get_releases(max_releases=max_releases, promoted=promoted) == output
def test_gitlab_download_release() -> None:
upstream = gitlab.Upstream(
url="https://foo.bar-mc.foo",
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment