diff --git a/.github/labeler.yml b/.github/labeler.yml index 3f7fcb8c2..569315927 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -16,7 +16,7 @@ - all-globs-to-all-files: ['!doc/styles/**', '!doc/.vale.ini'] ## -- Other labels ------------------------------------------------------------ -'code-style': +'style': - any: - changed-files: - any-glob-to-any-file: ['.pre-commit-config.yaml', 'doc/.vale.ini'] @@ -38,4 +38,4 @@ 'deploy-pr-doc': - all: - changed-files: - - all-globs-to-all-files: ['THIS-NEVER-MATCHES-A-FILE'] \ No newline at end of file + - all-globs-to-all-files: ['THIS-NEVER-MATCHES-A-FILE'] diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ba4c95fac..5968054ef 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,5 +1,13 @@ repos: +- repo: local + hooks: + - id: shellcheck + name: shellcheck + entry: python bin/shellcheck + language: python + additional_dependencies: [shellcheck-py, pyyaml] + - repo: https://github.com/psf/black rev: 25.1.0 hooks: @@ -38,4 +46,4 @@ repos: files: '/.*\.(yml|py)$' args: - --start_year=2022 - exclude: '^(.github|doc)/.*' \ No newline at end of file + exclude: '^(.github|doc)/.*' diff --git a/bin/shellcheck b/bin/shellcheck new file mode 100644 index 000000000..fe3c9cf43 --- /dev/null +++ b/bin/shellcheck @@ -0,0 +1,36 @@ +import pathlib +import subprocess + +import yaml + +ACTIONS_DIRECTORY = pathlib.Path(__file__).parent.resolve() + +for actionfile in ACTIONS_DIRECTORY.glob("**/*.yml"): + if any(substring in str(actionfile) for substring in [".venv", "doc", ".github"]): + continue + + print(f"Reading {actionfile}", flush=True) + + try: + action = yaml.safe_load(actionfile.read_text(encoding="utf-8")) + except yaml.YAMLError as e: + print(f"YAML parse error in {actionfile}: {e}", flush=True) + continue + + # Check for existence of "runs" and "steps" + runs = action.get("runs", {}) + steps = runs.get("steps", []) + + for step in steps: + if step.get("shell") != "bash": + continue + if "run" not in step: + continue + + run_content = "#!/bin/bash\n" + step["run"] + result = subprocess.run( + ["shellcheck", "-"], + input=run_content, + capture_output=True, + text=True, + ) diff --git a/doc/source/changelog/906.maintenance.md b/doc/source/changelog/906.maintenance.md new file mode 100644 index 000000000..d126eeff9 --- /dev/null +++ b/doc/source/changelog/906.maintenance.md @@ -0,0 +1 @@ +Add shellcheck hook