Skip to content

Commit

Permalink
Merge pull request #1 from stfc/add_action_code
Browse files Browse the repository at this point in the history
Add action code from khalford/check-version-action
  • Loading branch information
DavidFair authored Oct 24, 2024
2 parents c37757f + ac1bfe2 commit f72383b
Show file tree
Hide file tree
Showing 21 changed files with 577 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[report]

exclude_lines =
if __name__ == .__main__.:
28 changes: 28 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Lint
on: [push, pull_request]
jobs:
test_and_lint:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ "3.12", "3.x" ]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: "pip"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Analyse with pylint
run: |
python3 -m pylint src --recursive=true --rcfile=.pylintrc
python3 -m pylint tests --recursive=true --rcfile=.pylintrc
- name: Run Black formatter
uses: psf/black@stable
33 changes: 33 additions & 0 deletions .github/workflows/self_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Integration Test
on: [push, pull_request]
jobs:
self-test:
runs-on: ubuntu-latest
steps:
- name: Checkout Main to compare
uses: actions/checkout@v4
with:
ref: 'main'
path: 'main'

- uses: actions/checkout@v4
with:
path: 'branch'

- name: Self test
if: ${{ github.ref != 'refs/heads/main' }}
id: selftest
uses: khalford/check-version-action@main
with:
app_version_path: "version.txt"
docker_compose_path: "docker-compose.yml"

- 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!"
34 changes: 34 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Unit Test
on: [push, pull_request]
jobs:
test_and_lint:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ "3.12", "3.x" ]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: "pip"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests
run: |
python3 -m pytest tests
- name: Run tests and collect coverage
run: |
python3 -m pytest tests --cov-report xml:coverage.xml --cov
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
token: ${{secrets.CODECOV_TOKEN}}
files: cloud-coverage.xml
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea
main_version
branch_version
src/__pycache__/*
tests/__pycache__/*
13 changes: 13 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[FORMAT]
# Black will enforce 88 chars on Python code
# this will enforce 120 chars on docs / comments
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

[MASTER]
init-hook='import sys; sys.path.append("src")'
5 changes: 5 additions & 0 deletions Dockerfile
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"]
66 changes: 65 additions & 1 deletion README.md
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/stfc/check-version-action/self_test.yaml?label=Intergration%20Test)
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/stfc/check-version-action/lint.yaml?label=Linting)
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/stfc/check-version-action/test.yaml?label=Tests)
[![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.

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 -->
19 changes: 19 additions & 0 deletions action.yml
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'
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
services:
self-test:
image: some/test:1.0.0
5 changes: 5 additions & 0 deletions pytest.ini
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_*
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
packaging
pylint
pytest
pytest-cov
Empty file added src/__init__.py
Empty file.
36 changes: 36 additions & 0 deletions src/base.py
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"""
Loading

0 comments on commit f72383b

Please sign in to comment.