introduce version parsed from string

This commit is contained in:
Michael Jerger 2023-05-09 09:19:26 +02:00
parent 1e9f8ff077
commit f569d07a84
8 changed files with 171 additions and 109 deletions

View file

@ -19,7 +19,6 @@ from .devops_terraform_build import DevopsTerraformBuild, create_devops_terrafor
from .devops_build import DevopsBuild, create_devops_build_config, get_devops_build
from .credential import gopass_password_from_path, gopass_field_from_path
from .release_mixin import ReleaseMixin
from .domain import Validateable, DnsRecord, Devops, Image, Release
from .domain import Validateable, DnsRecord, Devops, Image, Release, Version
__version__ = "${version}"

View file

@ -1,5 +1,6 @@
from .common import Validateable, DnsRecord, Devops, BuildType, MixinType
from .common import Validateable, DnsRecord, Devops, BuildType, MixinType, ReleaseType
from .devops_factory import DevopsFactory
from .image import Image
from .c4k import C4k
from .release import Release, ReleaseType, Version, EnvironmentKeys
from .release import Release, EnvironmentKeys
from .version import Version

View file

@ -18,6 +18,13 @@ class MixinType(Enum):
RELEASE = 0
class ReleaseType(Enum):
MAJOR = 3
MINOR = 2
PATCH = 1
NONE = None
class Validateable:
def __validate_is_not_none__(self, field_name: str) -> List[str]:
value = self.__dict__[field_name]
@ -26,10 +33,13 @@ class Validateable:
return []
def __validate_is_not_empty__(self, field_name: str) -> List[str]:
result = self.__validate_is_not_none__(field_name)
value = self.__dict__[field_name]
if value is None or value == "":
return [f"Field '{field_name}' must not be empty."]
return []
if type(value) is str and value == "":
result += [f"Field '{field_name}' must not be empty."]
elif type(value) is list and len(value) == 0:
result += [f"Field '{field_name}' must not be empty."]
return result
def validate(self) -> List[str]:
return []

View file

@ -4,83 +4,25 @@ from pathlib import Path
from .common import (
Validateable,
Devops,
ReleaseType,
)
from .version import (
Version,
)
class ReleaseType(Enum):
MAJOR = 3
MINOR = 2
PATCH = 1
NONE = None
class EnvironmentKeys(Enum):
DDADEVOPS_RELEASE_TYPE = 0
class Version(Validateable):
def __init__(self, path: Path, version_list: list):
self.path = path
self.version_list = version_list
self.version_string: Optional[str | None] = None
self.is_snapshot: Optional[bool | None] = None
def increment(self, release_type: ReleaseType | None):
self.is_snapshot = False
match release_type:
case ReleaseType.BUMP:
self.is_snapshot = True
self.version_list[ReleaseType.PATCH.value] += 1
case ReleaseType.SNAPSHOT:
self.is_snapshot = True
case ReleaseType.PATCH:
self.version_list[ReleaseType.PATCH.value] += 1
case ReleaseType.MINOR:
self.version_list[ReleaseType.PATCH.value] = 0
self.version_list[ReleaseType.MINOR.value] += 1
case ReleaseType.MAJOR:
self.version_list[ReleaseType.PATCH.value] = 0
self.version_list[ReleaseType.MINOR.value] = 0
self.version_list[ReleaseType.MAJOR.value] += 1
case None:
raise RuntimeError("Release Type was not set!")
def get_version_string(self) -> str:
self.version_string = ".".join([str(x) for x in self.version_list])
if self.is_snapshot:
self.version_string += "-SNAPSHOT"
return self.version_string
def create_release_version(self, release_type: ReleaseType | None):
release_version = Version(self.path, self.version_list.copy())
release_version.is_snapshot = self.is_snapshot
release_version.increment(release_type)
return release_version
def create_bump_version(self):
bump_version = Version(self.path, self.version_list.copy())
bump_version.is_snapshot = self.is_snapshot
bump_version.increment(ReleaseType.BUMP)
return bump_version
class Release(Validateable):
def __init__(self, input: dict):
self.release_type = ReleaseType[input.get("release_type", "NONE")]
self.release_main_branch = input.get("release_main_branch", "main")
self.release_current_branch = input.get("release_current_branch")
self.release_current_version = input.get("release_current_version")
self.release_config_file = input.get("release_config_file", "project.clj")
self.version = self.__version_from_str__()
self.release_current_version = Version.from_str(input.get("release_current_version"))
# TODO: mv version parsing to version
def __version_from_str__(self):
if not self.release_current_version:
return
version_parsed = []
for x in self.release_current_version.split("."):
version_parsed += [int(x)]
return Version("unused", version_parsed)
def release_version(self) -> Version:
return self.version.create_release_version(self.release_type)
@ -91,16 +33,16 @@ class Release(Validateable):
def validate(self):
result = []
result += self.__validate_is_not_empty__("release_type")
result += self.__validate_is_not_empty__("release_current_version")
result += self.__validate_is_not_empty__("release_current_branch")
result += self.__validate_is_not_empty__("release_main_branch")
result += self.__validate_is_not_empty__("release_config_file")
if self.version:
result += self.version.validate()
result += self.__validate_is_not_empty__("release_current_version")
if self.release_current_version:
result += self.release_current_version.validate()
if (
self.release_type is not None
and self.release_type != ReleaseType.NONE
and main_branch != self.release_current_branch
and self.release_main_branch != self.release_current_branch
):
result.append(f"Releases are allowed only on {main_branch}")
return result

View file

@ -0,0 +1,64 @@
from enum import Enum
from typing import Optional
from .common import (
Validateable,
)
class Version(Validateable):
@classmethod
def from_str(cls, input_str: str):
snapshot_parsed = input_str.split("-")
version_str = snapshot_parsed[0]
suffix_str = None
if len(snapshot_parsed) > 1:
suffix_str = snapshot_parsed[1]
version_no_parsed = []
for x in version_str.split("."):
version_no_parsed += [int(x)]
return cls(
version_no_parsed,
suffix_str,
input_str,
)
def __init__(
self,
version_list: list,
snapshot_suffix: Optional[str] = None,
version_str: Optional[str] = None,
):
self.version_list = version_list
self.snapshot_suffix = snapshot_suffix
self.version_string = version_str
def is_snapshot(self):
return not self.snapshot_suffix == None
def to_string(self) -> str:
version_no = ".".join([str(x) for x in self.version_list])
if self.is_snapshot():
return f"{version_no}-{self.snapshot_suffix}"
return version_no
def validate(self):
result = []
result += self.__validate_is_not_empty__("version_list")
if self.version_list and len(self.version_list) < 3:
result += [f"version_list must have at least 3 levels."]
if self.version_list and self.version_string and self.to_string() != self.version_string:
result += [f"version_string not parsed correct. Input was {self.version_string} parsed was {self.to_string()}"]
return result
# def create_release_version(self, release_type: ReleaseType | None):
# release_version = Version(self.path, self.version_list.copy())
# release_version.is_snapshot = self.is_snapshot
# release_version.increment(release_type)
# return release_version
# def create_bump_version(self):
# bump_version = Version(self.path, self.version_list.copy())
# bump_version.is_snapshot = self.is_snapshot
# bump_version.increment(ReleaseType.BUMP)
# return bump_version

View file

@ -50,7 +50,7 @@ def test_should_validate_non_empty_others():
def test_validate_with_reason():
sut = MockValidateable(None)
assert sut.validate()[0] == "Field 'field' must not be empty."
assert sut.validate()[0] == "Field 'field' must not be None."
def test_should_validate_DnsRecord():

View file

@ -15,39 +15,6 @@ from src.main.python.ddadevops.domain import (
from src.main.python.ddadevops.domain.image import Image
from .test_helper import build_devops, devops_config
def test_version(tmp_path: Path):
version = Version(tmp_path, [1, 2, 3])
version.increment(ReleaseType.SNAPSHOT)
assert version.get_version_string() == "1.2.3-SNAPSHOT"
assert version.version_list == [1, 2, 3]
assert version.is_snapshot
version = Version(tmp_path, [1, 2, 3])
version.increment(ReleaseType.BUMP)
assert version.get_version_string() == "1.2.4-SNAPSHOT"
assert version.version_list == [1, 2, 4]
assert version.is_snapshot
version = Version(tmp_path, [1, 2, 3])
version.increment(ReleaseType.PATCH)
assert version.get_version_string() == "1.2.4"
assert version.version_list == [1, 2, 4]
assert not version.is_snapshot
version = Version(tmp_path, [1, 2, 3])
version.increment(ReleaseType.MINOR)
assert version.get_version_string() == "1.3.0"
assert version.version_list == [1, 3, 0]
assert not version.is_snapshot
version = Version(tmp_path, [1, 2, 3])
version.increment(ReleaseType.MAJOR)
assert version.get_version_string() == "2.0.0"
assert version.version_list == [2, 0, 0]
assert not version.is_snapshot
def test_release_context(tmp_path):
sut = Release(
devops_config(

View file

@ -0,0 +1,79 @@
from pybuilder.core import Project
from pathlib import Path
from src.main.python.ddadevops.domain import (
Version,
ReleaseType,
)
from src.main.python.ddadevops.domain.image import Image
from .test_helper import build_devops, devops_config
def test_version_creation():
sut = Version.from_str("1.2.3")
assert sut.to_string() == "1.2.3"
assert sut.version_list == [1, 2, 3]
assert sut.is_snapshot() == False
sut = Version.from_str("1.2.3-SNAPSHOT")
assert sut.to_string() == "1.2.3-SNAPSHOT"
assert sut.version_list == [1, 2, 3]
assert sut.is_snapshot() == True
def test_should_validate_version_list():
sut = Version(None)
assert not sut.is_valid()
sut = Version([])
assert not sut.is_valid()
sut = Version([1, 2])
assert not sut.is_valid()
sut = Version([1, 2, 3])
assert sut.is_valid()
def test_should_validate_parsing():
sut = Version.from_str("1.2")
assert not sut.is_valid()
sut = Version.from_str("1.2.3")
sut.version_list = [2, 2, 2]
assert not sut.is_valid()
sut = Version.from_str("1.2.3")
assert sut.is_valid()
sut = Version.from_str("1.2.3-SNAPSHOT")
assert sut.is_valid()
sut = Version.from_str("1.2.3-dev")
assert sut.is_valid()
def test_version():
pass
# version = Version.version_from_str("1.2.3-SNAPSHOT")
# version.increment(ReleaseType.NONE)
# assert version.get_version_string() == "1.2.3-SNAPSHOT"
# assert version.version_list == [1, 2, 3]
# assert version.is_snapshot
# version = Version.version_from_str("1.2.3")
# version.increment(ReleaseType.PATCH)
# assert version.get_version_string() == "1.2.4"
# assert version.version_list == [1, 2, 4]
# assert not version.is_snapshot
# version = Version.version_from_str("1.2.3")
# version.increment(ReleaseType.MINOR)
# assert version.get_version_string() == "1.3.0"
# assert version.version_list == [1, 3, 0]
# assert not version.is_snapshot
# version = Version.version_from_str("1.2.3")
# version.increment(ReleaseType.MAJOR)
# assert version.get_version_string() == "2.0.0"
# assert version.version_list == [2, 0, 0]
# assert not version.is_snapshot