From 97ad297610b3b5d6c320a02d91eaea1dfb8654f6 Mon Sep 17 00:00:00 2001 From: Eric Ball Date: Wed, 9 Mar 2022 13:26:07 -0800 Subject: [PATCH] Fix: Avoid LegacyVersion casting due to hyphens Plugins are no longer following semantic versioning, and frequently include additional versioning information. pkg_resources.parse_version generally handles this well, but some versions with a hyphen will return a LegacyVersion object rather than a Version object, causing it to always compare as lower than any Version object. Simply replacing any '-' with '+' fixes this issue, and does not materially change the versioning. Signed-off-by: Eric Ball Change-Id: I5ad949d688ce7ebd0d183d69f4ce87b35343357f --- jenkins_jobs/registry.py | 23 +++++++++++++++++++++ tests/moduleregistry/test_moduleregistry.py | 4 ++++ 2 files changed, 27 insertions(+) diff --git a/jenkins_jobs/registry.py b/jenkins_jobs/registry.py index 9418dea52..ab37f663c 100644 --- a/jenkins_jobs/registry.py +++ b/jenkins_jobs/registry.py @@ -72,6 +72,29 @@ class ModuleRegistry(object): r"(.*)-(?:SNAPSHOT|BETA).*", r"\g<1>.preview", version ) + if isinstance( + pkg_resources.parse_version(plugin_info["version"]), + pkg_resources.extern.packaging.version.LegacyVersion, + ): + plugin_info["version"] = plugin_info["version"].replace("-", "+") + if isinstance( + pkg_resources.parse_version(plugin_info["version"]), + pkg_resources.extern.packaging.version.LegacyVersion, + ): + plugin_name = plugin_info.get( + "shortName", plugin_info.get("longName", None) + ) + if plugin_name: + logger.warning( + "Version %s for plugin %s is being treated as a LegacyVersion" + % (plugin_info["version"], plugin_name) + ) + else: + logger.warning( + "Version %s is being treated as a LegacyVersion" + % plugin_info["version"] + ) + aliases = [] for key in ["longName", "shortName"]: value = plugin_info.get(key, None) diff --git a/tests/moduleregistry/test_moduleregistry.py b/tests/moduleregistry/test_moduleregistry.py index 743f180f2..4f1d55020 100644 --- a/tests/moduleregistry/test_moduleregistry.py +++ b/tests/moduleregistry/test_moduleregistry.py @@ -37,6 +37,10 @@ class ModuleRegistryPluginInfoTestsWithScenarios( v1="1.4.6-SNAPSHOT (private-0986edd9-example)", op="__gt__", v2="1.4.5" ), ), + ("s16", dict(v1="1.0.1-1.v1", op="__gt__", v2="1.0.1")), + ("s17", dict(v1="1.0.1-1.v1", op="__lt__", v2="1.0.2")), + ("s18", dict(v1="1.0.2-1.v1", op="__gt__", v2="1.0.1")), + ("s19", dict(v1="1.0.2-1.v1", op="__gt__", v2="1.0.1-2")), ] def setUp(self):