Implement infrastructure for ReleaseTypes from Environment

This commit is contained in:
bom 2023-03-17 11:52:22 +01:00
parent befb5eb653
commit 50faa89f6e
5 changed files with 89 additions and 14 deletions

View file

@ -1,5 +1,5 @@
from .domain import Release, Version, ReleaseType from .domain import Release, Version, ReleaseType
from .infrastructure_api import FileHandler from .infrastructure_api import FileHandler, GitApi, EnvironmentApi
class VersionRepository(): class VersionRepository():
@ -31,10 +31,21 @@ class VersionRepository():
return version return version
class ReleaseTypeRepository(): class ReleaseTypeRepository():
def __init__(self, git_api, environment_api=None): def __init__(self, git_api: GitApi, environment_api: EnvironmentApi):
self.git_api = git_api self.git_api = git_api
self.environment_api = environment_api
def get_release_type(self): @classmethod
def from_git(cls, git_api: GitApi):
environment_api = None
return cls(git_api, environment_api)
@classmethod
def from_environment(cls, environment_api: EnvironmentApi):
git_api = None
return cls(git_api, environment_api)
def __get_release_type_git(self) -> ReleaseType | None:
latest_commit = self.git_api.get_latest_commit() latest_commit = self.git_api.get_latest_commit()
if ReleaseType.MAJOR.name in latest_commit.upper(): if ReleaseType.MAJOR.name in latest_commit.upper():
@ -48,6 +59,30 @@ class ReleaseTypeRepository():
else: else:
return None return None
def __get_release_type_environment(self) -> ReleaseType | None:
release_name = self.environment_api.get('RELEASE_TYPE')
if release_name is None:
return None
elif ReleaseType.MAJOR.name in release_name.upper():
return ReleaseType.MAJOR
elif ReleaseType.MINOR.name in release_name.upper():
return ReleaseType.MINOR
elif ReleaseType.PATCH.name in release_name.upper():
return ReleaseType.PATCH
elif ReleaseType.SNAPSHOT.name in release_name.upper():
return ReleaseType.SNAPSHOT
else:
return None
def get_release_type(self) -> ReleaseType | None:
if self.git_api is not None:
return self.__get_release_type_git()
elif self.environment_api is not None:
return self.__get_release_type_environment()
else:
raise Exception('No valid api passed to ReleaseTypeRepository')
class ReleaseRepository(): class ReleaseRepository():
def __init__(self, version_repository: VersionRepository, release_type_repository: ReleaseTypeRepository, main_branch: str): def __init__(self, version_repository: VersionRepository, release_type_repository: ReleaseTypeRepository, main_branch: str):
self.version_repository = version_repository self.version_repository = version_repository

View file

@ -242,7 +242,7 @@ class EnvironmentApi():
self.environ = environ self.environ = environ
def get(self, key): def get(self, key):
return self.environ[key] return self.environ.get(key)
def set(self, key, value): def set(self, key, value):
self.environ[key] = value self.environ[key] = value

View file

@ -25,7 +25,7 @@ class ReleaseMixin(DevopsBuild):
self.config_file = release_mixin_config['config_file'] self.config_file = release_mixin_config['config_file']
self.main_branch = release_mixin_config['main_branch'] self.main_branch = release_mixin_config['main_branch']
self.git_api = GitApi() self.git_api = GitApi()
self.release_type_repo = ReleaseTypeRepository(self.git_api) # maybe get from env? self.release_type_repo = ReleaseTypeRepository.from_git(self.git_api)
self.version_repo = VersionRepository(self.config_file) self.version_repo = VersionRepository(self.config_file)
self.release_repo = ReleaseRepository(self.version_repo, self.release_type_repo, self.main_branch) self.release_repo = ReleaseRepository(self.version_repo, self.release_type_repo, self.main_branch)

View file

@ -56,3 +56,14 @@ class MockGitApi():
def checkout(self, branch: str): def checkout(self, branch: str):
return " " return " "
class MockEnvironmentApi():
def __init__(self, environ_map):
self.environ = environ_map
def get(self, name):
return self.environ.get(name)
def set(self, name, value):
self.environ[name] = value

View file

@ -1,6 +1,6 @@
from src.main.python.ddadevops.release_mixin.domain import ReleaseType from src.main.python.ddadevops.release_mixin.domain import ReleaseType
from src.main.python.ddadevops.release_mixin.infrastructure import ReleaseTypeRepository, VersionRepository, ReleaseRepository from src.main.python.ddadevops.release_mixin.infrastructure import ReleaseTypeRepository, VersionRepository, ReleaseRepository
from .mock_infrastructure_api import MockGitApi from .mock_infrastructure_api import MockGitApi, MockEnvironmentApi
from .helper import Helper from .helper import Helper
def test_version_repository(tmp_path): def test_version_repository(tmp_path):
@ -19,7 +19,7 @@ def test_release_repository(tmp_path):
th = Helper() th = Helper()
th.copy_files( th.TEST_FILE_PATH, tmp_path) th.copy_files( th.TEST_FILE_PATH, tmp_path)
version_repo = VersionRepository(th.TEST_FILE_PATH) version_repo = VersionRepository(th.TEST_FILE_PATH)
release_type_repo = ReleaseTypeRepository(MockGitApi('MINOR test')) release_type_repo = ReleaseTypeRepository.from_git(MockGitApi('MINOR test'))
# test # test
sut = ReleaseRepository(version_repo, release_type_repo, 'main') sut = ReleaseRepository(version_repo, release_type_repo, 'main')
@ -28,27 +28,56 @@ def test_release_repository(tmp_path):
assert release is not None assert release is not None
def test_release_type_repository(): def test_release_type_repository_git():
sut = ReleaseTypeRepository(MockGitApi('MINOR test')) sut = ReleaseTypeRepository.from_git(MockGitApi('MINOR test'))
release_type = sut.get_release_type() release_type = sut.get_release_type()
assert release_type is ReleaseType.MINOR assert release_type is ReleaseType.MINOR
sut = ReleaseTypeRepository(MockGitApi('MINOR bla')) sut = ReleaseTypeRepository.from_git(MockGitApi('MINOR bla'))
release_type = sut.get_release_type() release_type = sut.get_release_type()
assert release_type is ReleaseType.MINOR assert release_type is ReleaseType.MINOR
sut = ReleaseTypeRepository(MockGitApi('Major bla')) sut = ReleaseTypeRepository.from_git(MockGitApi('Major bla'))
release_type = sut.get_release_type() release_type = sut.get_release_type()
assert release_type == ReleaseType.MAJOR assert release_type == ReleaseType.MAJOR
sut = ReleaseTypeRepository(MockGitApi('PATCH bla')) sut = ReleaseTypeRepository.from_git(MockGitApi('PATCH bla'))
release_type = sut.get_release_type() release_type = sut.get_release_type()
assert release_type == ReleaseType.PATCH assert release_type == ReleaseType.PATCH
sut = ReleaseTypeRepository(MockGitApi('SNAPSHOT bla')) sut = ReleaseTypeRepository.from_git(MockGitApi('SNAPSHOT bla'))
release_type = sut.get_release_type() release_type = sut.get_release_type()
assert release_type == ReleaseType.SNAPSHOT assert release_type == ReleaseType.SNAPSHOT
sut = ReleaseTypeRepository(MockGitApi('bla')) sut = ReleaseTypeRepository.from_git(MockGitApi('bla'))
release_type = sut.get_release_type() release_type = sut.get_release_type()
assert release_type == None assert release_type == None
def test_release_type_repository_env():
sut = ReleaseTypeRepository.from_environment(MockEnvironmentApi({'RELEASE_TYPE': 'MINOR test'}))
release_type = sut.get_release_type()
assert release_type is ReleaseType.MINOR
sut = ReleaseTypeRepository.from_environment(MockEnvironmentApi({'RELEASE_TYPE': 'MINOR'}))
release_type = sut.get_release_type()
assert release_type is ReleaseType.MINOR
sut = ReleaseTypeRepository.from_environment(MockEnvironmentApi({'RELEASE_TYPE': 'Major bla'}))
release_type = sut.get_release_type()
assert release_type == ReleaseType.MAJOR
sut = ReleaseTypeRepository.from_environment(MockEnvironmentApi({'RELEASE_TYPE': 'Patch bla'}))
release_type = sut.get_release_type()
assert release_type == ReleaseType.PATCH
sut = ReleaseTypeRepository.from_environment(MockEnvironmentApi({'RELEASE_TYPE': 'Snapshot bla'}))
release_type = sut.get_release_type()
assert release_type == ReleaseType.SNAPSHOT
sut = ReleaseTypeRepository.from_environment(MockEnvironmentApi({'RELEASE_TYPE': 'Random text'}))
release_type = sut.get_release_type()
assert release_type == None
sut = ReleaseTypeRepository.from_environment(MockEnvironmentApi({'REL_TYPE': 'Not the right variable'}))
release_type = sut.get_release_type()
assert release_type == None