Fix CI: Refactor None like logic

We now do check wether from_git or from_env was
called and then take a bool based decision on which
method to call. Also: Type declarations and specific errors.
This commit is contained in:
erik 2023-04-19 15:29:59 +02:00
parent 420030fa0a
commit 8e8ad034a7

View file

@ -1,3 +1,4 @@
from typing import Optional
from src.main.python.ddadevops.domain import Release, Version, ReleaseType, EnvironmentKeys
from src.main.python.ddadevops.infrastructure.release_mixin import FileHandler, GitApi, EnvironmentApi
@ -32,19 +33,23 @@ class VersionRepository():
return version
class ReleaseTypeRepository():
def __init__(self, git_api: GitApi, environment_api: EnvironmentApi):
self.git_api = git_api
self.environment_api = environment_api
def __init__(self, git_api: GitApi = GitApi(), environment_api: EnvironmentApi = EnvironmentApi()):
self.git_api: GitApi = git_api
self.environment_api: EnvironmentApi = environment_api
self.get_from_git: bool = False
self.get_from_env: bool = False
@classmethod
def from_git(cls, git_api: GitApi):
environment_api = None
return cls(git_api, environment_api)
releaseTypeRepo = cls(git_api= git_api)
releaseTypeRepo.get_from_git = True
return releaseTypeRepo
@classmethod
def from_environment(cls, environment_api: EnvironmentApi):
git_api = None
return cls(git_api, environment_api)
releaseTypeRepo = cls(environment_api=environment_api)
releaseTypeRepo.get_from_env = True
return releaseTypeRepo
def __get_release_type_git(self) -> ReleaseType | None:
latest_commit = self.git_api.get_latest_commit()
@ -64,7 +69,7 @@ class ReleaseTypeRepository():
release_name = self.environment_api.get(EnvironmentKeys.DDADEVOPS_RELEASE_TYPE.name)
if release_name is None:
return None
raise ValueError("Release Name not found. Is the Environment correctly configured?")
elif ReleaseType.MAJOR.name in release_name.upper():
return ReleaseType.MAJOR
elif ReleaseType.MINOR.name in release_name.upper():
@ -77,12 +82,12 @@ class ReleaseTypeRepository():
return None
def get_release_type(self) -> ReleaseType | None:
if self.git_api is not None:
if self.get_from_git:
return self.__get_release_type_git()
elif self.environment_api is not None:
elif self.get_from_env:
return self.__get_release_type_environment()
else:
raise Exception('No valid api passed to ReleaseTypeRepository')
raise ValueError('No valid api passed to ReleaseTypeRepository')
class ReleaseRepository():
def __init__(self, version_repository: VersionRepository, release_type_repository: ReleaseTypeRepository, main_branch: str):