Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the validation into a pre-commit hook #1083

Merged
merged 1 commit into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-validator
- 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
33 changes: 1 addition & 32 deletions c2cciutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
import glob
import json
import os.path
import pkgutil
import re
import subprocess # nosec
import sys
from typing import Any, Dict, List, Match, Optional, Pattern, Tuple, TypedDict, cast

import jsonschema_gentypes.validate
import magic
import requests
import ruamel.yaml
Expand Down Expand Up @@ -135,36 +133,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-validator

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
98 changes: 2 additions & 96 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ google-api-python-client = { version = "2.83.0", optional = true }
google-auth-httplib2 = { version = "0.1.0", optional = true }
google-auth-oauthlib = { version = "1.0.0", optional = true }
"ruamel.yaml" = "0.17.21"
jsonschema-gentypes = "1.5.0"
defusedxml = "0.7.1"
twine = { version = "4.0.2", optional = true }
codespell = { version = "2.2.4", optional = true }
Expand Down