generated from coulomb/repo-seed
Harden registry API and schema validation
This commit is contained in:
@@ -7,9 +7,8 @@ from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import jsonschema
|
||||
|
||||
from .loader import load_yaml, repo_root
|
||||
from .loader import repo_root
|
||||
from .schema_validation import draft202012_validator
|
||||
|
||||
|
||||
class RegistryError(Exception):
|
||||
@@ -530,6 +529,30 @@ class RegistryStore:
|
||||
],
|
||||
}
|
||||
|
||||
def status(self) -> dict[str, Any]:
|
||||
with self._connect() as db:
|
||||
counts = {
|
||||
"repositories": db.execute("select count(*) from repositories").fetchone()[0],
|
||||
"snapshots": db.execute("select count(*) from snapshots").fetchone()[0],
|
||||
"artifacts": db.execute("select count(*) from artifacts").fetchone()[0],
|
||||
"libraries": db.execute("select count(*) from libraries").fetchone()[0],
|
||||
}
|
||||
latest = [
|
||||
{
|
||||
"repo_slug": snapshot["repo_slug"],
|
||||
"snapshot_id": snapshot["id"],
|
||||
"commit": snapshot["commit"],
|
||||
"generated_at": snapshot["generated_at"],
|
||||
}
|
||||
for snapshot in self.latest_snapshots()
|
||||
]
|
||||
return {
|
||||
"status": "ok",
|
||||
"database": str(self.path),
|
||||
"counts": counts,
|
||||
"latest_snapshots": latest,
|
||||
}
|
||||
|
||||
def _connect(self) -> sqlite3.Connection:
|
||||
db = sqlite3.connect(self.path)
|
||||
db.row_factory = sqlite3.Row
|
||||
@@ -537,19 +560,8 @@ class RegistryStore:
|
||||
|
||||
|
||||
def validate_graph_export(graph: dict[str, Any]) -> None:
|
||||
schemas_dir = repo_root() / "schemas"
|
||||
schema_path = schemas_dir / "state-hub-export.schema.yaml"
|
||||
store = {
|
||||
path.resolve().as_uri(): load_yaml(path)
|
||||
for path in sorted(schemas_dir.glob("*.schema.yaml"))
|
||||
}
|
||||
schema = load_yaml(schema_path)
|
||||
resolver = jsonschema.RefResolver(
|
||||
base_uri=schema_path.resolve().as_uri(),
|
||||
referrer=schema,
|
||||
store=store,
|
||||
)
|
||||
validator = jsonschema.Draft202012Validator(schema, resolver=resolver)
|
||||
schema_path = repo_root() / "schemas" / "state-hub-export.schema.yaml"
|
||||
validator = draft202012_validator(schema_path)
|
||||
errors = sorted(validator.iter_errors(graph), key=lambda error: list(error.path))
|
||||
if errors:
|
||||
error = errors[0]
|
||||
|
||||
Reference in New Issue
Block a user