From d6f63f0d220b0c2a195234e42627a611367afeec Mon Sep 17 00:00:00 2001 From: gardar Date: Sun, 26 May 2024 14:37:16 +0000 Subject: [PATCH] fix: make yaml output pass `ansible-lint` (#160) * fix: add yaml boundary marker to header Signed-off-by: gardar * fix: Increase indent of list items in yaml output Signed-off-by: gardar * fix: make nice yaml formatter optional Signed-off-by: gardar * fix: make nice yaml argument optional Signed-off-by: gardar * fix: lint Signed-off-by: gardar * fix: typos Signed-off-by: gardar * Apply suggestions from code review Co-authored-by: Felix Fontein * fix: alphabetical order Signed-off-by: gardar --------- Signed-off-by: gardar Co-authored-by: Felix Fontein --- changelogs/config.yaml | 1 + .../fragments/160-changelog-nice-yaml.yml | 9 ++++++++ docs/changelog-configuration.md | 11 ++++++++++ src/antsibull_changelog/changes.py | 2 +- src/antsibull_changelog/config.py | 5 +++++ src/antsibull_changelog/yaml.py | 21 +++++++++++++++---- 6 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/160-changelog-nice-yaml.yml diff --git a/changelogs/config.yaml b/changelogs/config.yaml index 391d6d26..e854426e 100644 --- a/changelogs/config.yaml +++ b/changelogs/config.yaml @@ -37,3 +37,4 @@ output_formats: - rst - md add_plugin_period: false +changelog_nice_yaml: false \ No newline at end of file diff --git a/changelogs/fragments/160-changelog-nice-yaml.yml b/changelogs/fragments/160-changelog-nice-yaml.yml new file mode 100644 index 00000000..87bc2020 --- /dev/null +++ b/changelogs/fragments/160-changelog-nice-yaml.yml @@ -0,0 +1,9 @@ +--- +minor_changes: + - | + There is now an option ``changelog_nice_yaml`` to prepend the YAML document start + marker ``---`` to the header of the ``changelogs/changelog.yaml`` file, and to increases + indentation level on list items. This makes the file pass ansible-lint + (https://github.com/ansible-community/antsibull-changelog/issues/91, + https://github.com/ansible-community/antsibull-changelog/issues/152, + https://github.com/ansible-community/antsibull-changelog/pull/160). diff --git a/docs/changelog-configuration.md b/docs/changelog-configuration.md index 02a86300..0911f37d 100644 --- a/docs/changelog-configuration.md +++ b/docs/changelog-configuration.md @@ -92,6 +92,17 @@ except when using the changelog generator for Ansible 2.9 or earlier. Note that support for `classic` is **DEPRECATED** and will be removed in a future release. The field will from then on be required. +### `changelog_nice_yaml` (boolean) + +The default is `false`. + +When set to `true`, the `changelogs/changelog.yaml` file will be written with a slightly different +YAML encoding that is compatible with ansible-lint's default rules. + +!!! note + The exact format used might be adjusted in the future if new releases of ansible-lint + adjust their yamllint configuration. + ### `flatmap` (optional boolean) The default value is `null`. diff --git a/src/antsibull_changelog/changes.py b/src/antsibull_changelog/changes.py index 9e6617d5..cbb2dc5f 100644 --- a/src/antsibull_changelog/changes.py +++ b/src/antsibull_changelog/changes.py @@ -156,7 +156,7 @@ def save(self) -> None: """ self.sort() self.data["ancestor"] = self.ancestor - store_yaml(self.path, self.data) + store_yaml(self.path, self.data, self.config.changelog_nice_yaml) def add_release( self, diff --git a/src/antsibull_changelog/config.py b/src/antsibull_changelog/config.py index 81908c63..bf018bd6 100644 --- a/src/antsibull_changelog/config.py +++ b/src/antsibull_changelog/config.py @@ -379,6 +379,7 @@ class ChangelogConfig: sections: Mapping[str, str] output_formats: set[TextFormat] add_plugin_period: bool + changelog_nice_yaml: bool def __init__( self, @@ -468,6 +469,8 @@ def __init__( self.add_plugin_period = self.config.get("add_plugin_period", False) + self.changelog_nice_yaml = self.config.get("changelog_nice_yaml", False) + self._validate_config(ignore_is_other_project) def _validate_config(self, ignore_is_other_project: bool) -> None: @@ -518,6 +521,7 @@ def store(self) -> None: # noqa: C901 "ignore_other_fragment_extensions": self.ignore_other_fragment_extensions, "sanitize_changelog": self.sanitize_changelog, "add_plugin_period": self.add_plugin_period, + "changelog_nice_yaml": self.changelog_nice_yaml, } if not self.is_collection: if self.use_semantic_versioning: @@ -597,6 +601,7 @@ def default( "ignore_other_fragment_extensions": True, "sanitize_changelog": True, "add_plugin_period": True, + "changelog_nice_yaml": False, } if title is not None: config["title"] = title diff --git a/src/antsibull_changelog/yaml.py b/src/antsibull_changelog/yaml.py index 4a2546bf..ef643aae 100644 --- a/src/antsibull_changelog/yaml.py +++ b/src/antsibull_changelog/yaml.py @@ -25,6 +25,15 @@ from yaml import SafeLoader as _SafeLoader +class _IndentedDumper(yaml.SafeDumper): + """ + Extend YAML dumper to increase indent of list items. + """ + + def increase_indent(self, flow=False, indentless=False): + return super().increase_indent(flow, False) + + def load_yaml(path: str) -> Any: """ Load and parse YAML file ``path``. @@ -33,11 +42,15 @@ def load_yaml(path: str) -> Any: return yaml.load(stream, Loader=_SafeLoader) -def store_yaml(path: str, content: Any) -> None: +def store_yaml(path: str, content: Any, nice: bool = False) -> None: """ Store ``content`` as YAML file under ``path``. """ with open(path, "w", encoding="utf-8") as stream: - dumper = _SafeDumper - dumper.ignore_aliases = lambda *args: True - yaml.dump(content, stream, default_flow_style=False, Dumper=dumper) + yaml.dump( + content, + stream, + default_flow_style=False, + Dumper=_IndentedDumper if nice else _SafeDumper, + explicit_start=nice, + )