From 736a7ca16ff685289913cd2b3d448f15fc341157 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Tue, 31 Jan 2023 05:26:32 +0000 Subject: [PATCH] Don't fail if the `actionlint` binary cannot be found Signed-off-by: Pedro Algarvio --- .pre-commit-config.yaml | 14 +++++++++----- tools/pre_commit.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 290dd90268f..14c143af2b4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,11 +4,6 @@ default_language_version: exclude: ^(doc/_static/.*|doc/_themes/.*)$ repos: - - repo: https://github.com/rhysd/actionlint - rev: v1.6.23 - hooks: - - id: actionlint - - repo: https://github.com/s0undt3ch/python-tools-scripts rev: "0.10.1" hooks: @@ -24,6 +19,15 @@ repos: - boto3==1.21.46 - pyyaml==6.0 - jinja2==3.1.2 + - id: tools + alias: actionlint + name: Lint GitHub Actions Workflows + files: "^.github/workflows/" + types: + - yaml + args: + - pre-commit + - actionlint - repo: https://github.com/saltstack/pip-tools-compile-impersonate rev: "4.6" diff --git a/tools/pre_commit.py b/tools/pre_commit.py index c3f63b521d6..7e5cbe7a2c6 100644 --- a/tools/pre_commit.py +++ b/tools/pre_commit.py @@ -6,6 +6,7 @@ from __future__ import annotations import logging import pathlib +import shutil from jinja2 import Environment, FileSystemLoader from ptscripts import Context, command_group @@ -69,3 +70,36 @@ def generate_workflows(ctx: Context): loaded_template = env.get_template(f"{template}.j2") rendered_template = loaded_template.render(**context) workflow_path.write_text(rendered_template.rstrip() + "\n") + + +@cgroup.command( + name="actionlint", + arguments={ + "files": { + "help": "Files to run actionlint against", + "nargs": "*", + }, + "no_color": { + "help": "Disable colors in output", + }, + }, +) +def actionlint(ctx: Context, files: list[str], no_color: bool = False): + """ + Run `actionlint` + """ + actionlint = shutil.which("actionlint") + if not actionlint: + ctx.warn("Could not find the 'actionlint' binary") + ctx.exit(0) + cmdline = [actionlint] + if no_color is False: + cmdline.append("-color") + shellcheck = shutil.which("shellcheck") + if shellcheck: + cmdline.append(f"-shellcheck={shellcheck}") + pyflakes = shutil.which("pyflakes") + if pyflakes: + cmdline.append(f"-pyflakes={pyflakes}") + ret = ctx.run(*cmdline, *files, check=False) + ctx.exit(ret.returncode)