fix(ci): Refactor YAML dumping of annotations for improved clarity and structure

This commit is contained in:
Skyler Mäntysaari 2025-03-29 12:36:41 +02:00
parent 3c8da7e8ca
commit 800070646f

View File

@ -9,7 +9,7 @@ from pathlib import Path
from ruamel.yaml import YAML from ruamel.yaml import YAML
from ruamel.yaml.comments import CommentedMap from ruamel.yaml.comments import CommentedMap
from ruamel.yaml.scalarstring import LiteralScalarString, PreservedScalarString from ruamel.yaml.scalarstring import PreservedScalarString
from typing import List from typing import List
app = typer.Typer(add_completion=False) app = typer.Typer(add_completion=False)
@ -190,17 +190,27 @@ def main(
}) })
if annotations: if annotations:
annotations_stream = io.StringIO() annotations_data = [
yaml.exclude_start = False {"kind": annotation["kind"], "description": annotation["description"]}
yaml.dump_all([annotations], annotations_stream) for annotation in annotations
]
with io.StringIO() as annotations_stream:
yaml.explicit_start = False # You likely want this to be false so that the output doesn't have '---'
for annotation in annotations_data:
annotations_stream.write(
f"- kind: {annotation['kind']}\n"
f" description: {annotation['description']}\n"
)
# Get the final YAML string and strip leading/trailing whitespace
annotations_string = annotations_stream.getvalue().strip() annotations_string = annotations_stream.getvalue().strip()
annotations_stream.close()
if "annotations" not in new_chart_metadata: if "annotations" not in new_chart_metadata:
new_chart_metadata["annotations"] = CommentedMap() new_chart_metadata["annotations"] = CommentedMap()
new_chart_metadata["annotations"]["artifacthub.io/changes"] = LiteralScalarString(annotations_string) new_chart_metadata["annotations"]["artifacthub.io/changes"] = PreservedScalarString(annotations_string)
logger.debug(f"Annotations: {annotations_string}") logger.debug(f"Annotations: {annotations_string}")
logger.debug(new_chart_metadata) logger.debug(new_chart_metadata)
@ -208,7 +218,7 @@ def main(
new_chart_metadata = bump_patch_version(new_chart_metadata) new_chart_metadata = bump_patch_version(new_chart_metadata)
with chart_metadata_file.open("w") as f: with chart_metadata_file.open("w") as f:
yaml.exclude_start = False yaml.explicit_start = True
yaml.dump(new_chart_metadata, f) yaml.dump(new_chart_metadata, f)