From adc766d6b4bc9d9797e81a55c5b05789c6fea35d Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Tue, 26 Sep 2023 21:35:27 +0200 Subject: [PATCH] Allow to specify constraints. --- changelogs/fragments/99-constraints.yml | 2 ++ src/antsibull_core/galaxy.py | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/99-constraints.yml diff --git a/changelogs/fragments/99-constraints.yml b/changelogs/fragments/99-constraints.yml new file mode 100644 index 0000000..13cbaf8 --- /dev/null +++ b/changelogs/fragments/99-constraints.yml @@ -0,0 +1,2 @@ +minor_changes: + - "Allow to specify constraints when determining the latest version of a collection from Galaxy (https://github.com/ansible-community/antsibull-core/pull/99)." diff --git a/src/antsibull_core/galaxy.py b/src/antsibull_core/galaxy.py index 3c4f43c..bf5262a 100644 --- a/src/antsibull_core/galaxy.py +++ b/src/antsibull_core/galaxy.py @@ -325,7 +325,11 @@ async def get_release_info( return collection_info async def get_latest_matching_version( - self, collection: str, version_spec: str, pre: bool = False + self, + collection: str, + version_spec: str, + pre: bool = False, + constraint: semver.SimpleSpec | None = None, ) -> semver.Version: """ Get the latest version of a collection that matches a specification. @@ -338,6 +342,7 @@ async def get_latest_matching_version( and pre=True, if the available versions are 2.0.0-a1 and 2.0.0-a2, then 2.0.0-a2 will be returned. If the available versions are 2.0.0 and 2.1.0-b2, 2.0.0 will be returned since non-pre-releases are preferred. The default is False + :kwarg constraint: If provided, only consider versions that match this specification. :returns: :obj:`semantic_version.Version` of the latest collection version that satisfied the specification. @@ -356,6 +361,8 @@ async def get_latest_matching_version( spec = semver.SimpleSpec(version_spec) prereleases = [] for version in (v for v in sem_versions if v in spec): + if constraint is not None and version not in constraint: + continue # If this is a pre-release, first check if there's a non-pre-release that # will satisfy the version_spec. if version.prerelease: