Skip to content

Commit

Permalink
Added 3 new predefined variables: VERSION_MAJOR, VERSION_MINOR, VERSI… (
Browse files Browse the repository at this point in the history
#15)

* Added 3 new predefined variables: VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH
  • Loading branch information
marwin1991 authored Jun 1, 2024
1 parent 5eda1e6 commit 7606766
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -, <br/>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 |

Expand Down
12 changes: 12 additions & 0 deletions test/common/resolver_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
41 changes: 40 additions & 1 deletion valhalla/common/resolver.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import os
import re

from valhalla.common.logger import info, error
from valhalla.common.logger import info, error, warn

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()


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

Expand Down Expand Up @@ -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)

Expand All @@ -73,3 +88,27 @@ 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)
warn(f"Could not get VERSION_MINOR variable value from version: {version}")
return ""


def __get_minor(version):
match = re.match(r'^\d+\.(\d+)', version)
if match:
return match.group(1)
warn(f"Could not get VERSION_MINOR variable value from version: {version}")
return ""


def __get_patch(version):
match = re.match(r'^\d+\.\d+\.(\d+)', version)
if match:
return match.group(1)
warn(f"Could not get VERSION_PATCH variable value from version: {version}")
return ""

0 comments on commit 7606766

Please sign in to comment.