Compare commits

..

No commits in common. "4a23b0b800c61ec764f1fa9492e5b5b9879a0bef" and "2fe05b0e06342cea2af501e1c0755bde0d2f2506" have entirely different histories.

7 changed files with 94 additions and 93 deletions

View file

@ -16,14 +16,9 @@ class GitRepository():
inst.latest_commit = commit_string inst.latest_commit = commit_string
return inst return inst
def get_latest_n_commits(self, n: int):
self.system_repository.run_checked('git', 'log', '--oneline', '--format="%s %b"', f'-n {n}')
return self.system_repository.stdout
def get_latest_commit(self): def get_latest_commit(self):
output = self.get_latest_n_commits(1) self.system_repository.run_checked('git', 'log', '--oneline', '--format="%s %b"', '-n 1')
self.latest_commit = " ".join(output) # returns a list of strings otherwise self.latest_commit = " ".join(self.system_repository.stdout) # returns a list of strings otherwise
return self.latest_commit
def get_release_type_from_latest_commit(self): def get_release_type_from_latest_commit(self):
if self.latest_commit is None: if self.latest_commit is None:
@ -43,9 +38,6 @@ class GitRepository():
def tag_annotated(self, annotation: str, message: str): def tag_annotated(self, annotation: str, message: str):
self.system_repository.run_checked('git', 'tag', '-a', annotation, '-m', message) self.system_repository.run_checked('git', 'tag', '-a', annotation, '-m', message)
def tag_annotated(self, annotation: str, message: str, count: int):
self.system_repository.run_checked('git', 'tag', '-a', annotation, '-m', message, f'HEAD~{count}')
def get_current_branch(self): def get_current_branch(self):
self.system_repository.run_checked('git', 'branch', '--show-current') self.system_repository.run_checked('git', 'branch', '--show-current')

View file

@ -13,7 +13,7 @@ def create_release_mixin_config(config_file, main_branch) -> dict:
config.update( config.update(
{'ReleaseMixin': {'ReleaseMixin':
{'main_branch': main_branch, {'main_branch': main_branch,
'config_file': config_file}}) 'file': config_file}})
return config return config
def add_versions(config, release_version, bump_version) -> dict: def add_versions(config, release_version, bump_version) -> dict:
@ -27,28 +27,44 @@ class ReleaseMixin(DevopsBuild):
def __init__(self, project, config): def __init__(self, project, config):
super().__init__(project, config) super().__init__(project, config)
release_mixin_config = config['ReleaseMixin'] release_mixin_config = config['ReleaseMixin']
self.config_file = release_mixin_config['config_file'] self.file = release_mixin_config['file']
self.main_branch = release_mixin_config['main_branch'] self.main_branch = release_mixin_config['main_branch']
self.version_repo = VersionRepository(self.config_file) self.version_repo = VersionRepository(self.file)
self.git_repo = GitRepository()
self.release_version = None self.release_version = None
self.bump_version = None self.bump_version = None
self.commit_string = None self.commit_string = None
def init_release(self): def init(self):
init_service = InitReleaseService(self.version_repo) init_service = InitReleaseService(self.version_repo)
self.release_version = init_service.create_release_version(self.commit_string) self.release_version = init_service.create_release_version(self.commit_string)
self.bump_version = self.release_version.create_bump_version() release_version_copy = copy.deepcopy(self.release_version) # otherwise we'll modify the release_version again
self.bump_version = release_version_copy.create_bump_version()
def prepare_release(self): def prepare(self, version):
prepare_release_service = PrepareReleaseService(self.version_repo, self.config_file, self.main_branch) git_repository = GitRepository()
if self.release_version is None or self.bump_version is None: if self.main_branch not in git_repository.get_current_branch():
raise Exception('prepare_release was called before init_release') raise Exception('Trying to release while not on main branch')
# prepare_release_service.run_tests() # not implemented self.version_repo.write_file(version.get_version_string())
prepare_release_service.write_and_commit_release(self.release_version) git_repository.add_file(self.file)
prepare_release_service.write_and_commit_bump(self.bump_version) match version.release_type:
case None:
raise Exception('Release type not set but trying to commit.')
case ReleaseType.BUMP:
git_repository.commit(f'Version bump')
case _:
git_repository.commit(f'Release {version.get_version_string()}')
def tag_and_push(self): def tag_and_push(self, version):
tag_and_push_release_service = TagAndPushReleaseService(self.git_repo) git_repository = GitRepository()
tag_and_push_release_service.tag_and_push_release(self.release_version) match version.release_type:
case None:
raise Exception('Release type not set but trying to tag and push.')
case ReleaseType.BUMP:
annotation = 'v' + version.get_version_string()
message = 'Version bump'
case _:
annotation = 'v' + self.release_version.get_version_string()
message = 'Release' + annotation
git_repository.tag_annotated(annotation, message)
git_repository.push()

View file

@ -1,4 +1,3 @@
from pathlib import Path
from version_repository import VersionRepository from version_repository import VersionRepository
from release_type import ReleaseType from release_type import ReleaseType
from git_repository import GitRepository from git_repository import GitRepository
@ -17,53 +16,45 @@ class InitReleaseService():
else: else:
return GitRepository.create_from_commit_string(commit_string).get_release_type_from_latest_commit() return GitRepository.create_from_commit_string(commit_string).get_release_type_from_latest_commit()
def get_version(self): def get_version(self, release_type):
return self.version_repo.get_version() return self.version_repo.get_version(release_type)
def create_release_version(self, commit_string = None): def create_release_version(self, commit_string = None):
release_type = self.__calculate_release_type(commit_string) release_type = self.__calculate_release_type(commit_string)
version = self.get_version().create_release_version(release_type) version = self.get_version(release_type).create_release_version()
return version return version
def create_bump_version(self): def create_bump_version(self):
version = self.get_version().create_bump_version() version = self.get_version(ReleaseType.BUMP).create_bump_version()
return version return version
class PrepareReleaseService(): class PrepareReleaseService():
def __init__(self, version_repository: VersionRepository, git_repository: GitRepository, config_file: Path, main_branch: str): def __init__(self, version_repo: VersionRepository):
self.version_repository = version_repository self.version_repo = version_repo
self.git_repository = git_repository
self.main_branch = main_branch
self.config_file = config_file
def run_tests(self): # maybe auto? def run_tests(self): # maybe auto?
raise NotImplementedError pass
def __write_and_commit_version(self, version: Version, commit_message: str): def prepare_release(self, version: Version):
if self.main_branch != self.git_repository.get_current_branch(): # self.version_repo.write_file(version.get_version_string()) # side effect
raise Exception('Trying to release while not on main branch') pass
self.version_repository.write_file(version.get_version_string()) def prepare_bump(self, version: Version):
self.git_repository.add_file(self.config_file) # self.version_repo.write_file(version.get_version_string()) # side effect
self.git_repository.commit(commit_message) pass
def write_and_commit_release(self, release_version: Version):
self.__write_and_commit_version(release_version, commit_message=f'Release {release_version.get_version_string()}')
def write_and_commit_bump(self, bump_version: Version): # write
self.__write_and_commit_version(bump_version, commit_message=f'Version bump')
# add
# commit
pass
class TagAndPushReleaseService(): class TagAndPushReleaseService():
pass
def __init__(self, git_repository: GitRepository):
self.git_repository = git_repository
def tag_and_push_release(self, release_version: Version):
annotation = 'v' + release_version.get_version_string()
message = 'Release ' + annotation
self.git_repository.tag_annotated(annotation, message, 1)
self.git_repository.push()

View file

@ -34,10 +34,10 @@ class MyBuild(ReleaseMixin):
def initialize(project, CONFIG_FILE): def initialize(project, CONFIG_FILE):
project.build_depends_on('ddadevops>=3.1.2') project.build_depends_on('ddadevops>=3.1.2')
config = create_release_mixin_config(CONFIG_FILE, MAIN_BRANCH) config = create_release_mixin_config(CONFIG_FILE, MAIN_BRANCH)
config.update({'stage': STAGE, config.update({'stage': STAGE})
'module': MODULE, config.update({'module': MODULE})
'project_root_path': PROJECT_ROOT_PATH, config.update({'project_root_path': PROJECT_ROOT_PATH})
'build_dir_name': BUILD_DIR_NAME}) config.update({'build_dir_name': BUILD_DIR_NAME})
build = MyBuild(project, config) build = MyBuild(project, config)
return build return build
@ -56,7 +56,7 @@ def test_release_mixin(tmp_path):
# init # init
build = initialize(project, CONFIG_FILE) build = initialize(project, CONFIG_FILE)
build.commit_string = "MAJOR bla" build.commit_string = "MAJOR bla"
build.init_release() build.init()
release_version = build.release_version release_version = build.release_version
# test # test

View file

@ -22,33 +22,33 @@ from version_repository import VersionRepository
from release_type import ReleaseType from release_type import ReleaseType
def test_version(): def test_version():
version = Version([1, 2, 3]) version = Version([1, 2, 3], ReleaseType.SNAPSHOT)
version.increment(ReleaseType.SNAPSHOT) version.increment()
assert version.get_version_string() == "1.2.3-SNAPSHOT" assert version.get_version_string() == "1.2.3-SNAPSHOT"
assert version.version_list == [1, 2, 3] assert version.version_list == [1, 2, 3]
assert version.is_snapshot assert version.is_snapshot
version = Version([1, 2, 3]) version = Version([1, 2, 3], ReleaseType.BUMP)
version.increment(ReleaseType.BUMP) version.increment()
assert version.get_version_string() == "1.2.4-SNAPSHOT" assert version.get_version_string() == "1.2.4-SNAPSHOT"
assert version.version_list == [1, 2, 4] assert version.version_list == [1, 2, 4]
assert version.is_snapshot assert version.is_snapshot
version = Version([1, 2, 3]) version = Version([1, 2, 3], ReleaseType.PATCH)
version.increment(ReleaseType.PATCH) version.increment()
assert version.get_version_string() == "1.2.4" assert version.get_version_string() == "1.2.4"
assert version.version_list == [1, 2, 4] assert version.version_list == [1, 2, 4]
assert not version.is_snapshot assert not version.is_snapshot
version = Version([1, 2, 3]) version = Version([1, 2, 3], ReleaseType.MINOR)
version.increment(ReleaseType.MINOR) version.increment()
assert version.get_version_string() == "1.3.0" assert version.get_version_string() == "1.3.0"
assert version.version_list == [1, 3, 0] assert version.version_list == [1, 3, 0]
assert not version.is_snapshot assert not version.is_snapshot
version = Version([1, 2, 3]) version = Version([1, 2, 3], ReleaseType.MAJOR)
version.increment(ReleaseType.MAJOR) version.increment()
assert version.get_version_string() == "2.0.0" assert version.get_version_string() == "2.0.0"
assert version.version_list == [2, 0, 0] assert version.version_list == [2, 0, 0]
assert not version.is_snapshot assert not version.is_snapshot
@ -65,8 +65,8 @@ def test_gradle(tmp_path):
# test # test
repo = VersionRepository(f) repo = VersionRepository(f)
version = repo.get_version() version = repo.get_version(ReleaseType.SNAPSHOT)
version = version.create_release_version(ReleaseType.SNAPSHOT) version = version.create_release_version()
repo.write_file(version.get_version_string()) repo.write_file(version.get_version_string())
# check # check
@ -83,8 +83,8 @@ def test_json(tmp_path):
# test # test
repo = VersionRepository(f) repo = VersionRepository(f)
version = repo.get_version() version = repo.get_version(ReleaseType.SNAPSHOT)
version = version.create_release_version(ReleaseType.SNAPSHOT) version = version.create_release_version()
repo.write_file(version.get_version_string()) repo.write_file(version.get_version_string())
# check # check
@ -101,8 +101,8 @@ def test_clojure(tmp_path):
# test # test
repo = VersionRepository(f) repo = VersionRepository(f)
version = repo.get_version() version = repo.get_version(ReleaseType.SNAPSHOT)
version = version.create_release_version(ReleaseType.SNAPSHOT) version = version.create_release_version()
repo.write_file(version.get_version_string()) repo.write_file(version.get_version_string())
# check # check
@ -119,8 +119,8 @@ def test_python(tmp_path):
# test # test
repo = VersionRepository(f) repo = VersionRepository(f)
version = repo.get_version() version = repo.get_version(ReleaseType.SNAPSHOT)
version = version.create_release_version(ReleaseType.SNAPSHOT) version = version.create_release_version()
repo.write_file(version.get_version_string()) repo.write_file(version.get_version_string())
# check # check

View file

@ -3,14 +3,15 @@ from file_handlers import FileHandler
class Version(): class Version():
def __init__(self, version_list: list): def __init__(self, version_list: list, release_type: ReleaseType):
self.version_list = version_list self.version_list = version_list
self.release_type = release_type
self.version_string = None self.version_string = None
self.is_snapshot = None self.is_snapshot = None
def increment(self, release_type: ReleaseType): def increment(self):
self.is_snapshot = False self.is_snapshot = False
match release_type: match self.release_type:
case ReleaseType.BUMP: case ReleaseType.BUMP:
self.is_snapshot = True self.is_snapshot = True
self.version_list[ReleaseType.PATCH.value] += 1 self.version_list[ReleaseType.PATCH.value] += 1
@ -34,14 +35,15 @@ class Version():
self.version_string += "-SNAPSHOT" self.version_string += "-SNAPSHOT"
return self.version_string return self.version_string
def create_release_version(self, release_type: ReleaseType): def create_release_version(self):
release_version = Version(self.version_list.copy()) release_version = Version(self.version_list, self.release_type)
release_version.is_snapshot = self.is_snapshot release_version.is_snapshot = self.is_snapshot
release_version.increment(release_type) release_version.increment()
return release_version return release_version
def create_bump_version(self): def create_bump_version(self):
bump_version = Version(self.version_list.copy()) bump_version = Version(self.version_list, self.release_type)
bump_version.is_snapshot = self.is_snapshot bump_version.is_snapshot = self.is_snapshot
bump_version.increment(ReleaseType.BUMP) bump_version.release_type = ReleaseType.BUMP
bump_version.increment()
return bump_version return bump_version

View file

@ -21,11 +21,11 @@ class VersionRepository():
version_list, is_snapshot = self.file_handler.parse() version_list, is_snapshot = self.file_handler.parse()
return version_list, is_snapshot return version_list, is_snapshot
def get_version(self): def get_version(self, release_type):
self.file_handler = self.load_file() self.file_handler = self.load_file()
version_list, is_snapshot = self.parse_file() version_list, is_snapshot = self.parse_file()
version = Version(version_list) version = Version(version_list, release_type)
version.is_snapshot = is_snapshot version.is_snapshot = is_snapshot
return version return version