-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: Add action code from khalford/check-version-action
- Loading branch information
Showing
15 changed files
with
460 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FROM python:3 | ||
WORKDIR /app | ||
COPY . . | ||
RUN pip install -r requirements.txt | ||
CMD ["python","/app/src/main.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,65 @@ | ||
# check-version-action | ||
# Check Version | ||
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/khalford/check-version-action/self_test.yaml?label=Intergration%20Test) | ||
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/khalford/check-version-action/lint.yaml?label=Linting) | ||
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/khalford/check-version-action/test.yaml?label=Tests) | ||
[![codecov](https://codecov.io/gh/khalford/check-version-action/graph/badge.svg?token=441S7FRP3I)](https://codecov.io/gh/khalford/check-version-action) | ||
|
||
|
||
This action compares the application 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 | ||
|
||
The comparison follows the PEP 440 Version Identification and Dependency Specification. | ||
|
||
More detailed information about the versions can be found [here](https://packaging.python.org/en/latest/specifications/version-specifiers/) | ||
|
||
# Usage | ||
|
||
## Notes: | ||
|
||
As of October 2024 GitHub actions using Docker Containers can only be run on GitHub runners using a Linux operating system.<br> | ||
Read here for details: [Link to GitHub docs](https://docs.github.com/en/actions/sharing-automations/creating-actions/about-custom-actions#types-of-actions) | ||
|
||
The release tag is extracted and stored in `$GITHUB_ENV`, | ||
you can access this in your workflow with `$ {{ env.release_tag }}` | ||
|
||
<!-- start usage --> | ||
```yaml | ||
- name: Checkout main | ||
uses: actions/checkout@v4 | ||
with: | ||
# Change to "master" if needed | ||
ref: 'main' | ||
# Do not change the path here | ||
path: 'main' | ||
|
||
- name: Checkout current working branch | ||
uses: actions/checkout@v4 | ||
with: | ||
# Do not change the path here | ||
path: 'branch' | ||
|
||
- name: Compare versions | ||
# Don't run on main otherwise it will compare main with main | ||
if: ${{ github.ref != 'refs/heads/main' }} | ||
id: version_comparison | ||
uses: khalford/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 | ||
docker_compose_path: "docker-compose.yaml" | ||
|
||
- 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 --> | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: 'Check Semver Version Number' | ||
description: 'Check if the semver version number has changed from the main branch. Can also check if the docker compose file reflects the application version.' | ||
inputs: | ||
app_version_path: | ||
description: 'Path to main app version file.' | ||
required: true | ||
default: './version.txt' | ||
docker_compose_path: | ||
description: 'Path to compose file.' | ||
required: false | ||
outputs: | ||
app_updated: | ||
description: 'If the app version was updated or not.' | ||
compose_updated: | ||
description: 'If the compose version was updated or not.' | ||
|
||
runs: | ||
using: 'docker' | ||
image: 'Dockerfile' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
services: | ||
self-test: | ||
image: some/test:0.4.10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[pytest] | ||
pythonpath = src | ||
testpaths = tests | ||
python_files = *.py | ||
python_functions = test_* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
packaging | ||
pylint | ||
pytest | ||
pytest-cov |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""This is the base module including helper/abstract classes and errors.""" | ||
|
||
from abc import ABC, abstractmethod | ||
from pathlib import Path | ||
from typing import Union | ||
from packaging.version import Version | ||
|
||
|
||
class VersionNotUpdated(Exception): | ||
"""The version number has not been updated or updated incorrectly.""" | ||
|
||
|
||
class Base(ABC): | ||
"""The base abstract class to build features on.""" | ||
|
||
@abstractmethod | ||
def run(self, path1: Path, path2: Path) -> bool: | ||
""" | ||
This method is the entry point to the feature. | ||
It should take two paths and return the comparison result. | ||
""" | ||
|
||
@staticmethod | ||
@abstractmethod | ||
def read_files(path1: Path, path2: Path) -> (str, str): | ||
"""This method should read the contents of the compared files and return the strings""" | ||
|
||
@staticmethod | ||
@abstractmethod | ||
def get_version(content: str) -> Version: | ||
"""This method should extract the version from the file and return it as a packaging Version object""" | ||
|
||
@staticmethod | ||
@abstractmethod | ||
def compare(version1: Version, version2: Version) -> Union[bool, VersionNotUpdated]: | ||
"""This method should compare the versions and return a bool status""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
"""The comparison module which is where the main check classes are.""" | ||
|
||
from pathlib import Path | ||
from typing import Union, List, Type | ||
|
||
from packaging.version import Version | ||
from base import Base, VersionNotUpdated | ||
|
||
|
||
class CompareAppVersion(Base): | ||
"""This class compares the application versions""" | ||
|
||
def run(self, path1: Path, path2: Path) -> bool: | ||
""" | ||
Entry point to compare application 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 comparison == VersionNotUpdated: | ||
raise VersionNotUpdated( | ||
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 application versions we expect nothing else in the file than the version. | ||
:param content: Application version string | ||
:return: Application version object | ||
""" | ||
return Version(content) | ||
|
||
@staticmethod | ||
def compare(main: Version, branch: Version) -> Union[bool, Type[VersionNotUpdated]]: | ||
""" | ||
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 | ||
""" | ||
if branch > main: | ||
return True | ||
return VersionNotUpdated | ||
|
||
|
||
class CompareComposeVersion(Base): | ||
"""This class compares the docker compose image version to the application version.""" | ||
|
||
def run(self, app: Path, compose: Path) -> bool: | ||
""" | ||
Entry point to compare docker compose and application versions. | ||
:param app: Path to application 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 comparison == VersionNotUpdated: | ||
raise VersionNotUpdated( | ||
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) -> Union[bool, Type[VersionNotUpdated]]: | ||
""" | ||
Returns if the application 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 | ||
""" | ||
if app == compose: | ||
return True | ||
return VersionNotUpdated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""This module is the entry point for the Action.""" | ||
|
||
import os | ||
from pathlib import Path | ||
from comparison import CompareAppVersion, CompareComposeVersion | ||
|
||
|
||
def main(): | ||
""" | ||
The entry point function for the action. | ||
Here we get environment variables then set environment variables when finished. | ||
""" | ||
app_path = Path(os.environ.get("INPUT_APP_VERSION_PATH")) | ||
compose_path = os.environ.get("INPUT_DOCKER_COMPOSE_PATH") | ||
root_path = Path(os.environ.get("GITHUB_WORKSPACE")) | ||
main_path = root_path / "main" | ||
branch_path = root_path / "branch" | ||
with open(branch_path / app_path, "r", encoding="utf-8") as release_file: | ||
release_version = release_file.read().strip("\n") | ||
|
||
CompareAppVersion().run(main_path / app_path, branch_path / app_path) | ||
if compose_path: | ||
compose_path = Path(compose_path) | ||
CompareComposeVersion().run(branch_path / app_path, branch_path / compose_path) | ||
|
||
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}") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
"""Tests for comparison.CompareAppVersion""" | ||
|
||
from unittest.mock import patch, mock_open | ||
from pathlib import Path | ||
from packaging.version import Version | ||
import pytest | ||
from comparison import CompareAppVersion, VersionNotUpdated | ||
|
||
|
||
@pytest.fixture(name="instance", scope="function") | ||
def instance_fixture(): | ||
"""Provide a fixture instance for the tests""" | ||
return CompareAppVersion() | ||
|
||
|
||
@patch("comparison.CompareAppVersion.compare") | ||
@patch("comparison.CompareAppVersion.get_version") | ||
@patch("comparison.CompareAppVersion.read_files") | ||
def test_run(mock_read, mock_get_version, mock_compare, instance): | ||
"""Test the run method makes correct calls.""" | ||
mock_path1 = Path("mock1") | ||
mock_path2 = Path("mock2") | ||
mock_read.return_value = ("1.0.0", "1.0.1") | ||
mock_get_version.side_effect = [Version("1.0.0"), Version("1.0.0")] | ||
mock_compare.return_value = True | ||
res = instance.run(mock_path1, mock_path2) | ||
mock_read.assert_called_once_with(mock_path1, mock_path2) | ||
mock_get_version.assert_any_call("1.0.0") | ||
mock_get_version.assert_any_call("1.0.1") | ||
mock_compare.assert_called_once_with(Version("1.0.0"), Version("1.0.0")) | ||
assert res | ||
|
||
|
||
@patch("comparison.CompareAppVersion.compare") | ||
@patch("comparison.CompareAppVersion.get_version") | ||
@patch("comparison.CompareAppVersion.read_files") | ||
def test_run_fails(mock_read, _, mock_compare, instance): | ||
"""Test the run method fails.""" | ||
mock_read.return_value = ("mock1", "mock2") | ||
mock_compare.side_effect = VersionNotUpdated() | ||
with pytest.raises(VersionNotUpdated): | ||
instance.run(Path("mock1"), Path("mock2")) | ||
|
||
|
||
def test_read_files(instance): | ||
"""Test the read files method returns a tuple""" | ||
with patch("builtins.open", mock_open(read_data="1.0.0")): | ||
res = instance.read_files(Path("mock1"), Path("mock2")) | ||
assert res == ("1.0.0", "1.0.0") | ||
|
||
|
||
def test_get_version(instance): | ||
"""Test a version object is returned""" | ||
res = instance.get_version("1.0.0") | ||
assert isinstance(res, Version) | ||
|
||
|
||
def test_compare_pass(instance): | ||
"""Test that the compare returns true for a valid comparison""" | ||
res = instance.compare(Version("1.0.0"), Version("1.0.1")) | ||
assert res != VersionNotUpdated | ||
|
||
|
||
def test_compare_fails(instance): | ||
"""Test that the compare returns an error for an invalid comparison""" | ||
res = instance.compare(Version("1.0.1"), Version("1.0.0")) | ||
assert res == VersionNotUpdated |
Oops, something went wrong.