Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add galaxy[no-changelog] check to galaxy rule #2832

Merged
merged 12 commits into from
Jan 10, 2023
Empty file.
2 changes: 2 additions & 0 deletions examples/meta/changelogs/changelog.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
releases: {}
16 changes: 16 additions & 0 deletions examples/no_changelog/galaxy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
name: foo
namespace: bar
version: 0.0.0 # noqa: galaxy[version-incorrect]
authors:
- John
readme: ../README.md
description: "..."
dependencies:
other_namespace.collection1: ">=1.0.0"
other_namespace.collection2: ">=2.0.0,<3.0.0"
anderson55.my_collection: "*" # note: "*" selects the highest version available
license:
- GPL # <-- invalid license values based on galaxy schema
- Apache
repository: some-url
16 changes: 16 additions & 0 deletions src/ansiblelint/rules/galaxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

This rule identifies if the collection version mentioned in galaxy.yml is ideal in terms of the version number being greater than or equal to `1.0.0`.

This rule also looks for a changelog file in expected locations, detailed below in the Changelog Details section.

This rule can produce messages such:

- `galaxy[version-missing]` - `galaxy.yaml` should have version tag.
- `galaxy[version-incorrect]` - collection version should be greater than or equal to `1.0.0`
- `galaxy[no-changelog]` - collection is missing a changelog file in expected locations.

If you want to ignore some of the messages above, you can add any of them to
the `ignore_list`.
Expand Down Expand Up @@ -37,3 +40,16 @@ authors:
readme: ../README.md
description: "..."
```

# Changelog Details

This rule expects a `CHANGELOG.md` or `.rst` file in the collection root or a `changelogs/changelog.yaml` file.

If a `changelogs/changelog.yaml` file exists, the schema will be checked.

# Minimum required changelog.yaml file

```yaml
# changelog.yaml
---
releases: {}
58 changes: 51 additions & 7 deletions src/ansiblelint/rules/galaxy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Implementation of GalaxyRule."""
from __future__ import annotations

import os
import sys
from functools import total_ordering
from typing import TYPE_CHECKING, Any
Expand All @@ -14,10 +15,10 @@


class GalaxyRule(AnsibleLintRule):
"""Rule for checking collection version is greater than 1.0.0."""
"""Rule for checking collection version is greater than 1.0.0 and checking for changelog."""

id = "galaxy"
description = "Confirm via galaxy.yml file if collection version is greater than or equal to 1.0.0"
description = "Confirm via galaxy.yml file if collection version is greater than or equal to 1.0.0 and check for changelog."
severity = "MEDIUM"
tags = ["metadata", "opt-in", "experimental"]
version_added = "v6.6.0 (last update)"
Expand All @@ -26,27 +27,55 @@ def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]:
"""Return matches found for a specific play (entry in playbook)."""
if file.kind != "galaxy": # type: ignore
return []

results = []

if "version" not in data:
return [
results.append(
self.create_matcherror(
message="galaxy.yaml should have version tag.",
linenumber=data[LINE_NUMBER_KEY],
tag="galaxy[version-missing]",
filename=file,
)
]
)
version = data.get("version")
if Version(version) < Version("1.0.0"):
return [
results.append(
self.create_matcherror(
message="collection version should be greater than or equal to 1.0.0",
# pylint: disable=protected-access
linenumber=version._line_number,
tag="galaxy[version-incorrect]",
filename=file,
)
]
return []
)

# Changelog Check - building off Galaxy rule as there is no current way to check
# for a nonexistent file

base_path = os.path.split(str(file.abspath))[0]
changelog_found = 0
changelog_paths = [
os.path.join(base_path, "changelogs", "changelog.yaml"),
os.path.join(base_path, "CHANGELOG.rst"),
os.path.join(base_path, "CHANGELOG.md"),
]

for path in changelog_paths:
if os.path.isfile(path):
changelog_found = 1

if not changelog_found:
results.append(
self.create_matcherror(
message="No changelog found. Please add a changelog file. Refer to the galaxy.md file for more info.",
tag="galaxy[no-changelog]",
filename=file,
)
)

return results


@total_ordering
Expand Down Expand Up @@ -105,3 +134,18 @@ def test_galaxy_collection_version_negative() -> None:
bad_runner = Runner(failure, rules=collection)
errs = bad_runner.run()
assert len(errs) == 1

def test_changelog_present() -> None:
"""Positive test for finding a changelog."""
collection = RulesCollection()
collection.register(GalaxyRule())
good_runner = Runner("examples/collection/galaxy.yml", rules=collection)
assert [] == good_runner.run()

def test_changelog_missing() -> None:
"""Negative test for finding a changelog."""
collection = RulesCollection()
collection.register(GalaxyRule())
bad_runner = Runner("examples/no_changelog/galaxy.yml", rules=collection)
errs = bad_runner.run()
assert len(errs) == 1