Skip to content

Commit

Permalink
Merge pull request #5 from stfc/re-configure_features
Browse files Browse the repository at this point in the history
Refactor features
  • Loading branch information
khalford authored Feb 3, 2025
2 parents 249835d + 8b493e9 commit 52f1ac8
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 240 deletions.
10 changes: 0 additions & 10 deletions .github/workflows/self_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,3 @@ jobs:
app_version_path: "version.txt"
docker_compose_path: "docker-compose.yml"
labels: ${{ toJSON(github.event.pull_request.labels.*.name) }}

- name: Log Success
if: ${{ env.app_updated == 'true' }}
run: |
echo "App version has been updated correctly!"
- name: Log Success
if: ${{ env.compose_updated == 'true' }}
run: |
echo "Compose version has been updated correctly!"
8 changes: 5 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.idea
main_version
branch_version
src/__pycache__/*
tests/__pycache__/*
src/features/__pycache__/*
tests/__pycache__/*
.coverage
.coveragerc
coverage.xml
6 changes: 3 additions & 3 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ max-line-length=120

# Disable various warnings:

# W0237 Disabled as it makes the code more readable
# R0801 Disabled as it's a small amount of duplication
disable=W0237, R0801
# Disable R0801 as the code is only repeated twice.

disable=R0801

[MASTER]
init-hook='import sys; sys.path.append("src")'
19 changes: 4 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
[![codecov](https://codecov.io/gh/stfc/check-version-action/graph/badge.svg?token=OD2Z90ST8R)](https://codecov.io/gh/stfc/check-version-action)


This action compares the application version number from your working branch to the main branch.
This action compares the app version number from your working branch to the main branch.

You can also check that the **first** image version that appears in your `docker-compose.yaml` file will match the application version
You can also check that the **first** image version that appears in your `docker-compose.yaml` file matches the app version

The comparison follows the PEP 440 Version Identification and Dependency Specification.

Expand Down Expand Up @@ -42,27 +42,16 @@ If you are making a change which should not affect the version such as README or
path: 'branch'

- name: Compare versions
# Don't run on main otherwise it will compare main with main
# Don't run on main otherwise it compares main with main
if: ${{ github.ref != 'refs/heads/main' }}
id: version_comparison
uses: stfc/check-version-action@main
with:
# Path to version file from project root
app_version_path: "version.txt"
# Optional: To check if compose image version matches application version
# Optional: to check if Docker compose image version matches app version
docker_compose_path: "docker-compose.yaml"
labels: ${{ toJSON(github.event.pull_request.labels.*.name) }}

- name: Log App Success
if: ${{ env.app_updated == 'true' }}
run: |
echo "App version has been updated correctly!"
# Optional: If using the docker compose check
- name: Log Compose Success
if: ${{ env.compose_updated == 'true' }}
run: |
echo "Compose version has been updated correctly!"
```
<!-- end usage -->
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
services:
self-test:
image: some/test:1.1.0
image: some/test:1.2.0
36 changes: 0 additions & 36 deletions src/base.py

This file was deleted.

127 changes: 0 additions & 127 deletions src/comparison.py

This file was deleted.

Empty file added src/features/__init__.py
Empty file.
60 changes: 60 additions & 0 deletions src/features/app_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""Compare app version.txt on main to the branch."""

from pathlib import Path

from packaging.version import Version


class CompareAppVersion:
"""This class compares the app versions"""

def run(self, path1: Path, path2: Path) -> bool:
"""
Entry point to compare app versions.
:param path1: Path to main version
:param path2: Path to branch version
:return: true if success, error if fail
"""
main_content, branch_content = self.read_files(path1, path2)
main_ver = self.get_version(main_content)
branch_ver = self.get_version(branch_content)
comparison = self.compare(main_ver, branch_ver)
if not comparison:
raise RuntimeError(
f"The version in {('/'.join(str(path2).split('/')[4:]))[0:]} has not been updated correctly."
)
return True

@staticmethod
def read_files(path1: Path, path2: Path) -> (str, str):
"""
Read both version files and return the contents
:param path1: Path to main version
:param path2: Path to branched version
:return: main_ver, branch_ver
"""
with open(path1, "r", encoding="utf-8") as file1:
content1 = file1.read()
with open(path2, "r", encoding="utf-8") as file2:
content2 = file2.read()
return content1, content2

@staticmethod
def get_version(content: str) -> Version:
"""
This method returns the version from the file as an object
For app versions we expect nothing else in the file than the version.
:param content: app version string
:return: app version object
"""
return Version(content)

@staticmethod
def compare(main: Version, branch: Version) -> bool:
"""
Returns if the branch version is larger than the main version
:param main: Version on main
:param branch: Version on branch
:return: If the version update is correct return true, else return error
"""
return branch > main
67 changes: 67 additions & 0 deletions src/features/compose_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""Compare Docker compose image version to the version.txt."""

from pathlib import Path
from typing import List

from packaging.version import Version


class CompareComposeVersion:
"""This class compares the docker compose image version to the app version."""

def run(self, app: Path, compose: Path) -> bool:
"""
Entry point to compare docker compose and app versions.
:param app: Path to app version
:param compose: Path to compose image version
:return: true if success, error if fail
"""
app_content, compose_content = self.read_files(app, compose)
app_ver = Version(app_content)
compose_ver = self.get_version(compose_content)
comparison = self.compare(app_ver, compose_ver)
if not comparison:
raise RuntimeError(
f"The version in {('/'.join(str(compose).split('/')[4:]))[0:]}"
f" does not match {('/'.join(str(app).split('/')[4:]))[0:]}."
)
return True

@staticmethod
def read_files(app: Path, compose: Path) -> (str, List):
"""
Read both version files and return the contents
:param app: Path to app version
:param compose: Path to compose version
:return: main_ver, branch_ver
"""
with open(app, "r", encoding="utf-8") as file1:
content1 = file1.read()
with open(compose, "r", encoding="utf-8") as file2:
content2 = file2.readlines()
return content1, content2

@staticmethod
def get_version(content: List[str]) -> Version:
"""
This method returns the version from the file as an object
For compose versions we have to do some data handling.
:param content: Compose version string
:return: Compose version object
"""
version_str = ""
for line in content:
if "image" in line:
version_str = line.strip("\n").split(":")[-1]
break
return Version(version_str)

@staticmethod
def compare(app: Version, compose: Version) -> bool:
"""
Returns if the app version and docker compose version are equal.
:param app: App version
:param compose: Compose version
:return: If the version update is correct return true, else return error
"""
return app == compose
Loading

0 comments on commit 52f1ac8

Please sign in to comment.