2023-02-24 09:14:26 +00:00
|
|
|
from enum import Enum
|
2023-02-28 12:25:47 +00:00
|
|
|
from pathlib import Path
|
2023-02-24 10:39:38 +00:00
|
|
|
|
|
|
|
class Config():
|
|
|
|
def __init__(self, main_branch):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-02-24 09:14:26 +00:00
|
|
|
class ReleaseType(Enum):
|
|
|
|
MAJOR = 0
|
|
|
|
MINOR = 1
|
|
|
|
PATCH = 2
|
|
|
|
SNAPSHOT = 3
|
|
|
|
BUMP = None
|
|
|
|
|
2023-02-08 09:57:40 +00:00
|
|
|
|
2023-02-08 10:37:41 +00:00
|
|
|
class Version():
|
2023-02-08 10:22:55 +00:00
|
|
|
|
2023-02-24 10:39:38 +00:00
|
|
|
def __init__(self, id: Path, version_list: list):
|
|
|
|
self.id = id
|
2023-02-20 16:04:50 +00:00
|
|
|
self.version_list = version_list
|
2023-02-20 13:24:55 +00:00
|
|
|
self.version_string = None
|
2023-02-21 15:14:47 +00:00
|
|
|
self.is_snapshot = None
|
2023-02-09 12:20:42 +00:00
|
|
|
|
2023-02-23 13:38:54 +00:00
|
|
|
def increment(self, release_type: ReleaseType):
|
2023-02-08 11:37:00 +00:00
|
|
|
self.is_snapshot = False
|
2023-02-23 13:38:54 +00:00
|
|
|
match release_type:
|
2023-02-20 15:21:02 +00:00
|
|
|
case ReleaseType.BUMP:
|
|
|
|
self.is_snapshot = True
|
|
|
|
self.version_list[ReleaseType.PATCH.value] += 1
|
2023-02-20 13:24:55 +00:00
|
|
|
case ReleaseType.SNAPSHOT:
|
2023-02-08 11:37:00 +00:00
|
|
|
self.is_snapshot = True
|
2023-02-20 13:24:55 +00:00
|
|
|
case ReleaseType.PATCH:
|
|
|
|
self.version_list[ReleaseType.PATCH.value] += 1
|
|
|
|
case ReleaseType.MINOR:
|
2023-02-24 09:14:26 +00:00
|
|
|
self.version_list[ReleaseType.PATCH.value] = 0
|
2023-02-20 13:24:55 +00:00
|
|
|
self.version_list[ReleaseType.MINOR.value] += 1
|
|
|
|
case ReleaseType.MAJOR:
|
2023-02-24 09:14:26 +00:00
|
|
|
self.version_list[ReleaseType.PATCH.value] = 0
|
|
|
|
self.version_list[ReleaseType.MINOR.value] = 0
|
2023-02-20 13:24:55 +00:00
|
|
|
self.version_list[ReleaseType.MAJOR.value] += 1
|
2023-02-21 15:14:47 +00:00
|
|
|
case None:
|
2023-02-21 14:52:09 +00:00
|
|
|
raise Exception("Release Type was not set!")
|
2023-02-08 11:37:00 +00:00
|
|
|
|
2023-02-20 12:47:14 +00:00
|
|
|
def get_version_string(self) -> str:
|
2023-02-20 13:24:55 +00:00
|
|
|
self.version_string = ".".join([str(x) for x in self.version_list])
|
2023-02-08 11:37:00 +00:00
|
|
|
if self.is_snapshot:
|
2023-02-20 15:21:02 +00:00
|
|
|
self.version_string += "-SNAPSHOT"
|
2023-02-20 16:04:50 +00:00
|
|
|
return self.version_string
|
2023-02-24 09:14:26 +00:00
|
|
|
|
2023-02-23 13:38:54 +00:00
|
|
|
def create_release_version(self, release_type: ReleaseType):
|
2023-02-28 12:25:47 +00:00
|
|
|
release_version = Version(self.id, self.version_list.copy())
|
2023-02-22 09:57:42 +00:00
|
|
|
release_version.is_snapshot = self.is_snapshot
|
2023-02-23 13:38:54 +00:00
|
|
|
release_version.increment(release_type)
|
2023-02-24 09:14:26 +00:00
|
|
|
return release_version
|
|
|
|
|
2023-02-21 14:52:09 +00:00
|
|
|
def create_bump_version(self):
|
2023-02-28 12:25:47 +00:00
|
|
|
bump_version = Version(self.id, self.version_list.copy())
|
2023-02-22 09:57:42 +00:00
|
|
|
bump_version.is_snapshot = self.is_snapshot
|
2023-02-23 13:38:54 +00:00
|
|
|
bump_version.increment(ReleaseType.BUMP)
|
2023-02-22 09:57:42 +00:00
|
|
|
return bump_version
|
2023-02-24 09:14:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Release():
|
2023-02-24 10:39:38 +00:00
|
|
|
def __init__(self, release_type: ReleaseType, version: Version, current_branch: str):
|
2023-02-24 09:14:26 +00:00
|
|
|
self.release_type = release_type
|
|
|
|
self.version = version
|
2023-02-24 10:39:38 +00:00
|
|
|
self.current_branch = current_branch
|
|
|
|
|
|
|
|
def release_version(self):
|
|
|
|
return self.version.create_release_version(self.release_type)
|
|
|
|
|
|
|
|
def bump_version(self):
|
|
|
|
return self.version.create_bump_version()
|
|
|
|
|
|
|
|
def validate(self, main_branch):
|
|
|
|
result = []
|
|
|
|
if self.release_type is not None and main_branch != self.current_branch:
|
|
|
|
result.append(f"Releases are allowed only on {main_branch}")
|
|
|
|
return result
|
|
|
|
|
|
|
|
def is_valid(self, main_branch):
|
|
|
|
return self.validate(main_branch).count < 1
|