Skip to content

Commit

Permalink
Added methods related to version control to new file
Browse files Browse the repository at this point in the history
Moved methods that relate to updating or retrieving a version to new file - versioning.py
reduced comparison to single method
  • Loading branch information
DaveW-STFC committed Dec 12, 2024
1 parent b7503e6 commit d1d139d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 83 deletions.
7 changes: 1 addition & 6 deletions src/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
54 changes: 8 additions & 46 deletions src/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 7 additions & 31 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -17,29 +16,26 @@ 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:
# We can assume either/both of these values returned true otherwise they would have errored
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


Expand All @@ -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")
Expand Down
33 changes: 33 additions & 0 deletions src/versioning.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d1d139d

Please sign in to comment.