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 dependency version check for collection metadata #3601

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:GITHUB_STEP_SUMMARY
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 807
PYTEST_REQPASS: 808
steps:
- name: Activate WSL1
if: "contains(matrix.shell, 'wsl')"
Expand Down
Empty file.
18 changes: 18 additions & 0 deletions examples/invalid_dependencies/galaxy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
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
foo.my_collection1: " " # note: this should error out because of invalid dependency version
foo.my_collection2: "" # note: this should error out because of invalid dependency version
license:
- Apache-2.0
repository: some-url
tags: [networking, test_tag]
Empty file.
2 changes: 2 additions & 0 deletions src/ansiblelint/rules/galaxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ This rule can produce messages such:
- `galaxy[tags]` - `galaxy.yaml` must have one of the required tags:
`application`, `cloud`, `database`, `infrastructure`, `linux`, `monitoring`,
`networking`, `security`, `storage`, `tools`, `windows`.
- `galaxy[invalid-dependency-version]` = Invalid collection metadata. Dependency
version spec range is invalid

If you want to ignore some of the messages above, you can add any of them to the
`ignore_list`.
Expand Down
26 changes: 24 additions & 2 deletions src/ansiblelint/rules/galaxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from functools import total_ordering
from typing import TYPE_CHECKING, Any

from ansiblelint.constants import LINE_NUMBER_KEY
from ansiblelint.constants import FILENAME_KEY, LINE_NUMBER_KEY
from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
Expand All @@ -27,6 +27,7 @@ class GalaxyRule(AnsibleLintRule):
"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-runtime]": "meta/runtime.yml file not found.",
"galaxy[invalid-dependency-version]": "Invalid collection metadata. Dependency version spec range is invalid",
}

def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]:
Expand Down Expand Up @@ -62,8 +63,21 @@ def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]:
for path in changelog_paths:
if path.is_file():
changelog_found = 1

galaxy_tag_list = data.get("tags", None)
collection_deps = data.get("dependencies", None)
if collection_deps:
for dep, ver in collection_deps.items():
if (
dep not in [LINE_NUMBER_KEY, FILENAME_KEY]
and len(str(ver).strip()) == 0
):
results.append(
self.create_matcherror(
message=f"Invalid collection metadata. Dependency version spec range is invalid for '{dep}'.",
tag="galaxy[invalid-dependency-version]",
filename=file,
),
)

# Changelog Check - building off Galaxy rule as there is no current way to check
# for a nonexistent file
Expand Down Expand Up @@ -226,6 +240,14 @@ def test_coerce() -> None:
["schema[galaxy]"],
id="schema",
),
pytest.param(
"examples/invalid_dependencies/galaxy.yml",
[
"galaxy[invalid-dependency-version]",
"galaxy[invalid-dependency-version]",
],
id="invalid-dependency-version",
),
pytest.param(
"examples/no_changelog/galaxy.yml",
["galaxy[no-changelog]"],
Expand Down