diff --git a/.github/version_check.py b/.github/version_check.py new file mode 100644 index 0000000..2a3cf17 --- /dev/null +++ b/.github/version_check.py @@ -0,0 +1,53 @@ +import sys +import os +import re +from enum import Enum + + +class BumpType(Enum): + MAJOR = "major" + MINOR = "minor" + PATCH = "patch" + + +def is_valid_semver(version: str) -> bool: + semver_regex = r"^\d+\.\d+\.\d+$" + return bool(re.match(semver_regex, version)) + + +def raise_semver_exception(): + raise ValueError("Invalid version - must follow SEMVER convention (major.minor.patch)!") + + +def validate_semver_bump(previous_version: str, next_version: str) -> BumpType: + if not (is_valid_semver(previous_version) and is_valid_semver(next_version)): + raise_semver_exception() + + if previous_version == next_version: + raise ValueError("Version was not bumped!") + + print(f"Comparing next version {next_version} with previous version {previous_version}.") + + previous_version = list(map(int, previous_version.split("."))) + next_version = list(map(int, next_version.split("."))) + + semver_parts = [BumpType.MAJOR, BumpType.MINOR, BumpType.PATCH] + for idx in range(len(semver_parts)): + if next_version[idx] != previous_version[idx]: + correct_bump = next_version[idx] - 1 == previous_version[idx] + lower_levels_reset = all(next_version[jdx] == 0 for jdx in range(idx + 1, len(semver_parts))) + + if correct_bump and lower_levels_reset: + print(f"{semver_parts[idx].value} version bump.") + return semver_parts[idx] + + raise_semver_exception() + + +if __name__ == "__main__": + previous_version = sys.argv[1].removeprefix("v") + next_version = sys.argv[2].removeprefix("v") + + bump_type = validate_semver_bump(previous_version, next_version) + with open(os.getenv("GITHUB_OUTPUT"), "a") as output: + output.write(f"bump_type={bump_type.value}\n") \ No newline at end of file diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index 4c762f5..fb19bae 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -22,8 +22,38 @@ jobs: - name: Run tests run: cargo test + version-check: + needs: test + runs-on: ubuntu-latest + + outputs: + bump_type: ${{ steps.check_version.outputs.bump_type }} + new_version: ${{ steps.new_version.outputs.version }} + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Get new version + id: new_version + run: echo ::set-output name=version::$(grep -Po '^version = \"\K[^\"]+' Cargo.toml) + + - name: Get previous version + id: previous_version + run: echo ::set-output name=version::$(git describe --tags --abbrev=0 || echo '0.0.0') + + - name: Check version + id: check_version + run: | + python .github/version_check.py ${{ steps.previous_version.outputs.version }} ${{ steps.new_version.outputs.version }} + echo "${{ github.event_name}} ${{ github.ref }}" + build_and_release: - if: github.event_name == 'push' && github.ref == 'refs/heads/master' + needs: version-check + if: github.event_name == 'push' && github.ref == 'refs/heads/master' && needs.version-check.outputs.bump_type != 'patch' + runs-on: ubuntu-latest steps: - name: Checkout code diff --git a/Cargo.toml b/Cargo.toml index 325bc20..028a4df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "somo" -version = "1.0.0" +version = "1.0.1" edition = "2021" authors = ["theopfr"] description = "A human-friendly alternative to netstat for socket and port monitoring on Linux."