Compare commits

...

3 commits

Author SHA1 Message Date
94b993ccf5 Fix bugs and tests 2023-02-21 16:14:47 +01:00
6883435248 Refactor for code consistency 2023-02-21 15:52:09 +01:00
fce565d2d5 Implement git handler 2023-02-21 15:08:39 +01:00
6 changed files with 176 additions and 79 deletions

77
git_handler.py Normal file
View file

@ -0,0 +1,77 @@
import os
import subprocess as sub
# define semantics for release types by commit messages
## snapshot - snapshot release
## fix/patch - patch release
## bump - version bump release
## feature/feat/minor - minor release
## major/breaking - major release
GIT = 'git'
LOG = 'log'
FORMAT = '"%h %s"'
FORMAT_DEC = "%d"
PRETTY_OPTION = '--pretty='
DECORATE_OPTION = '--decorate=full'
class GitRepo():
def __init__(self):
self.commits = None
self.tags = None
def __clean_commit_string(self, commit_string):
return commit_string.replace('\"', "").replace('\n', "").split()
def __clean_tag_string(self, tag_string):
return tag_string.replace(" ", "").replace('\n', "")
def get_tags(self):
stream = sub.Popen([GIT,
LOG,
PRETTY_OPTION + FORMAT_DEC,
DECORATE_OPTION],
stdout=sub.PIPE,
stderr=sub.PIPE,
text=True,
encoding="UTF-8")
stdout = stream.stdout.readlines()
stderr = stream.stderr.readlines()
if len(stderr) > 0:
raise Exception(f"Git command failed with: {stderr}")
return stdout
def get_commits(self):
stream = sub.Popen([GIT,
LOG,
PRETTY_OPTION + FORMAT],
stdout=sub.PIPE,
stderr=sub.PIPE,
text=True,
encoding="UTF-8")
stdout = stream.stdout.readlines()
stderr = stream.stderr.readlines()
if len(stderr) > 0:
raise Exception(f"Git command failed with: {stderr}")
self.tags = self.get_tags()
self.commits = {}
if len(self.tags) != len(stdout):
raise Exception("Tags list did not match commits list")
for i, elem in enumerate(stdout):
commit_string = self.__clean_commit_string(elem)
commit_id = commit_string[0]
commit_message = " ".join(commit_string[1:len(commit_string)])
commit_tag = self.__clean_tag_string(self.tags[i])
self.commits[commit_id] = [commit_tag, commit_message]
return self.commits

View file

@ -1,3 +1,5 @@
import version_repository
import release_type
class InitReleaseService():
def __init__(self, commit_id, file):
@ -11,28 +13,23 @@ class InitReleaseService():
def __calculate_release_type(self):
pass
def get_version_repo(self):
self.version_repo = VersionRepository.get(self.file)
def get_version_list(self):
self.get_version_repo()
return self.version_repo.version_list
def get_version(self, release_type):
self.version_repo = VersionRepository(self.file)
return repo.get_version(release_type)
def create_release_version(self):
commit_message = self.read_commit_message(self.commit_id)
release_type = self.calculate_release_type(commit_message)
version_list = self.get_version_list()
release_version = Version.create_release_version(version_list, release_type)
self.version_repo.write_file(release_version.get_version_string())
version = self.get_version(release_type).create_release_version()
self.version_repo.write_file(version.get_version_string())
def create_bump_version(self):
if self.version_repo == None:
raise Exception('VersionRepo was not created. Did you run create_lease_version()?')
version_list = self.get_version_list()
bump_version = Version.create_bump_version(version_list)
self.version_repo.write_file(bump_version.get_version_string())
version = self.get_version(ReleaseType.BUMP).create_bump_version()
self.version_repo.write_file(version.get_version_string())

30
test/test_git_handler.py Normal file
View file

@ -0,0 +1,30 @@
from pathlib import Path
import sys
import os
# getting the name of the directory
# where the this file is present.
current = os.path.dirname(os.path.realpath(__file__))
# Getting the parent directory name
# where the current directory is present.
parent = os.path.dirname(current)
# adding the parent directory to
# the sys.path.
sys.path.append(parent)
# now we can import the module in the parent
# directory.
from git_handler import *
def test_git_handler():
# init
repo = GitRepo()
repo.get_commits()
#test
assert type(repo.commits) == dict
assert repo.commits["decd36b"] == ["(tag:refs/tags/TEST)", "Initial commit"]

View file

@ -19,36 +19,39 @@ sys.path.append(parent)
from version import Version
from version_repository import VersionRepository
from release_type import ReleaseType
from release_type import ReleaseType
def test_version():
version = Version([1, 2, 3], ReleaseType.SNAPSHOT)
version.increment(ReleaseType.SNAPSHOT)
version.increment()
assert version.get_version_string() == "1.2.3-SNAPSHOT"
assert version.version_list == [1, 2, 3]
assert version.is_snapshot
version.increment(ReleaseType.BUMP)
version = Version([1, 2, 3], ReleaseType.BUMP)
version.increment()
assert version.get_version_string() == "1.2.4-SNAPSHOT"
assert version.version_list == [1, 2, 4]
assert version.is_snapshot
version.increment(ReleaseType.PATCH)
assert version.get_version_string() == "1.2.5"
assert version.version_list == [1, 2, 5]
version = Version([1, 2, 3], ReleaseType.PATCH)
version.increment()
assert version.get_version_string() == "1.2.4"
assert version.version_list == [1, 2, 4]
assert not version.is_snapshot
version.increment(ReleaseType.SNAPSHOT)
assert version.get_version_string() == "1.2.5-SNAPSHOT"
version.increment(ReleaseType.SNAPSHOT)
assert version.get_version_string() == "1.2.5-SNAPSHOT"
version.increment(ReleaseType.MINOR)
version = Version([1, 2, 3], ReleaseType.MINOR)
version.increment()
assert version.get_version_string() == "1.3.0"
assert version.version_list == [1, 3, 0]
assert not version.is_snapshot
version.increment(ReleaseType.MAJOR)
version = Version([1, 2, 3], ReleaseType.MAJOR)
version.increment()
assert version.get_version_string() == "2.0.0"
assert version.version_list == [2, 0, 0]
assert not version.is_snapshot
def test_gradle(tmp_path):
@ -61,10 +64,10 @@ def test_gradle(tmp_path):
f.write_text(contents)
# test
version_repo = VersionRepository.get(f)
version_list = version_repo.version_list
version = Version.create_release_version(version_list, ReleaseType.SNAPSHOT)
version_repo.write_file(version.get_version_string())
repo = VersionRepository(f)
version = repo.get_version(ReleaseType.SNAPSHOT)
version.create_release_version()
repo.write_file(version.get_version_string())
# check
assert 'version = "12.4.678-SNAPSHOT"' in f.read_text()
@ -79,10 +82,10 @@ def test_json(tmp_path):
f.write_text(contents)
# test
version_repo = VersionRepository.get(f)
version_list = version_repo.version_list
version = Version.create_release_version(version_list, ReleaseType.SNAPSHOT)
version_repo.write_file(version.get_version_string())
repo = VersionRepository(f)
version = repo.get_version(ReleaseType.SNAPSHOT)
version.create_release_version()
repo.write_file(version.get_version_string())
# check
assert '"version": "123.123.456-SNAPSHOT"' in f.read_text()
@ -97,10 +100,10 @@ def test_clojure(tmp_path):
f.write_text(contents)
# test
version_repo = VersionRepository.get(f)
version_list = version_repo.version_list
version = Version.create_release_version(version_list, ReleaseType.SNAPSHOT)
version_repo.write_file(version.get_version_string())
repo = VersionRepository(f)
version = repo.get_version(ReleaseType.SNAPSHOT)
version.create_release_version()
repo.write_file(version.get_version_string())
# check
assert '1.1.3-SNAPSHOT' in f.read_text()
@ -115,10 +118,10 @@ def test_python(tmp_path):
f.write_text(contents)
# test
version_repo = VersionRepository.get(f)
version_list = version_repo.version_list
version = Version.create_release_version(version_list, ReleaseType.SNAPSHOT)
version_repo.write_file(version.get_version_string())
repo = VersionRepository(f)
version = repo.get_version(ReleaseType.SNAPSHOT)
version.create_release_version()
repo.write_file(version.get_version_string())
# check
assert '3.1.3-SNAPSHOT' in f.read_text()

View file

@ -7,11 +7,11 @@ class Version():
self.version_list = version_list
self.release_type = release_type
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
match release_type:
match self.release_type:
case ReleaseType.BUMP:
self.is_snapshot = True
self.version_list[ReleaseType.PATCH.value] += 1
@ -26,24 +26,20 @@ class Version():
self.version_list[ReleaseType.PATCH.value] = 0
self.version_list[ReleaseType.MINOR.value] = 0
self.version_list[ReleaseType.MAJOR.value] += 1
case None:
raise Exception("Release Type was not set!")
def get_version_string(self) -> str:
self.version_string = ".".join([str(x) for x in self.version_list])
if self.is_snapshot:
self.version_string += "-SNAPSHOT"
return self.version_string
@classmethod
def create_release_version(cls, version_list, release_type):
inst = cls(version_list, release_type)
if release_type == ReleaseType.PATCH:
inst.is_snapshot = False
def create_release_version(self):
if self.release_type == ReleaseType.PATCH:
self.is_snapshot = False
else:
inst.increment(release_type)
return inst
@classmethod
def create_bump_version(cls, version_list):
inst = cls(version_list, ReleaseType.BUMP)
inst.increment(release_type)
return inst
self.increment()
def create_bump_version(self):
self.increment(ReleaseType.BUMP)

View file

@ -1,12 +1,11 @@
from file_handlers import FileHandler
from version import Version
class VersionRepository():
def __init__(self, file):
self.file = file
self.file_handler = None
self.version_list = None
self.is_snapshot = None
self.file_handler = None
def load_file(self):
self.file_handler = FileHandler.from_file_path(self.file)
@ -14,25 +13,20 @@ class VersionRepository():
def write_file(self, version_string):
if self.file_handler is None:
raise Exception('Version was not created by load_file method.')
if self.version_list is None or self.is_snapshot is None:
raise Exception('Version or is_snapshot attribute not set.')
raise Exception('Version was not created by load_file method.')
else:
self.file_handler.write(version_string)
def parse_file(self, file_handler):
version_list, is_snapshot = file_handler.parse()
def parse_file(self):
version_list, is_snapshot = self.file_handler.parse()
return version_list, is_snapshot
def get_version(self, release_type):
@classmethod
def get(cls, file):
inst = cls(file)
self.file_handler = self.load_file()
version_list, is_snapshot = self.parse_file()
version = Version(version_list, release_type)
version.is_snapshot = is_snapshot
return version
file_handler = inst.load_file()
version_list, is_snapshot = inst.parse_file(file_handler)
inst.version_list = version_list
inst.is_snapshot = is_snapshot
inst.file_handler = file_handler
return inst