From e37076db9cd8d48ffababd6e8a82ad1329be77cc Mon Sep 17 00:00:00 2001 From: KB-perByte Date: Thu, 26 Oct 2023 18:09:35 +0530 Subject: [PATCH 1/4] rule to check changelog vs galaxy version --- docs/rules/index.md | 1 + src/ansiblelint/data/profiles.yml | 1 + .../rules/changelog_galaxy_version.md | 36 ++++++ .../rules/changelog_galaxy_version.py | 111 ++++++++++++++++++ 4 files changed, 149 insertions(+) create mode 100644 src/ansiblelint/rules/changelog_galaxy_version.md create mode 100644 src/ansiblelint/rules/changelog_galaxy_version.py diff --git a/docs/rules/index.md b/docs/rules/index.md index 43ac967e10..aa667cfdd0 100644 --- a/docs/rules/index.md +++ b/docs/rules/index.md @@ -2,6 +2,7 @@ - [args][] - [avoid-implicit][] +- [changelog-galaxy-version][] - [complexity][] - [command-instead-of-module][] - [command-instead-of-shell][] diff --git a/src/ansiblelint/data/profiles.yml b/src/ansiblelint/data/profiles.yml index 0749ad5030..61331fbd79 100644 --- a/src/ansiblelint/data/profiles.yml +++ b/src/ansiblelint/data/profiles.yml @@ -58,6 +58,7 @@ safety: extends: moderate rules: avoid-implicit: + changelog-galaxy-version: latest: package-latest: risky-file-permissions: diff --git a/src/ansiblelint/rules/changelog_galaxy_version.md b/src/ansiblelint/rules/changelog_galaxy_version.md new file mode 100644 index 0000000000..c3f051bdb5 --- /dev/null +++ b/src/ansiblelint/rules/changelog_galaxy_version.md @@ -0,0 +1,36 @@ +# meta-video-links + +This rule checks formatting for video links in metadata. Always use dictionaries +for items in the `meta/main.yml` file. + +Items in the `video_links` section must be in a dictionary and use the following +keys: + +- `url` +- `title` + +The value of the `url` key must be a shared link from YouTube, Vimeo, or Google +Drive. + +## Problematic Code + +```yaml +--- +galaxy_info: + video_links: + - https://www.youtube.com/watch?v=aWmRepTSFKs&feature=youtu.be # <- Does not use the url key. + - my_bad_key: https://www.youtube.com/watch?v=aWmRepTSFKs&feature=youtu.be # <- Uses an unsupported key. + title: Incorrect key. + - url: www.acme.com/vid # <- Uses an unsupported url format. + title: Incorrect url format. +``` + +## Correct Code + +```yaml +--- +galaxy_info: + video_links: + - url: https://www.youtube.com/watch?v=aWmRepTSFKs&feature=youtu.be # <- Uses a supported shared link with the url key. + title: Correctly formatted video link. +``` diff --git a/src/ansiblelint/rules/changelog_galaxy_version.py b/src/ansiblelint/rules/changelog_galaxy_version.py new file mode 100644 index 0000000000..0082037895 --- /dev/null +++ b/src/ansiblelint/rules/changelog_galaxy_version.py @@ -0,0 +1,111 @@ +"""Implementation of meta-runtime rule.""" +from __future__ import annotations + +import sys +from typing import TYPE_CHECKING + +from galaxy import Version + +from ansiblelint.rules import AnsibleLintRule + +# Copyright (c) 2023, Ansible Project + + +if TYPE_CHECKING: + from ansiblelint.errors import MatchError + from ansiblelint.file_utils import Lintable + + +class ChangelogGalaxyVersion(AnsibleLintRule): + """Required latest changlog version should be in sync with the version in galaxy.yml""" + + id = "changelog-galaxy-version" + description = ( + "The latest changlog version should be in sync with the version in galaxy.yml" + ) + severity = "VERY_HIGH" + tags = ["metadata"] + version_added = "v6.11.0 (last update)" + + _ids = { + "changelog-galaxy-version[update-version]": "latest changelog version should be in sync with version in galaxy.yml", + } + + def matchyaml(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]: + """Find violations inside meta files. + + :param file: Input lintable file that is a match for `meta-runtime` + :returns: List of errors matched to the input file + """ + results = [] + + if file.kind != "changelog": # type: ignore[comparison-overlap] + return [] + + release_data = file.data.get("releases", None) + version_required = data.get("releases", None) + + version = data.get("version") + if Version(version) < Version("1.0.0"): + results.append( + self.create_matcherror( + message="collection version should be greater than or equal to 1.0.0", + lineno=version._line_number, # noqa: SLF001 + tag="galaxy[version-incorrect]", + filename=file, + ), + ) + + return results + + +# testing code to be loaded only with pytest or when executed the rule file +if "pytest" in sys.modules: + import pytest + + # pylint: disable=ungrouped-imports + from ansiblelint.rules import RulesCollection + from ansiblelint.runner import Runner + + @pytest.mark.parametrize( + ("test_file", "failures", "tags"), + ( + pytest.param( + "examples/meta_runtime_version_checks/pass/meta/runtime.yml", + 0, + "meta-runtime[unsupported-version]", + id="pass", + ), + pytest.param( + "examples/meta_runtime_version_checks/fail_0/meta/runtime.yml", + 1, + "meta-runtime[unsupported-version]", + id="fail0", + ), + pytest.param( + "examples/meta_runtime_version_checks/fail_1/meta/runtime.yml", + 1, + "meta-runtime[unsupported-version]", + id="fail1", + ), + pytest.param( + "examples/meta_runtime_version_checks/fail_2/meta/runtime.yml", + 1, + "meta-runtime[invalid-version]", + id="fail2", + ), + ), + ) + def test_meta_supported_version( + default_rules_collection: RulesCollection, + test_file: str, + failures: int, + tags: str, + ) -> None: + """Test rule matches.""" + default_rules_collection.register(ChangelogGalaxyVersion()) + results = Runner(test_file, rules=default_rules_collection).run() + for result in results: + assert result.rule.id == ChangelogGalaxyVersion().id + assert result.tag == tags + assert len(results) == failures From ececdc98215c25a1758f386843be016281b26236 Mon Sep 17 00:00:00 2001 From: KB-perByte Date: Thu, 25 Jan 2024 22:16:59 +0530 Subject: [PATCH 2/4] add new rule change --- docs/rules/index.md | 1 - .../rules/changelog_galaxy_version.md | 36 ------ .../rules/changelog_galaxy_version.py | 111 ------------------ src/ansiblelint/rules/galaxy.py | 21 ++++ 4 files changed, 21 insertions(+), 148 deletions(-) delete mode 100644 src/ansiblelint/rules/changelog_galaxy_version.md delete mode 100644 src/ansiblelint/rules/changelog_galaxy_version.py diff --git a/docs/rules/index.md b/docs/rules/index.md index aa667cfdd0..43ac967e10 100644 --- a/docs/rules/index.md +++ b/docs/rules/index.md @@ -2,7 +2,6 @@ - [args][] - [avoid-implicit][] -- [changelog-galaxy-version][] - [complexity][] - [command-instead-of-module][] - [command-instead-of-shell][] diff --git a/src/ansiblelint/rules/changelog_galaxy_version.md b/src/ansiblelint/rules/changelog_galaxy_version.md deleted file mode 100644 index c3f051bdb5..0000000000 --- a/src/ansiblelint/rules/changelog_galaxy_version.md +++ /dev/null @@ -1,36 +0,0 @@ -# meta-video-links - -This rule checks formatting for video links in metadata. Always use dictionaries -for items in the `meta/main.yml` file. - -Items in the `video_links` section must be in a dictionary and use the following -keys: - -- `url` -- `title` - -The value of the `url` key must be a shared link from YouTube, Vimeo, or Google -Drive. - -## Problematic Code - -```yaml ---- -galaxy_info: - video_links: - - https://www.youtube.com/watch?v=aWmRepTSFKs&feature=youtu.be # <- Does not use the url key. - - my_bad_key: https://www.youtube.com/watch?v=aWmRepTSFKs&feature=youtu.be # <- Uses an unsupported key. - title: Incorrect key. - - url: www.acme.com/vid # <- Uses an unsupported url format. - title: Incorrect url format. -``` - -## Correct Code - -```yaml ---- -galaxy_info: - video_links: - - url: https://www.youtube.com/watch?v=aWmRepTSFKs&feature=youtu.be # <- Uses a supported shared link with the url key. - title: Correctly formatted video link. -``` diff --git a/src/ansiblelint/rules/changelog_galaxy_version.py b/src/ansiblelint/rules/changelog_galaxy_version.py deleted file mode 100644 index 0082037895..0000000000 --- a/src/ansiblelint/rules/changelog_galaxy_version.py +++ /dev/null @@ -1,111 +0,0 @@ -"""Implementation of meta-runtime rule.""" -from __future__ import annotations - -import sys -from typing import TYPE_CHECKING - -from galaxy import Version - -from ansiblelint.rules import AnsibleLintRule - -# Copyright (c) 2023, Ansible Project - - -if TYPE_CHECKING: - from ansiblelint.errors import MatchError - from ansiblelint.file_utils import Lintable - - -class ChangelogGalaxyVersion(AnsibleLintRule): - """Required latest changlog version should be in sync with the version in galaxy.yml""" - - id = "changelog-galaxy-version" - description = ( - "The latest changlog version should be in sync with the version in galaxy.yml" - ) - severity = "VERY_HIGH" - tags = ["metadata"] - version_added = "v6.11.0 (last update)" - - _ids = { - "changelog-galaxy-version[update-version]": "latest changelog version should be in sync with version in galaxy.yml", - } - - def matchyaml(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]: - """Find violations inside meta files. - - :param file: Input lintable file that is a match for `meta-runtime` - :returns: List of errors matched to the input file - """ - results = [] - - if file.kind != "changelog": # type: ignore[comparison-overlap] - return [] - - release_data = file.data.get("releases", None) - version_required = data.get("releases", None) - - version = data.get("version") - if Version(version) < Version("1.0.0"): - results.append( - self.create_matcherror( - message="collection version should be greater than or equal to 1.0.0", - lineno=version._line_number, # noqa: SLF001 - tag="galaxy[version-incorrect]", - filename=file, - ), - ) - - return results - - -# testing code to be loaded only with pytest or when executed the rule file -if "pytest" in sys.modules: - import pytest - - # pylint: disable=ungrouped-imports - from ansiblelint.rules import RulesCollection - from ansiblelint.runner import Runner - - @pytest.mark.parametrize( - ("test_file", "failures", "tags"), - ( - pytest.param( - "examples/meta_runtime_version_checks/pass/meta/runtime.yml", - 0, - "meta-runtime[unsupported-version]", - id="pass", - ), - pytest.param( - "examples/meta_runtime_version_checks/fail_0/meta/runtime.yml", - 1, - "meta-runtime[unsupported-version]", - id="fail0", - ), - pytest.param( - "examples/meta_runtime_version_checks/fail_1/meta/runtime.yml", - 1, - "meta-runtime[unsupported-version]", - id="fail1", - ), - pytest.param( - "examples/meta_runtime_version_checks/fail_2/meta/runtime.yml", - 1, - "meta-runtime[invalid-version]", - id="fail2", - ), - ), - ) - def test_meta_supported_version( - default_rules_collection: RulesCollection, - test_file: str, - failures: int, - tags: str, - ) -> None: - """Test rule matches.""" - default_rules_collection.register(ChangelogGalaxyVersion()) - results = Runner(test_file, rules=default_rules_collection).run() - for result in results: - assert result.rule.id == ChangelogGalaxyVersion().id - assert result.tag == tags - assert len(results) == failures diff --git a/src/ansiblelint/rules/galaxy.py b/src/ansiblelint/rules/galaxy.py index 28f35437ae..4c7445e8f8 100644 --- a/src/ansiblelint/rules/galaxy.py +++ b/src/ansiblelint/rules/galaxy.py @@ -7,12 +7,16 @@ from typing import TYPE_CHECKING, Any from ansiblelint.constants import FILENAME_KEY, LINE_NUMBER_KEY +from ansiblelint.errors import MatchError +from ansiblelint.file_utils import Lintable from ansiblelint.rules import AnsibleLintRule if TYPE_CHECKING: from ansiblelint.errors import MatchError from ansiblelint.file_utils import Lintable +CHANGELOG_FILE = None + class GalaxyRule(AnsibleLintRule): """Rule for checking collection version is greater than 1.0.0 and checking for changelog.""" @@ -29,13 +33,21 @@ class GalaxyRule(AnsibleLintRule): "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", + "galaxy[version-no-sync-changelog-latest]": "Checks if version in galaxy.yaml is in sync with latest version in changelog yaml", } def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]: """Return matches found for a specific play (entry in playbook).""" + if file.kind == "changelog": + global CHANGELOG_FILE + CHANGELOG_FILE = list(changelog_file_data.data.get("releases", None).keys()) + if file.kind != "galaxy": # type: ignore[comparison-overlap] return [] + if CHANGELOG_FILE: + changelog_file_data = CHANGELOG_FILE + # Defined by Automation Hub Team and Partner Engineering required_tag_list = [ "application", @@ -90,6 +102,15 @@ def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]: filename=file, ), ) + else: + if Version(data.get("version")) != Version(changelog_file_data[-2]): + results.append( + self.create_matcherror( + message="Version in galaxy.yaml and the latest version in changelog should be same.", + tag="galaxy[version-no-sync-changelog-latest]", + filename=file, + ), + ) # Checking if galaxy.yml contains one or more required tags for certification if not galaxy_tag_list or not any( From 677e6852c1c621109634a48ef579374512b31c58 Mon Sep 17 00:00:00 2001 From: KB-perByte Date: Thu, 25 Jan 2024 22:27:12 +0530 Subject: [PATCH 3/4] fix lint 1 --- src/ansiblelint/rules/galaxy.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ansiblelint/rules/galaxy.py b/src/ansiblelint/rules/galaxy.py index 4c7445e8f8..f5be35fafd 100644 --- a/src/ansiblelint/rules/galaxy.py +++ b/src/ansiblelint/rules/galaxy.py @@ -7,8 +7,6 @@ from typing import TYPE_CHECKING, Any from ansiblelint.constants import FILENAME_KEY, LINE_NUMBER_KEY -from ansiblelint.errors import MatchError -from ansiblelint.file_utils import Lintable from ansiblelint.rules import AnsibleLintRule if TYPE_CHECKING: @@ -38,6 +36,7 @@ class GalaxyRule(AnsibleLintRule): def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]: """Return matches found for a specific play (entry in playbook).""" + changelog_file_data = [] if file.kind == "changelog": global CHANGELOG_FILE CHANGELOG_FILE = list(changelog_file_data.data.get("releases", None).keys()) From efcbac99c053f4c7d2a32b77821a850d184ca7aa Mon Sep 17 00:00:00 2001 From: KB-perByte Date: Fri, 26 Jan 2024 10:29:50 +0530 Subject: [PATCH 4/4] ah fix --- src/ansiblelint/rules/galaxy.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ansiblelint/rules/galaxy.py b/src/ansiblelint/rules/galaxy.py index f5be35fafd..5cf8426020 100644 --- a/src/ansiblelint/rules/galaxy.py +++ b/src/ansiblelint/rules/galaxy.py @@ -37,9 +37,9 @@ class GalaxyRule(AnsibleLintRule): def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]: """Return matches found for a specific play (entry in playbook).""" changelog_file_data = [] + global CHANGELOG_FILE if file.kind == "changelog": - global CHANGELOG_FILE - CHANGELOG_FILE = list(changelog_file_data.data.get("releases", None).keys()) + CHANGELOG_FILE = list(file.data.get("releases", None).keys()) if file.kind != "galaxy": # type: ignore[comparison-overlap] return [] @@ -102,7 +102,7 @@ def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]: ), ) else: - if Version(data.get("version")) != Version(changelog_file_data[-2]): + if Version(data.get("version")) != Version(changelog_file_data[-3]): results.append( self.create_matcherror( message="Version in galaxy.yaml and the latest version in changelog should be same.",