Skip to content

Commit

Permalink
feat: add rule to check standards version (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoxys committed Jan 9, 2023
1 parent d980dfc commit 505a2ae
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
20 changes: 3 additions & 17 deletions ansiblelater/candidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def __init__(self, filename, settings={}, standards=[]):
self.binary = False
self.vault = False
self.filetype = type(self).__name__.lower()
self.expected_version = True
self.faulty = False
self.config = settings.config
self.settings = settings
Expand Down Expand Up @@ -69,21 +68,7 @@ def _get_version(self):
if match:
version = match.group(1)

if not version:
version = utils.standards_latest(self.standards)
if self.expected_version:
if isinstance(self, RoleFile):
LOG.warning(
f"{name} {path} is in a role that contains a "
"meta/main.yml without a declared standards version. "
f"Using latest standards version {version}"
)
else:
LOG.warning(
f"{name} {path} does not present standards version. "
f"Using latest standards version {version}"
)
else:
if version:
LOG.info(f"{name} {path} declares standards version {version}")

return version
Expand All @@ -105,7 +90,8 @@ def _filter_standards(self):
def review(self, lines=None):
errors = 0
self.standards = SingleStandards(self.config["rules"]["standards"]).rules
self.version = self._get_version()
self.version_config = self._get_version()
self.version = self.version_config or utils.standards_latest(self.standards)

for standard in self._filter_standards():
if type(self).__name__.lower() not in standard.types:
Expand Down
17 changes: 17 additions & 0 deletions ansiblelater/rules/CheckVersion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from ansiblelater.standard import StandardBase


class CheckVersion(StandardBase):

sid = "ANSIBLE9998"
description = "Standards version should be pinned"
helptext = "Standards version not set. Using latest standards version {version}"
types = ["playbook", "task", "handler"]

def check(self, candidate, settings):
errors = []

if not candidate.version_config:
errors.append(self.Error(None, self.helptext.format(version=candidate.version)))

return self.Result(candidate.path, errors)
5 changes: 4 additions & 1 deletion ansiblelater/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ def _get_defaults(self):
"standards": [],
"filter": [],
"exclude_filter": [],
"warning_filter": ["ANSIBLE9999"],
"warning_filter": [
"ANSIBLE9999",
"ANSIBLE9998",
],
"ignore_dotfiles": True,
"exclude_files": [],
"version": ""
Expand Down
1 change: 1 addition & 0 deletions docs/content/configuration/defaults.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ rules:
# This list allows to degrade errors to warnings for each rule.
warning_filter:
- "ANSIBLE9999"
- "ANSIBLE9998"

# All dotfiles (including hidden folders) are excluded by default.
# You can disable this setting and handle dotfiles by yourself with `exclude_files`.
Expand Down
1 change: 1 addition & 0 deletions docs/content/included_rules/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ Reviews are useless without some rules or standards to check against. ansible-la
| CheckLocalAction | ANSIBLE0024 | Don't use local_action. | |
| CheckRelativeRolePaths | ANSIBLE0025 | Don't use a relative path in a role. | |
| CheckChangedInWhen | ANSIBLE0026 | Use handlers instead of `when: changed`. | |
| CheckVersion | ANSIBLE9998 | Standards version should be pinned. | |
| CheckDeprecated | ANSIBLE9999 | Deprecated features of `ansible-later` should not be used. | |

0 comments on commit 505a2ae

Please sign in to comment.