From f2ddc4e7dc707522b9fc68d31147917e276f423a Mon Sep 17 00:00:00 2001 From: Adam Moss <2951486+adam-moss@users.noreply.github.com> Date: Fri, 4 Aug 2023 09:29:53 +0100 Subject: [PATCH] feat: add pre-commit hook (#221) Signed-off-by: Adam Moss <2951486+adam-moss@users.noreply.github.com> --- .pre-commit-hooks.yaml | 17 +++++++++++++++++ README.md | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 .pre-commit-hooks.yaml diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 00000000..fc4b6333 --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -0,0 +1,17 @@ +- id: regal-lint + stages: + - pre-commit + language: golang + name: policy file linting + description: policy file linting with Regal from Styra built from source + entry: regal lint + files: (\.rego)$ + +- id: regal-lint-use-path + stages: + - pre-commit + language: system + name: policy file linting + description: policy file linting with Regal from Styra using system $PATH + entry: regal lint + files: (\.rego)$ diff --git a/README.md b/README.md index 55e66e78..3ebbf00b 100644 --- a/README.md +++ b/README.md @@ -281,6 +281,40 @@ Note that at this point in time, Regal only considers the line following the ign entire blocks of code (like rules, functions or even packages). See [configuration](#configuration) if you want to ignore certain rules altogether. +## Using Regal with Pre-Commit + +[Pre-Commit](https://pre-commit.com) is a framework for managing and maintaining multi-language pre-commit hooks. + +To use Regal with pre-commit, add this to your `.pre-commit-config.yaml` + +```yaml +- repo: https://github.com/StyraInc/regal + rev: v0.5.0 # Use the ref you want to point at + hooks: + - id: regal-lint + # - id: ... +``` + +### Hooks Available + +#### `regal-lint` + +![commit-msg hook](https://img.shields.io/badge/hook-pre--commit-informational?logo=git) + +Runs Regal against all staged `.rego` files, aborting the commit if any fail. + +- requires the `go` build chain is installed and available on `$PATH` +- will build and install the tagged version of Regal in an isolated `GOPATH` +- ensures compatibility between versions + +#### `regal-lint-use-path` + +![commit-msg hook](https://img.shields.io/badge/hook-pre--commit-informational?logo=git) + +Runs Regal against all staged `.rego` files, aborting the commit if any fail. + +- requires the `regal` package is already installed and available on `$PATH`. + ## Resources ### Documentation