feat: add rule to check standards version (#522)

This commit is contained in:
Robert Kaussow 2023-01-09 16:25:16 +01:00 committed by GitHub
parent d980dfc35d
commit 505a2aef5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 18 deletions

View File

@ -28,7 +28,6 @@ class Candidate(object):
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
@ -69,21 +68,7 @@ class Candidate(object):
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
@ -105,7 +90,8 @@ class Candidate(object):
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:

View File

@ -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)

View File

@ -120,7 +120,10 @@ class Settings(object):
"standards": [],
"filter": [],
"exclude_filter": [],
"warning_filter": ["ANSIBLE9999"],
"warning_filter": [
"ANSIBLE9999",
"ANSIBLE9998",
],
"ignore_dotfiles": True,
"exclude_files": [],
"version": ""

View File

@ -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`.

View File

@ -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. | |