Compare commits

..

7 commits

Author SHA1 Message Date
bom
4a23b0b800 Implement and use TagAndPushReleaseService 2023-02-23 15:33:34 +01:00
bom
e7152301dd Add utility functions to git_repository 2023-02-23 15:33:00 +01:00
bom
fa6beebc4e Move commit logic to prepare_release_service 2023-02-23 15:15:29 +01:00
bom
c58ddf70da Compact updating config into one call 2023-02-23 14:40:10 +01:00
bom
1ce4bd9c55 Remove release_type as requirement for Version
Require it in functions that actually use it instead
2023-02-23 14:38:54 +01:00
bom
992c72bda3 Give 'file' a more descriptive name 2023-02-23 14:23:32 +01:00
bom
0df4db54d8 Copy the version list when creating new Version 2023-02-23 14:16:17 +01:00
7 changed files with 93 additions and 94 deletions

View file

@ -16,9 +16,14 @@ 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):
self.system_repository.run_checked('git', 'log', '--oneline', '--format="%s %b"', '-n 1') output = self.get_latest_n_commits(1)
self.latest_commit = " ".join(self.system_repository.stdout) # returns a list of strings otherwise self.latest_commit = " ".join(output) # 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:
@ -38,6 +43,9 @@ 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,
'file': config_file}}) 'config_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,44 +27,28 @@ 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.file = release_mixin_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.version_repo = VersionRepository(self.file) self.version_repo = VersionRepository(self.config_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(self): def init_release(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)
release_version_copy = copy.deepcopy(self.release_version) # otherwise we'll modify the release_version again self.bump_version = self.release_version.create_bump_version()
self.bump_version = release_version_copy.create_bump_version()
def prepare(self, version): def prepare_release(self):
git_repository = GitRepository() prepare_release_service = PrepareReleaseService(self.version_repo, self.config_file, self.main_branch)
if self.main_branch not in git_repository.get_current_branch(): if self.release_version is None or self.bump_version is None:
raise Exception('Trying to release while not on main branch') raise Exception('prepare_release was called before init_release')
self.version_repo.write_file(version.get_version_string()) # prepare_release_service.run_tests() # not implemented
git_repository.add_file(self.file) prepare_release_service.write_and_commit_release(self.release_version)
match version.release_type: prepare_release_service.write_and_commit_bump(self.bump_version)
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, version): def tag_and_push(self):
git_repository = GitRepository() tag_and_push_release_service = TagAndPushReleaseService(self.git_repo)
match version.release_type: tag_and_push_release_service.tag_and_push_release(self.release_version)
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,3 +1,4 @@
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
@ -5,7 +6,7 @@ from version import Version
class InitReleaseService(): class InitReleaseService():
def __init__(self, version_repo: VersionRepository): def __init__(self, version_repo: VersionRepository):
if version_repo is None: if version_repo is None:
raise Exception('VersionRepo was not created. Did you run create_release_version()?') raise Exception('VersionRepo was not created. Did you run create_release_version()?')
self.version_repo = version_repo self.version_repo = version_repo
@ -16,45 +17,53 @@ 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, release_type): def get_version(self):
return self.version_repo.get_version(release_type) return self.version_repo.get_version()
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(release_type).create_release_version() version = self.get_version().create_release_version(release_type)
return version return version
def create_bump_version(self): def create_bump_version(self):
version = self.get_version(ReleaseType.BUMP).create_bump_version() version = self.get_version().create_bump_version()
return version return version
class PrepareReleaseService(): class PrepareReleaseService():
def __init__(self, version_repo: VersionRepository): def __init__(self, version_repository: VersionRepository, git_repository: GitRepository, config_file: Path, main_branch: str):
self.version_repo = version_repo self.version_repository = version_repository
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?
pass raise NotImplementedError
def prepare_release(self, version: Version): def __write_and_commit_version(self, version: Version, commit_message: str):
# self.version_repo.write_file(version.get_version_string()) # side effect if self.main_branch != self.git_repository.get_current_branch():
pass raise Exception('Trying to release while not on main branch')
self.version_repository.write_file(version.get_version_string())
self.git_repository.add_file(self.config_file)
self.git_repository.commit(commit_message)
def prepare_bump(self, version: Version): def write_and_commit_release(self, release_version: Version):
# self.version_repo.write_file(version.get_version_string()) # side effect self.__write_and_commit_version(release_version, commit_message=f'Release {release_version.get_version_string()}')
pass
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,
config.update({'module': MODULE}) 'module': MODULE,
config.update({'project_root_path': PROJECT_ROOT_PATH}) 'project_root_path': PROJECT_ROOT_PATH,
config.update({'build_dir_name': BUILD_DIR_NAME}) '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() build.init_release()
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], ReleaseType.SNAPSHOT) version = Version([1, 2, 3])
version.increment() version.increment(ReleaseType.SNAPSHOT)
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], ReleaseType.BUMP) version = Version([1, 2, 3])
version.increment() version.increment(ReleaseType.BUMP)
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], ReleaseType.PATCH) version = Version([1, 2, 3])
version.increment() version.increment(ReleaseType.PATCH)
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], ReleaseType.MINOR) version = Version([1, 2, 3])
version.increment() version.increment(ReleaseType.MINOR)
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], ReleaseType.MAJOR) version = Version([1, 2, 3])
version.increment() version.increment(ReleaseType.MAJOR)
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(ReleaseType.SNAPSHOT) version = repo.get_version()
version = version.create_release_version() version = version.create_release_version(ReleaseType.SNAPSHOT)
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(ReleaseType.SNAPSHOT) version = repo.get_version()
version = version.create_release_version() version = version.create_release_version(ReleaseType.SNAPSHOT)
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(ReleaseType.SNAPSHOT) version = repo.get_version()
version = version.create_release_version() version = version.create_release_version(ReleaseType.SNAPSHOT)
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(ReleaseType.SNAPSHOT) version = repo.get_version()
version = version.create_release_version() version = version.create_release_version(ReleaseType.SNAPSHOT)
repo.write_file(version.get_version_string()) repo.write_file(version.get_version_string())
# check # check

View file

@ -3,15 +3,14 @@ from file_handlers import FileHandler
class Version(): class Version():
def __init__(self, version_list: list, release_type: ReleaseType): def __init__(self, version_list: list):
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): def increment(self, release_type: ReleaseType):
self.is_snapshot = False self.is_snapshot = False
match self.release_type: match 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
@ -35,15 +34,14 @@ class Version():
self.version_string += "-SNAPSHOT" self.version_string += "-SNAPSHOT"
return self.version_string return self.version_string
def create_release_version(self): def create_release_version(self, release_type: ReleaseType):
release_version = Version(self.version_list, self.release_type) release_version = Version(self.version_list.copy())
release_version.is_snapshot = self.is_snapshot release_version.is_snapshot = self.is_snapshot
release_version.increment() release_version.increment(release_type)
return release_version return release_version
def create_bump_version(self): def create_bump_version(self):
bump_version = Version(self.version_list, self.release_type) bump_version = Version(self.version_list.copy())
bump_version.is_snapshot = self.is_snapshot bump_version.is_snapshot = self.is_snapshot
bump_version.release_type = ReleaseType.BUMP bump_version.increment(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, release_type): def get_version(self):
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, release_type) version = Version(version_list)
version.is_snapshot = is_snapshot version.is_snapshot = is_snapshot
return version return version