Skip to content

Commit

Permalink
fix: make yaml output pass ansible-lint (#160)
Browse files Browse the repository at this point in the history
* fix: add yaml boundary marker to header

Signed-off-by: gardar <[email protected]>

* fix: Increase indent of list items in yaml output

Signed-off-by: gardar <[email protected]>

* fix: make nice yaml formatter optional

Signed-off-by: gardar <[email protected]>

* fix: make nice yaml argument optional

Signed-off-by: gardar <[email protected]>

* fix: lint

Signed-off-by: gardar <[email protected]>

* fix: typos

Signed-off-by: gardar <[email protected]>

* Apply suggestions from code review

Co-authored-by: Felix Fontein <[email protected]>

* fix: alphabetical order

Signed-off-by: gardar <[email protected]>

---------

Signed-off-by: gardar <[email protected]>
Co-authored-by: Felix Fontein <[email protected]>
  • Loading branch information
gardar and felixfontein authored May 26, 2024
1 parent ff8f587 commit d6f63f0
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelogs/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ output_formats:
- rst
- md
add_plugin_period: false
changelog_nice_yaml: false
9 changes: 9 additions & 0 deletions changelogs/fragments/160-changelog-nice-yaml.yml
Original file line number Diff line number Diff line change
@@ -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).
11 changes: 11 additions & 0 deletions docs/changelog-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion src/antsibull_changelog/changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions src/antsibull_changelog/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
21 changes: 17 additions & 4 deletions src/antsibull_changelog/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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``.
Expand All @@ -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,
)

0 comments on commit d6f63f0

Please sign in to comment.