diff --git a/README.md b/README.md index 80a200d..b61b3c9 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,9 @@ and override values in custom use cases. | name | description | |:----------------:|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:| | `VERSION` | value extracted from branch name or `VALHALLA_RELEASE_CMD`, for `release-1.2.14` it will be `1.2.14` | +| `VERSION_MAJOR` | value extracted from branch name or `VALHALLA_RELEASE_CMD`, for `release-1.2.14` it will be `1` | +| `VERSION_MINOR` | value extracted from branch name or `VALHALLA_RELEASE_CMD`, for `release-1.2.14` it will be `2` | +| `VERSION_PATCH` | value extracted from branch name or `VALHALLA_RELEASE_CMD`, for `release-1.2.14` it will be `14` | | `VERSION_SLUG` | value extracted from branch name or `VALHALLA_RELEASE_CMD` and with everything except 0-9 and a-z replaced with -. No leading / trailing -,
for `release-1.2.14` it will be `1-2-14`. Use in URLs, host names, domain names and file names | | `VALHALLA_TOKEN` | token passed to CI runner which execute this job | diff --git a/test/common/resolver_test.py b/test/common/resolver_test.py index d5fa394..5c5bc03 100644 --- a/test/common/resolver_test.py +++ b/test/common/resolver_test.py @@ -18,6 +18,18 @@ def test_resolve_predefined(self): # then: self.assertEqual("Testing 1.0", resolved_string) + def test_resolve_predefined_version(self): + # given: + init_str_resolver("1.2.14-RC01", "token123") + init_str_resolver_custom_variables({"CUSTOM_VAR": "value123"}) + + # when: + resolved_string = resolve( + "Testing {VERSION}; major is {VERSION_MAJOR} and minor is {VERSION_MINOR} and patch {VERSION_PATCH}") + + # then: + self.assertEqual("Testing 1.2.14-RC01; major is 1 and minor is 2 and patch 14", resolved_string) + def test_resolve_predefined_version_slug(self): # given: init_str_resolver("1.0.0", "token123") diff --git a/valhalla/common/resolver.py b/valhalla/common/resolver.py index 6ea9ed1..2c3ff40 100644 --- a/valhalla/common/resolver.py +++ b/valhalla/common/resolver.py @@ -4,6 +4,9 @@ from valhalla.common.logger import info, error VERSION = "not_set" +VERSION_MAJOR = "not_set" +VERSION_MINOR = "not_set" +VERSION_PATCH = "not_set" VERSION_SLUG = "not_set" VALHALLA_TOKEN = "not_set" CUSTOM_VARIABLES_DICT = dict() @@ -11,9 +14,15 @@ def init_str_resolver(version: str, token: str): global VERSION + global VERSION_MAJOR + global VERSION_MINOR + global VERSION_PATCH global VERSION_SLUG global VALHALLA_TOKEN VERSION = version + VERSION_MAJOR = __get_major(version) + VERSION_MINOR = __get_minor(version) + VERSION_PATCH = __get_patch(version) VERSION_SLUG = __get_slug(version) VALHALLA_TOKEN = token @@ -43,10 +52,16 @@ def resolve(string: str): def __resolve_predefined(string: str): global VERSION + global VERSION_MAJOR + global VERSION_MINOR + global VERSION_PATCH global VERSION_SLUG global VALHALLA_TOKEN string = string.replace("{VERSION}", VERSION) + string = string.replace("{VERSION_MAJOR}", VERSION_MAJOR) + string = string.replace("{VERSION_MINOR}", VERSION_MINOR) + string = string.replace("{VERSION_PATCH}", VERSION_PATCH) string = string.replace("{VERSION_SLUG}", VERSION_SLUG) string = string.replace("{VALHALLA_TOKEN}", VALHALLA_TOKEN) @@ -73,3 +88,24 @@ def __get_slug(version): slug = re.sub(r'[^0-9a-zA-Z]+', '-', version).lower() slug = slug.strip('-') return slug + + +def __get_major(version): + match = re.match(r'^(\d+)', version) + if match: + return match.group(1) + raise ValueError("Invalid version format") + + +def __get_minor(version): + match = re.match(r'^\d+\.(\d+)', version) + if match: + return match.group(1) + raise ValueError("Invalid version format") + + +def __get_patch(version): + match = re.match(r'^\d+\.\d+\.(\d+)', version) + if match: + return match.group(1) + raise ValueError("Invalid version format")