Skip to content

Commit

Permalink
add rule to require pre-commit to be configured in the repository (#242)
Browse files Browse the repository at this point in the history
Co-authored-by: Giuseppe Scuglia <[email protected]>
  • Loading branch information
kantord and peppescg authored Dec 19, 2024
1 parent 5d6f64d commit 90b7100
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
19 changes: 19 additions & 0 deletions rule-types/common/require_pre_commit_to_be_configured.test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
tests:
- name: "Should have at least one pre-commit hook configured"
def: {}
params: {}
expect: "pass"
git:
repo_base: correct
- name: "Should fail pre-commit is not configured with at least one hook"
def: {}
params: {}
expect: "fail"
git:
repo_base: misconfigured
- name: "Should fail is pre-commit is not configured at all"
def: {}
params: {}
expect: "fail"
git:
repo_base: empty
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files

- repo: https://github.com/tenable/terrascan
rev: 'v1.28.0'
hooks:
- id: terraform-pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks: []
53 changes: 53 additions & 0 deletions rule-types/common/require_pre_commit_to_be_configured.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
version: v1
release_phase: alpha
type: rule-type
name: require_pre_commit_to_be_configured
display_name: Require pre-commit to be installed
short_failure_message: pre-commit configuration file missing
severity:
value: medium
context: {}
description: |
Verifies that `pre-commit` is installed in the repository
guidance: |
Ensure that (pre-commit)[https://pre-commit.com/] is configured in a repository.
def:
in_entity: repository
rule_schema:
type: object
properties: {}
ingest:
type: git
git: {}
eval:
type: rego
rego:
type: deny-by-default
def: |
package minder
import future.keywords.if
import future.keywords.every
default message := "pre-commit configuration file missing"
default allow := false
# pre-commit hook
precommit := file.read(".pre-commit-config.yaml")
parsed_data := parse_yaml(precommit)
allow if {
some repo_id, hook_id
repo_data := parsed_data.repos[repo_id]
hooks = repo_data["hooks"]
hooks[hook_id]
}
message := "" if allow
alert:
type: security_advisory
security_advisory: {}

0 comments on commit 90b7100

Please sign in to comment.