diff --git a/src/base.py b/src/base.py index 8828d6a..01d20fb 100644 --- a/src/base.py +++ b/src/base.py @@ -13,13 +13,8 @@ class Base(ABC): """The base abstract class to build features on.""" @abstractmethod - def run(self, path1: Path, target_version: semver) -> bool: + def run(self, path1: Path, old_semver, target_version: semver) -> bool: """ This method is the entry point to the feature. It should take a file path to retrieve a version from and the new Semantic Version and return the comparison result. """ - - @staticmethod - @abstractmethod - def read_files(path1: Path) -> str: - """This method should read the contents of the compared files and return the strings""" diff --git a/src/comparison.py b/src/comparison.py index 1bd94e6..727b663 100644 --- a/src/comparison.py +++ b/src/comparison.py @@ -2,62 +2,24 @@ from pathlib import Path import semver -from base import Base, VersionNotUpdated +from base import VersionNotUpdated -class CompareAppVersion(Base): +class CompareVersion: """This class compares the application version to the updated semantic version.""" - def run(self, path1: Path, target_version) -> bool: + @staticmethod + def run(path1: Path, file_semver, target_version) -> bool: """ Entry point to compare application versions. :param path1: Path to main version - :param target_version: The updated Semantic Version + :param file_semver - the version extracted from the updated file + :param target_version: The updated Semantic Version that the file version should match :return: true if success, error if fail """ - branch_content = self.read_files(path1) - if semver.compare(str(target_version), branch_content) == 1: - raise VersionNotUpdated( - f"The version in {('/'.join(str(path1).split('/')[4:]))[0:]} has not been updated correctly." - ) - return True - - @staticmethod - def read_files(path1: Path) -> str: - """ - Read both version files and return the contents - :param path1: Path to branched version - :return: branch_ver - """ - with open(path1, "r", encoding="utf-8") as file1: - content1 = file1.read() - return content1 - -class CompareComposeVersion(Base): - """This class compares the docker compose image version to the updated semantic version.""" - - def run(self, compose: Path, target_version) -> bool: - """ - Entry point to compare docker compose and updated semantic versions. - :param compose: Path to compose image version - :param target_version: Updated Semantic Version - :return: true if success, error if fail - """ - compose_content = self.read_files(compose) - if semver.compare(str(target_version), compose_content) == 1: + if semver.compare(str(target_version), file_semver) == 1: raise VersionNotUpdated( - f"The version in {('/'.join(str(compose).split('/')[4:]))[0:]} has not been updated correctly." + f"The version in {('/'.join(str(path1).split('/')[4:]))[0:]} has not been updated correctly." ) return True - - @staticmethod - def read_files(compose: Path) -> str: - """ - Read both version files and return the contents - :param compose: Path to compose version - :return: branch_ver - """ - with open(compose, "r", encoding="utf-8") as file1: - content1 = file1.read() - return content1 diff --git a/src/main.py b/src/main.py index cea9773..a536e5f 100644 --- a/src/main.py +++ b/src/main.py @@ -2,10 +2,9 @@ import os from typing import List - -import semver from pathlib import Path -from comparison import CompareAppVersion, CompareComposeVersion +from comparison import CompareVersion +from src.versioning import bump_semver_by_label, get_semver, read_file def main() -> bool: @@ -17,21 +16,18 @@ def main() -> bool: main_path = root_path / "main" branch_path = root_path / "branch" - # get the old semver before changes - # get new semver - # compare the two versions labels = get_labels() if should_skip_step(labels): return False old_semver = get_semver(main_path / app_path) new_semver = bump_semver_by_label(old_semver, labels) - # incremented_correctly = semver_incremented_correctly(old_semver, new_semver) - CompareAppVersion().run(branch_path / app_path, new_semver) + branch_content = read_file(branch_path / app_path) + compose_content = read_file(branch_path / compose_path) + CompareVersion().run(branch_path / app_path, branch_content, new_semver) if compose_path: - compose_path = Path(compose_path) - CompareComposeVersion().run(branch_path / compose_path, new_semver) + CompareVersion().run(branch_path / compose_path, compose_content, new_semver) github_env = os.getenv("GITHUB_ENV") with open(github_env, "a", encoding="utf-8") as env: @@ -39,7 +35,7 @@ def main() -> bool: env.write("app_updated=true\n") if compose_path: env.write("compose_updated=true") - env.write(f"release_tag={release_version}") + env.write(f"release_tag={new_semver}") return True @@ -55,26 +51,6 @@ def should_skip_step(labels) -> bool: if label in ["documentation", "workflow"]: return False -def get_semver(file: Path) -> semver: - with open(file, "r", encoding="utf-8") as release_file: - return release_file.read().strip("\n") - - -def bump_semver_by_label(semver: semver.Version, labels: List[str]) -> semver.Version: - for label in labels: - - if label == "major": - bumped_semver = semver.bump_major() - elif label == "minor": - bumped_semver = semver.bump_minor() - elif label == "patch": - bumped_semver = semver.bump_patch() - - return bumped_semver - -def semver_incremented_correctly(old_semver, new_semver) -> bool: - # todo - pass def get_labels() -> List[str]: labels = os.environ.get("INPUT_LABELS") diff --git a/src/versioning.py b/src/versioning.py new file mode 100644 index 0000000..fbb3a4f --- /dev/null +++ b/src/versioning.py @@ -0,0 +1,33 @@ +from typing import List + +import semver +from pathlib import Path + + +def get_semver(file: Path) -> semver: + with open(file, "r", encoding="utf-8") as release_file: + return release_file.read().strip("\n") + + +def read_file(path1: Path) -> str: + """ + Read both version files and return the contents + :param path1: Path to branched version + :return: branch_ver + """ + with open(path1, "r", encoding="utf-8") as file1: + content1 = file1.read() + return content1 + + +def bump_semver_by_label(old_semver: semver.Version, labels: List[str]) -> semver.Version: + for label in labels: + + if label == "major": + bumped_semver = old_semver.bump_major() + elif label == "minor": + bumped_semver = old_semver.bump_minor() + elif label == "patch": + bumped_semver = old_semver.bump_patch() + + return bumped_semver