From 359a8e80f782f9761826c7cfd624bc05ddb4d7d2 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Tue, 11 Apr 2023 14:36:57 +0100 Subject: [PATCH] Add actionlint to pre-commit Signed-off-by: Pedro Algarvio --- .github/actionlint.yaml | 4 ++++ .pre-commit-config.yaml | 13 +++++++++++ tools/__init__.py | 8 +++++++ tools/pre_commit.py | 51 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 .github/actionlint.yaml create mode 100644 tools/__init__.py create mode 100644 tools/pre_commit.py diff --git a/.github/actionlint.yaml b/.github/actionlint.yaml new file mode 100644 index 0000000..093a4fd --- /dev/null +++ b/.github/actionlint.yaml @@ -0,0 +1,4 @@ +self-hosted-runner: + # Labels of self-hosted runner in array of string + labels: + - repo-release diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b4d63be..e382cca 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -21,6 +21,19 @@ repos: hooks: - id: black + - repo: https://github.com/s0undt3ch/python-tools-scripts + rev: "0.12.0" + hooks: + - id: tools + alias: actionlint + name: Lint GitHub Actions Workflows + files: "^.github/workflows/" + types: + - yaml + args: + - pre-commit + - actionlint + - repo: local hooks: - id: generate-actions-workflow diff --git a/tools/__init__.py b/tools/__init__.py new file mode 100644 index 0000000..f1a2418 --- /dev/null +++ b/tools/__init__.py @@ -0,0 +1,8 @@ +import logging + +import ptscripts + +ptscripts.register_tools_module("tools.pre_commit") + +for name in ("boto3", "botocore", "urllib3"): + logging.getLogger(name).setLevel(logging.INFO) diff --git a/tools/pre_commit.py b/tools/pre_commit.py new file mode 100644 index 0000000..da24bf8 --- /dev/null +++ b/tools/pre_commit.py @@ -0,0 +1,51 @@ +""" +These commands are used by pre-commit. +""" +# pylint: disable=resource-leakage,broad-except,3rd-party-module-not-gated +from __future__ import annotations + +import logging +import shutil + +from ptscripts import Context, command_group + + +log = logging.getLogger(__name__) + +# Define the command group +cgroup = command_group( + name="pre-commit", help="Pre-Commit Related Commands", description=__doc__ +) + + +@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)