Skip to content

Commit

Permalink
Move the validation into a pre-commit hook
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Apr 12, 2023
1 parent 576185d commit b5e2653
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
12 changes: 11 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ci:
- helm-lock
- ripsecrets
- jsonschema-gentypes
- jsonschema-validate
- jsonschema2md

repos:
Expand Down Expand Up @@ -36,9 +37,18 @@ repos:
- id: jsonschema-gentypes
files: |-
(?x)(
ĵsonschema-gentypes.yaml$
^ĵsonschema-gentypes.yaml$
|^c2cciutils/schema.*\.json$
)
- repo: https://github.com/sbrunner/jsonschema-validator
rev: 0.1.0
hooks:
- id: jsonschema-validator
files: |-
(?x)^(
ci/config\.yaml
|example-project/ci/config\.yaml
)$
- repo: https://github.com/sbrunner/jsonschema2md2
rev: 0.6.0
hooks:
Expand Down
32 changes: 1 addition & 31 deletions c2cciutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import glob
import json
import os.path
import pkgutil
import re
import subprocess # nosec
import sys
Expand Down Expand Up @@ -135,36 +134,7 @@ def get_config() -> c2cciutils.configuration.Configuration:
}
merge(default_config, config)

return validate_config(config, "ci/config.yaml")


def validate_config(
config: c2cciutils.configuration.Configuration, config_file: str
) -> c2cciutils.configuration.Configuration:
"""
Validate the configuration.
Arguments:
config: The configuration to be validated
config_file: The configuration file name, used to build the error messages
Return the configuration (used to be chained)
Print an message and eventually exit on validation error.
"""
schema_data = pkgutil.get_data("c2cciutils", "schema.json")
assert schema_data is not None

errors, data = jsonschema_gentypes.validate.validate(
config_file, cast(Dict[str, Any], config), json.loads(schema_data)
)

if errors:
formated_errors = "\n".join(errors)
print(f"The config file is invalid:\n{formated_errors}")
if os.environ.get("IGNORE_CONFIG_ERROR", "FALSE").lower() != "true":
sys.exit(1)

return cast(c2cciutils.configuration.Configuration, data)
return config


def error(
Expand Down
18 changes: 18 additions & 0 deletions example-project/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
# https://pre-commit.com/hooks.html

ci:
autoupdate_schedule: quarterly
skip:
- copyright
- poetry-check
- poetry-lock
- ripsecrets
- jsonschema-validate

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
Expand All @@ -17,6 +27,14 @@ repos:
rev: 0.1.4
hooks:
- id: copyright
- repo: https://github.com/sbrunner/jsonschema-validator
rev: 0.1.0
hooks:
- id: jsonschema-validator
files: |
(?x)^(
ci/config\\.yaml
)$
- repo: https://github.com/camptocamp/c2cciutils
rev: 1.4.0
hooks:
Expand Down

0 comments on commit b5e2653

Please sign in to comment.