Skip to content

Commit

Permalink
ENH: Add '--release' flag to 'update-schema'
Browse files Browse the repository at this point in the history
This will cause the schemas to overwrite with an updated $id field, but
only if nothing else has changed. It is also tested in GitHub Actions.
  • Loading branch information
mferrera committed Jan 8, 2025
1 parent 5df7391 commit d6b034d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/schemas-up-to-date.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Schema up to date

on:
pull_request:
branches: [main]
branches: [main, staging]
schedule:
- cron: "0 0 * * *"

Expand All @@ -22,10 +22,13 @@ jobs:
pip install pip -U
pip install -e .
- name: Set SCHEMA_RELEASE if on staging
if: github.head_ref == 'staging'
run: echo "SCHEMA_RELEASE=1" >> $GITHUB_ENV

- name: Check schema
run: |
./tools/update-schema
git diff --exit-code
./tools/update-schema --diff
- name: Ensure schema validates with AJV
run: |
Expand Down
61 changes: 54 additions & 7 deletions tools/update-schema
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ from __future__ import annotations
import argparse
import difflib
import json
import os
import subprocess
import sys
from copy import deepcopy
from pathlib import Path
from typing import Any

Expand All @@ -32,6 +34,15 @@ SCHEMAS = [
def _get_parser() -> argparse.ArgumentParser:
"""Construct parser object."""
parser = argparse.ArgumentParser()
parser.add_argument(
"--release",
"-r",
action="store_true",
help=(
"Creates a release (production) version of the schema, primarily by "
"changing '$id' urls to the production url."
),
)
parser.add_argument(
"--diff",
"-d",
Expand Down Expand Up @@ -106,34 +117,61 @@ def _show_py_diff(existing_schema: dict[str, Any], new_schema: dict[str, Any]) -
fromfile="existing schema",
tofile="new schema",
)
print("\n".join(diff))
diff_str = "\n ".join(diff)
print(f" {diff_str}") # To indent the first line too


def _schemas_are_the_same(
existing_schema: dict[str, Any],
new_schema: dict[str, Any],
is_release: bool,
release_url: str,
) -> bool:
if is_release:
existing_schema = deepcopy(existing_schema)
existing_schema["$id"] = release_url
return existing_schema == new_schema


def write_schema(
schema: SchemaBase, force_overwrite: bool, is_test: bool, show_diff: bool
) -> None:
schema: SchemaBase,
force_overwrite: bool,
is_release: bool,
is_test: bool,
show_diff: bool,
) -> bool:
output_filepath = _get_output_filepath(schema.PATH)
_check_output_path(output_filepath.parent, is_test)

new_schema = schema.dump()
existing_schema = _load_json(output_filepath)

if output_filepath.exists() and not force_overwrite:
if existing_schema != new_schema:
if output_filepath.exists():
if not force_overwrite and not _schemas_are_the_same(
existing_schema, new_schema, is_release, schema.url()
):
print(
FAIL,
f"🚨 {BOLD}{schema.FILENAME}{NC} version {BOLD}{schema.VERSION}{NC} "
"has changed. does it need a new version?",
)
if show_diff:
_show_py_diff(existing_schema, new_schema)
return False

if is_release:
print(
INFO,
f"{BOLD}{schema.FILENAME}{NC} version {BOLD}{schema.VERSION}{NC}: "
f"modifying '$id' url to 'prod':\n {schema.url()}",
)
else:
print(
PASS,
f"{BOLD}{schema.FILENAME}{NC} version "
f"{BOLD}{schema.VERSION}{NC} unchanged",
)
return
return True

if not is_test:
with open(output_filepath, "w", encoding="utf-8") as f:
Expand All @@ -147,6 +185,7 @@ def write_schema(

if show_diff:
_show_git_diff(output_filepath)
return True


def main() -> None:
Expand All @@ -156,8 +195,16 @@ def main() -> None:
if args.force:
print(INFO, "forcing overwrite of all schemas")

is_release = bool(args.release or os.environ.get("SCHEMA_RELEASE", None))

failed_a_write = False
for schema in SCHEMAS:
write_schema(schema, args.force, args.test, args.diff)
did_write = write_schema(schema, args.force, is_release, args.test, args.diff)
if not did_write:
failed_a_write = True

if failed_a_write:
sys.exit(1)


if __name__ == "__main__":
Expand Down

0 comments on commit d6b034d

Please sign in to comment.