From f105fdd1b23f34d9e6a080050d23fb48abdb99ef Mon Sep 17 00:00:00 2001 From: Volodymyr Zahorniak <7808206+zahorniak@users.noreply.github.com> Date: Thu, 30 May 2024 10:30:13 +0300 Subject: [PATCH] feat: Migrate from Bash to Python (#12) --- .pre-commit-config.yaml | 7 +++++++ .pre-commit-hooks.yaml | 4 ++-- circleci_validate.py | 35 +++++++++++++++++++++++++++++++++++ circleci_validate.sh | 19 ------------------- setup.py | 13 +++++++++++++ 5 files changed, 57 insertions(+), 21 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100755 circleci_validate.py delete mode 100755 circleci_validate.sh create mode 100644 setup.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..ea50a90 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,7 @@ +repos: + - repo: https://github.com/psf/black + rev: 24.4.2 + hooks: + - id: black + name: Python black + language_version: python3.12 diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 5340876..ed4c3d7 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -1,7 +1,7 @@ - id: circleci_validate name: Validate CircleCI config description: This hook validates CircleCI config - entry: circleci_validate.sh - language: script + entry: circleci-validate + language: python files: .circleci/.*.yml pass_filenames: false diff --git a/circleci_validate.py b/circleci_validate.py new file mode 100755 index 0000000..ceedb2f --- /dev/null +++ b/circleci_validate.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 + +import os +import subprocess +import sys +import shutil + + +def main(): + # Check if running in CircleCI environment + if os.getenv("CIRCLECI"): + print("Circleci environment detected, skipping validation.") + sys.exit(0) + + # Check if CircleCI CLI is installed + if not shutil.which("circleci"): + print( + "Circleci CLI could not be found. Install the latest CLI version https://circleci.com/docs/2.0/local-cli/#installation" + ) + sys.exit(1) + + # Validate CircleCI config + try: + command = ["circleci", "config", "validate"] + sys.argv[1:] + result = subprocess.run(command, capture_output=True, text=True) + result.check_returncode() + print("CircleCI Configuration Passed Validation.") + except subprocess.CalledProcessError as e: + print("CircleCI Configuration Failed Validation.") + print(e.stderr) + sys.exit(1) + + +if __name__ == "__main__": + main() diff --git a/circleci_validate.sh b/circleci_validate.sh deleted file mode 100755 index a308577..0000000 --- a/circleci_validate.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env bash -set -e - -if [[ -n $CIRCLECI ]]; then - echo "Circleci environment detected, skipping validation." - exit 0 -fi - -if ! command -v circleci &> /dev/null -then - echo "Circleci CLI could not be found. Install the latest CLI version https://circleci.com/docs/2.0/local-cli/#installation" - exit 1 -fi - -if ! eMSG=$(circleci config validate "$@"); then - echo "CircleCI Configuration Failed Validation." - echo "${eMSG}" - exit 1 -fi diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..ab95ae6 --- /dev/null +++ b/setup.py @@ -0,0 +1,13 @@ +from setuptools import setup, find_packages + +setup( + name="pre_commit_circleci", + version="0.1.0", + packages=find_packages(), + py_modules=["circleci_validate"], + entry_points={ + "console_scripts": [ + "circleci-validate=circleci_validate:main", + ], + }, +)