From c79b3ce46bfc8d649eb549b487491217ce7dab14 Mon Sep 17 00:00:00 2001 From: Max Gautier Date: Mon, 17 Mar 2025 18:02:58 +0100 Subject: [PATCH] CI: convert galaxy version check to pre-commit + autodetect --- .gitlab-ci.yml | 1 - .gitlab-ci/lint.yml | 10 ------ .pre-commit-config.yaml | 8 +++++ scripts/galaxy_version.py | 49 +++++++++++++++++++++++++++ tests/scripts/check_galaxy_version.sh | 12 ------- 5 files changed, 57 insertions(+), 23 deletions(-) create mode 100755 scripts/galaxy_version.py delete mode 100755 tests/scripts/check_galaxy_version.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c1095a1e3..4af52a1ea 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -57,7 +57,6 @@ before_script: needs: - pipeline-image - ci-not-authorized - - check-galaxy-version # lint - pre-commit # lint - vagrant-validate # lint diff --git a/.gitlab-ci/lint.yml b/.gitlab-ci/lint.yml index 1fc50fe9a..c94a153f8 100644 --- a/.gitlab-ci/lint.yml +++ b/.gitlab-ci/lint.yml @@ -24,13 +24,3 @@ vagrant-validate: script: - ./tests/scripts/vagrant-validate.sh except: ['triggers', 'master'] - - -# TODO: convert to pre-commit hook -check-galaxy-version: - needs: [] - stage: test - tags: [ffci] - image: python:3 - script: - - tests/scripts/check_galaxy_version.sh diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4f298e502..585d4fe23 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -70,6 +70,14 @@ repos: - pathlib - pyaml + - id: check-galaxy-version + name: Verify correct version for galaxy.yml + entry: scripts/galaxy_version.py + language: python + pass_filenames: false + additional_dependencies: + - ruamel.yaml + - id: jinja-syntax-check name: jinja-syntax-check entry: tests/scripts/check-templates.py diff --git a/scripts/galaxy_version.py b/scripts/galaxy_version.py new file mode 100755 index 000000000..c04432651 --- /dev/null +++ b/scripts/galaxy_version.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python + +import subprocess +import ruamel.yaml +import os + +last_tag = ( + subprocess.Popen( + ["git", "describe", "--tags", "--abbrev=0"], stdout=subprocess.PIPE + ) + .communicate()[0] + .rstrip() + .decode("utf-8") + .removeprefix("v") + .split(".") +) +# Use CI provided base ref if available, else use HEAD to guess +git_branch = os.getenv( + "GITHUB_BASE_REF", + ( + subprocess.Popen( + ["git", "rev-parse", "--abbrev-ref", "HEAD"], stdout=subprocess.PIPE + ) + .communicate()[0] + .rstrip() + .decode("utf-8") + ), +) +if git_branch.startswith("release"): + version_comp_index = 2 +else: + version_comp_index = 1 + +last_tag[version_comp_index] = str(int(last_tag[version_comp_index]) + 1) +new_tag = ".".join(last_tag) + +yaml = ruamel.yaml.YAML() +yaml.indent(mapping=2, sequence=4, offset=2) +yaml.explicit_start = True + +with open( + "galaxy.yml", +) as galaxy_yml: + config = yaml.load(galaxy_yml) + +config["version"] = new_tag + +with open("galaxy.yml", "w") as galaxy_yml: + yaml.dump(config, galaxy_yml) diff --git a/tests/scripts/check_galaxy_version.sh b/tests/scripts/check_galaxy_version.sh deleted file mode 100755 index d663f121f..000000000 --- a/tests/scripts/check_galaxy_version.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -set -e - -version_from_galaxy=$(grep "^version:" galaxy.yml | awk '{print $2}') - -# TODO: compute the next expected version somehow -if [[ $KUBESPRAY_VERSION == "v${version_from_galaxy}" ]] -then - echo "Please update galaxy.yml version to match the next KUBESPRAY_VERSION." - echo "Be sure to remove the \"v\" to adhere to semantic versioning" - exit 1 -fi