Skip to content

Commit 90b7100

Browse files
kantordpeppescg
andauthored
add rule to require pre-commit to be configured in the repository (#242)
Co-authored-by: Giuseppe Scuglia <peppescg@gmail.com>
1 parent 5d6f64d commit 90b7100

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
tests:
2+
- name: "Should have at least one pre-commit hook configured"
3+
def: {}
4+
params: {}
5+
expect: "pass"
6+
git:
7+
repo_base: correct
8+
- name: "Should fail pre-commit is not configured with at least one hook"
9+
def: {}
10+
params: {}
11+
expect: "fail"
12+
git:
13+
repo_base: misconfigured
14+
- name: "Should fail is pre-commit is not configured at all"
15+
def: {}
16+
params: {}
17+
expect: "fail"
18+
git:
19+
repo_base: empty
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v3.2.0
4+
hooks:
5+
- id: trailing-whitespace
6+
- id: end-of-file-fixer
7+
- id: check-yaml
8+
- id: check-added-large-files
9+
10+
- repo: https://github.com/tenable/terrascan
11+
rev: 'v1.28.0'
12+
hooks:
13+
- id: terraform-pre-commit
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v3.2.0
4+
hooks: []
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
version: v1
3+
release_phase: alpha
4+
type: rule-type
5+
name: require_pre_commit_to_be_configured
6+
display_name: Require pre-commit to be installed
7+
short_failure_message: pre-commit configuration file missing
8+
severity:
9+
value: medium
10+
context: {}
11+
description: |
12+
Verifies that `pre-commit` is installed in the repository
13+
guidance: |
14+
Ensure that (pre-commit)[https://pre-commit.com/] is configured in a repository.
15+
16+
def:
17+
in_entity: repository
18+
rule_schema:
19+
type: object
20+
properties: {}
21+
ingest:
22+
type: git
23+
git: {}
24+
eval:
25+
type: rego
26+
rego:
27+
type: deny-by-default
28+
def: |
29+
package minder
30+
import future.keywords.if
31+
import future.keywords.every
32+
33+
default message := "pre-commit configuration file missing"
34+
default allow := false
35+
36+
37+
# pre-commit hook
38+
precommit := file.read(".pre-commit-config.yaml")
39+
40+
parsed_data := parse_yaml(precommit)
41+
42+
allow if {
43+
some repo_id, hook_id
44+
repo_data := parsed_data.repos[repo_id]
45+
hooks = repo_data["hooks"]
46+
47+
hooks[hook_id]
48+
}
49+
50+
message := "" if allow
51+
alert:
52+
type: security_advisory
53+
security_advisory: {}

0 commit comments

Comments
 (0)