Skip to content

Commit 906facc

Browse files
feat: add conftest-doc pre-commit hook with documentation
Adds a new conftest-doc hook to .pre-commit-hooks.yaml that uses 'policy' directory by default. Includes documentation for output directory and template options. Closes #1249 Signed-off-by: Julien Rottenberg <julien@rottenberg.info> Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent e130513 commit 906facc

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

.pre-commit-hooks.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,14 @@
3737
require_serial: false
3838
minimum_pre_commit_version: "2.9.0"
3939
stages: [pre-commit, pre-merge-commit, pre-push, manual]
40+
41+
- id: conftest-doc
42+
name: Conftest Doc Policy Documentation
43+
description: Generate policy documentation, fails if docs are out of date. Defaults to policy directory. Supports -o for output directory and --template for custom template.
44+
entry: conftest doc
45+
args: [policy]
46+
language: golang
47+
pass_filenames: false
48+
require_serial: true
49+
minimum_pre_commit_version: "2.9.0"
50+
stages: [pre-commit, pre-merge-commit, pre-push, manual]

docs/pre_commit.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,36 @@ repos:
2121
The `conftest-test` hook validates your configuration files against policies,
2222
while the `conftest-verify` hook runs unit tests for your policies.
2323

24+
## Documentation Check
25+
26+
The `conftest-doc` hook ensures your policy documentation stays in sync with
27+
your Rego policies. It runs `conftest doc` and if the generated documentation
28+
differs from what's committed, pre-commit will fail. The updated documentation
29+
files are written, so you can simply stage them and re-commit.
30+
31+
By default, the hook documents the `policy` directory. Specifying `args`
32+
replaces the default, so you must include the policy directory path.
33+
34+
```yaml
35+
repos:
36+
- repo: https://github.com/open-policy-agent/conftest
37+
rev: v0.64.0
38+
hooks:
39+
- id: conftest-doc
40+
# Uses 'policy' directory by default, or specify your own:
41+
# args: [path/to/your/policies]
42+
# To specify an output directory, use -o:
43+
# args: [-o, docs/, path/to/your/policies]
44+
# To use a custom template, use --template:
45+
# args: [--template, path/to/template.md, path/to/your/policies]
46+
# Combined example:
47+
# args: [-o, docs/, --template, path/to/template.md, path/to/your/policies]
48+
# To only run when .rego files in a specific directory change:
49+
# files: ^path/to/your/policies/.*\.rego$
50+
```
51+
52+
## Additional Hooks
53+
2454
Additional hooks are available including `conftest-pull` for downloading
2555
policies and `conftest-fmt` for formatting Rego files. See the
2656
[.pre-commit-hooks.yaml](https://github.com/open-policy-agent/conftest/blob/main/.pre-commit-hooks.yaml)

0 commit comments

Comments
 (0)