Skip to content

Commit

Permalink
fix: clean revision data of extra fields
Browse files Browse the repository at this point in the history
  • Loading branch information
clay-lake committed Oct 30, 2024
1 parent ae87140 commit beb931a
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/image/prepare_single_image_build_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from src.uploads.infer_image_track import get_base_and_track
from src.shared.github_output import GithubOutput
from src.image.utils.schema.triggers import ImageSchema
from utils.schema.revision_data import RevisionDataSchema

# TODO:
# - inject_metadata uses a static github url, does this break builds that are sourced
Expand Down Expand Up @@ -49,6 +50,21 @@
)


class RevisionDataSchemaFilter(RevisionDataSchema):
"""A subclass of RevisionDataSchema to prepare data for serialization."""

model_config = pydantic.ConfigDict(extra="ignore")

@pydantic.model_validator(mode="before")
def _warn_extra_fields(cls, data: Any) -> Any:
for extra_field in data.keys() - cls.model_fields.keys():
logging.warning(
f'Field "{extra_field}" removed from {data["name"]} revision data'
)

return data


class AmbiguousConfigFileError(Exception):
"""Raised when multiple trigger image.y*ml files are found."""

Expand Down Expand Up @@ -103,9 +119,11 @@ def filter_eol_builds(builds: list[dict[str, Any]]) -> list[dict[str, Any]]:
return [build for build in builds if len(build["release"])]


def write_build(data_dir: Path, build: dict[str, Any]):
def write_revision_data(data_dir: Path, build: dict[str, Any]):
"""Prepare and dump revision data from build dict. Ignore any fields not included in RevisionDataSchema"""
revision_data = RevisionDataSchemaFilter(**build)
with open(data_dir / str(build["revision"]), "w", encoding="UTF-8") as fh:
json.dump(build, fh)
json.dump(revision_data.model_dump(), fh)


def locate_trigger_yaml(oci_path: Path) -> Path:
Expand Down Expand Up @@ -212,7 +230,7 @@ def main():
)

for build in builds:
write_build(args.revision_data_dir, build)
write_revision_data(args.revision_data_dir, build)

# the workflow GH matrix has a problem parsing nested JSON dicts
# so let's remove this field since we don't need it for the builds
Expand Down

0 comments on commit beb931a

Please sign in to comment.