Skip to content

Commit

Permalink
Added additional galaxy[tags] checks (#4422)
Browse files Browse the repository at this point in the history
Co-authored-by: Sorin Sbarnea <[email protected]>
  • Loading branch information
minsis and ssbarnea authored Dec 11, 2024
1 parent 6fbb12e commit a376649
Show file tree
Hide file tree
Showing 19 changed files with 149 additions and 4 deletions.
33 changes: 33 additions & 0 deletions examples/galaxy_tags/galaxy_count_tags/galaxy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
namespace: bar
name: foo
version: "1.0.0" # <-- that version is not valid, should be 1.0.0 or greater
authors:
- John
readme: ../README.md
description: "..."
license:
- Apache-2.0
repository: https://github.com/ansible-collections/community.REPO_NAME
tags:
- application
- tag2
- tag3
- tag4
- tag5
- tag6
- tag7
- tag8
- tag9
- tag10
- tag11
- tag12
- tag13
- tag14
- tag15
- tag16
- tag17
- tag18
- tag19
- tag20
- tag21
14 changes: 14 additions & 0 deletions examples/galaxy_tags/galaxy_invalid_format_tags/galaxy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
namespace: bar
name: foo
version: "1.0.0"
authors:
- John
readme: ../README.md
description: "..."
license:
- Apache-2.0
repository: https://github.com/ansible-collections/community.REPO_NAME
tags:
- application
- invalid-tag-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
releases: {}
15 changes: 15 additions & 0 deletions examples/galaxy_tags/galaxy_invalid_length_tags/galaxy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
namespace: bar
name: foo
version: "1.0.0"
authors:
- John
readme: ../README.md
description: "..."
license:
- Apache-2.0
repository: https://github.com/ansible-collections/community.REPO_NAME
tags:
- application
- this_is_an_utterly_ridiculous_and_insanely_long_tag_length_eye_roll
- also_a_ridiculously_long_tag_but_within_the_limits
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
releases: {}
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ description: "..."
license:
- Apache-2.0
repository: https://github.com/ansible-collections/community.REPO_NAME
tags: [no_required_tag_here]
Empty file.
2 changes: 2 additions & 0 deletions examples/galaxy_tags/pass/changelogs/changelog.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
releases: {}
File renamed without changes.
Empty file.
4 changes: 4 additions & 0 deletions src/ansiblelint/constants.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
"""Constants used by AnsibleLint."""

import re
from enum import Enum
from pathlib import Path
from typing import Literal

TAG_NAME_REGEXP = re.compile(r"^(?!.*__)[a-z][0-9a-z_]*$")
MAX_TAGS_COUNT = 20
MAX_LENGTH_TAG = 64
DEFAULT_RULESDIR = Path(__file__).parent / "rules"
CUSTOM_RULESDIR_ENVVAR = "ANSIBLE_LINT_CUSTOM_RULESDIR"
RULE_DOC_URL = "https://ansible.readthedocs.io/projects/lint/rules/"
Expand Down
5 changes: 5 additions & 0 deletions src/ansiblelint/rules/galaxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ 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[tags-format]` - `galaxy.yal` tags must be formatted correctly
matching regex.
- `galaxy[tags-length]` - `galaxy.yml` tags character count cannot exceed 64
characters in length
- `galaxy[tags-count]` - `galaxy.yml` tag count cannot exceed 20
- `galaxy[invalid-dependency-version]` = Invalid collection metadata. Dependency
version spec range is invalid

Expand Down
73 changes: 70 additions & 3 deletions src/ansiblelint/rules/galaxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
import sys
from typing import TYPE_CHECKING, Any

from ansiblelint.constants import FILENAME_KEY, LINE_NUMBER_KEY
from ansiblelint.constants import (
FILENAME_KEY,
LINE_NUMBER_KEY,
MAX_LENGTH_TAG,
MAX_TAGS_COUNT,
TAG_NAME_REGEXP,
)
from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
Expand All @@ -23,6 +29,9 @@ class GalaxyRule(AnsibleLintRule):
version_changed = "6.11.0"
_ids = {
"galaxy[tags]": "galaxy.yaml must have one of the required tags",
"galaxy[tags-format]": "galaxy.yaml one or more tags are not formatted properly.",
"galaxy[tags-length]": "galaxy.yaml one or more tags exceed character length.",
"galaxy[tags-count]": "galaxy.yaml has too many tags.",
"galaxy[no-changelog]": "No changelog found. Please add a changelog file. Refer to the galaxy.md file for more info.",
"galaxy[version-missing]": "galaxy.yaml should have version tag.",
"galaxy[no-runtime]": "meta/runtime.yml file not found.",
Expand Down Expand Up @@ -65,6 +74,13 @@ def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]:
if path.is_file():
changelog_found = 1
galaxy_tag_list = data.get("tags")
galaxy_tag_invalid_format = [
tag for tag in galaxy_tag_list if not TAG_NAME_REGEXP.match(tag)
]
galaxy_tag_invalid_length = [
tag for tag in galaxy_tag_list if len(tag) > MAX_LENGTH_TAG
]

collection_deps = data.get("dependencies")
if collection_deps:
for dep, ver in collection_deps.items():
Expand Down Expand Up @@ -105,6 +121,42 @@ def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]:
),
)

# Checking if galaxy.yml tags are formatted correctly
if galaxy_tag_invalid_format:
results.append(
self.create_matcherror(
message=(
f"galaxy.yaml must have properly formatted tags. Invalid tags: {','.join(galaxy_tag_invalid_format)}"
),
tag="galaxy[tags-format]",
filename=file,
),
)

# Checking if galaxy.yml tags length are within limits
if galaxy_tag_invalid_length:
results.append(
self.create_matcherror(
message=(
f"galaxy.yaml tags must not exceed {MAX_LENGTH_TAG} characters. Invalid tags: {','.join(galaxy_tag_invalid_length)}"
),
tag="galaxy[tags-length]",
filename=file,
),
)

# Checking if galaxy.yml tags does not exceed the max number
if len(galaxy_tag_list) > MAX_TAGS_COUNT:
results.append(
self.create_matcherror(
message=(
f"galaxy.yaml exceeds {MAX_TAGS_COUNT} tags. Current count: {len(galaxy_tag_list)}"
),
tag="galaxy[tags-count]",
filename=file,
),
)

if "version" not in data:
results.append(
self.create_matcherror(
Expand Down Expand Up @@ -149,12 +201,27 @@ def test_galaxy_no_collection_version() -> None:
("file", "expected"),
(
pytest.param(
"examples/galaxy_no_required_tags/fail/galaxy.yml",
"examples/galaxy_tags/galaxy_invalid_format_tags/galaxy.yml",
["galaxy[tags-format]"],
id="tags-format",
),
pytest.param(
"examples/galaxy_tags/galaxy_invalid_length_tags/galaxy.yml",
["galaxy[tags-length]"],
id="tags-length",
),
pytest.param(
"examples/galaxy_tags/galaxy_count_tags/galaxy.yml",
["galaxy[tags-count]"],
id="tags-count",
),
pytest.param(
"examples/galaxy_tags/galaxy_no_required_tags/galaxy.yml",
["galaxy[tags]"],
id="tags",
),
pytest.param(
"examples/galaxy_no_required_tags/pass/galaxy.yml",
"examples/galaxy_tags/pass/galaxy.yml",
[],
id="pass",
),
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ setenv =
PRE_COMMIT_COLOR = always
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests. (tox-extra)
PYTEST_REQPASS = 901
PYTEST_REQPASS = 904
FORCE_COLOR = 1
pre: PIP_PRE = 1
allowlist_externals =
Expand Down

0 comments on commit a376649

Please sign in to comment.