Skip to content

Commit

Permalink
Change matchtask to receive a Task instance (#3403)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed May 7, 2023
1 parent 5cb5810 commit 6921d99
Show file tree
Hide file tree
Showing 43 changed files with 150 additions and 85 deletions.
3 changes: 2 additions & 1 deletion docs/custom-rules.md
Expand Up @@ -58,6 +58,7 @@ from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
from ansiblelint.file_utils import Lintable
from ansiblelint.utils import Task

class TaskHasTag(AnsibleLintRule):
"""Tasks must have tag."""
Expand All @@ -66,7 +67,7 @@ class TaskHasTag(AnsibleLintRule):
description = 'Tasks must have tag'
tags = ['productivity']

def matchtask(self, task: Dict[str, Any], file: 'Lintable' | None = None) -> Union[bool,str]:
def matchtask(self, task: Task, file: 'Lintable' | None = None) -> Union[bool,str]:
# If the task include another task or make the playbook fail
# Don't force to have a tag
if not set(task.keys()).isdisjoint(['include','fail']):
Expand Down
5 changes: 3 additions & 2 deletions examples/rules/task_has_tag.py
@@ -1,12 +1,13 @@
"""Example implementation of a rule requiring tasks to have tags set."""
from __future__ import annotations

from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING

from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
from ansiblelint.file_utils import Lintable
from ansiblelint.utils import Task


class TaskHasTag(AnsibleLintRule):
Expand All @@ -18,7 +19,7 @@ class TaskHasTag(AnsibleLintRule):

def matchtask(
self,
task: dict[str, Any],
task: Task,
file: Lintable | None = None,
) -> bool | str:
"""Task matching method."""
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/_internal/rules.py
Expand Up @@ -12,6 +12,7 @@
from ansiblelint.errors import MatchError
from ansiblelint.file_utils import Lintable
from ansiblelint.rules import RulesCollection
from ansiblelint.utils import Task

_logger = logging.getLogger(__name__)
LOAD_FAILURE_MD = """\
Expand Down Expand Up @@ -105,7 +106,7 @@ def matchlines(self, file: Lintable) -> list[MatchError]:

def matchtask(
self,
task: dict[str, Any],
task: Task,
file: Lintable | None = None,
) -> bool | str | MatchError | list[MatchError]:
"""Confirm if current rule is matching a specific task.
Expand Down
7 changes: 5 additions & 2 deletions src/ansiblelint/errors.py
Expand Up @@ -3,12 +3,15 @@

import functools
from dataclasses import dataclass, field
from typing import Any
from typing import TYPE_CHECKING, Any

from ansiblelint._internal.rules import BaseRule, RuntimeErrorRule
from ansiblelint.config import options
from ansiblelint.file_utils import Lintable

if TYPE_CHECKING:
from ansiblelint.utils import Task


class StrictModeError(RuntimeError):
"""Raise when we encounter a warning in strict mode."""
Expand Down Expand Up @@ -72,7 +75,7 @@ def __post_init__(self) -> None:

self.match_type: str | None = None
# for task matches, save the normalized task object (useful for transforms)
self.task: dict[str, Any] | None = None
self.task: Task | None = None
# path to the problem area, like: [0,"pre_tasks",3] for [0].pre_tasks[3]
self.yaml_path: list[int | str] = []

Expand Down
8 changes: 4 additions & 4 deletions src/ansiblelint/rules/__init__.py
Expand Up @@ -103,7 +103,7 @@ def create_matcherror(
@staticmethod
def _enrich_matcherror_with_task_details(
match: MatchError,
task: dict[str, Any],
task: ansiblelint.utils.Task,
) -> None:
match.task = task
if not match.details:
Expand Down Expand Up @@ -172,7 +172,7 @@ def matchtasks(self, file: Lintable) -> list[MatchError]: # noqa: C901
if self.needs_raw_task:
task.normalized_task["__raw_task__"] = task.raw_task

result = self.matchtask(task.normalized_task, file=file)
result = self.matchtask(task, file=file)
if not result:
continue

Expand All @@ -187,7 +187,7 @@ def matchtasks(self, file: Lintable) -> list[MatchError]: # noqa: C901
continue
self._enrich_matcherror_with_task_details(
match,
task.normalized_task,
task,
)
matches.append(match)
continue
Expand All @@ -205,7 +205,7 @@ def matchtasks(self, file: Lintable) -> list[MatchError]: # noqa: C901
filename=file,
)

self._enrich_matcherror_with_task_details(match, task.normalized_task)
self._enrich_matcherror_with_task_details(match, task)
matches.append(match)
return matches

Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/args.py
Expand Up @@ -28,6 +28,7 @@
if TYPE_CHECKING:
from ansiblelint.errors import MatchError
from ansiblelint.file_utils import Lintable
from ansiblelint.utils import Task


_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -96,7 +97,7 @@ class ArgsRule(AnsibleLintRule):

def matchtask( # noqa: C901
self,
task: dict[str, Any],
task: Task,
file: Lintable | None = None,
) -> list[MatchError]:
# pylint: disable=too-many-branches,too-many-locals
Expand Down
5 changes: 3 additions & 2 deletions src/ansiblelint/rules/avoid_implicit.py
Expand Up @@ -3,12 +3,13 @@
from __future__ import annotations

import sys
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING

from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
from ansiblelint.file_utils import Lintable
from ansiblelint.utils import Task


class AvoidImplicitRule(AnsibleLintRule):
Expand All @@ -26,7 +27,7 @@ class AvoidImplicitRule(AnsibleLintRule):

def matchtask(
self,
task: dict[str, Any],
task: Task,
file: Lintable | None = None,
) -> bool | str:
"""Confirm if current rule is matching a specific task."""
Expand Down
5 changes: 3 additions & 2 deletions src/ansiblelint/rules/command_instead_of_module.py
Expand Up @@ -22,13 +22,14 @@

import sys
from pathlib import Path
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING

from ansiblelint.rules import AnsibleLintRule
from ansiblelint.utils import convert_to_boolean, get_first_cmd_arg, get_second_cmd_arg

if TYPE_CHECKING:
from ansiblelint.file_utils import Lintable
from ansiblelint.utils import Task


class CommandsInsteadOfModulesRule(AnsibleLintRule):
Expand Down Expand Up @@ -75,7 +76,7 @@ class CommandsInsteadOfModulesRule(AnsibleLintRule):

def matchtask(
self,
task: dict[str, Any],
task: Task,
file: Lintable | None = None,
) -> bool | str:
if task["action"]["__ansible_module__"] not in self._commands:
Expand Down
5 changes: 3 additions & 2 deletions src/ansiblelint/rules/command_instead_of_shell.py
Expand Up @@ -21,13 +21,14 @@
from __future__ import annotations

import sys
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING

from ansiblelint.rules import AnsibleLintRule
from ansiblelint.utils import get_cmd_args

if TYPE_CHECKING:
from ansiblelint.file_utils import Lintable
from ansiblelint.utils import Task


class UseCommandInsteadOfShellRule(AnsibleLintRule):
Expand All @@ -45,7 +46,7 @@ class UseCommandInsteadOfShellRule(AnsibleLintRule):

def matchtask(
self,
task: dict[str, Any],
task: Task,
file: Lintable | None = None,
) -> bool | str:
# Use unjinja so that we don't match on jinja filters
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/deprecated_bare_vars.py
Expand Up @@ -31,6 +31,7 @@

if TYPE_CHECKING:
from ansiblelint.file_utils import Lintable
from ansiblelint.utils import Task


class UsingBareVariablesIsDeprecatedRule(AnsibleLintRule):
Expand All @@ -48,7 +49,7 @@ class UsingBareVariablesIsDeprecatedRule(AnsibleLintRule):

def matchtask(
self,
task: dict[str, Any],
task: Task,
file: Lintable | None = None,
) -> bool | str:
loop_type = next((key for key in task if key.startswith("with_")), None)
Expand Down
5 changes: 3 additions & 2 deletions src/ansiblelint/rules/deprecated_local_action.py
Expand Up @@ -4,12 +4,13 @@
from __future__ import annotations

import sys
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING

from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
from ansiblelint.file_utils import Lintable
from ansiblelint.utils import Task


class TaskNoLocalAction(AnsibleLintRule):
Expand All @@ -24,7 +25,7 @@ class TaskNoLocalAction(AnsibleLintRule):

def matchtask(
self,
task: dict[str, Any],
task: Task,
file: Lintable | None = None,
) -> bool | str:
"""Return matches for a task."""
Expand Down
5 changes: 3 additions & 2 deletions src/ansiblelint/rules/empty_string_compare.py
Expand Up @@ -6,13 +6,14 @@

import re
import sys
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING

from ansiblelint.rules import AnsibleLintRule
from ansiblelint.yaml_utils import nested_items_path

if TYPE_CHECKING:
from ansiblelint.file_utils import Lintable
from ansiblelint.utils import Task


class ComparisonToEmptyStringRule(AnsibleLintRule):
Expand All @@ -31,7 +32,7 @@ class ComparisonToEmptyStringRule(AnsibleLintRule):

def matchtask(
self,
task: dict[str, Any],
task: Task,
file: Lintable | None = None,
) -> bool | str:
for k, v, _ in nested_items_path(task):
Expand Down
4 changes: 3 additions & 1 deletion src/ansiblelint/rules/fqcn.py
Expand Up @@ -15,6 +15,8 @@

from ansiblelint.errors import MatchError
from ansiblelint.file_utils import Lintable
from ansiblelint.utils import Task


_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -104,7 +106,7 @@ class FQCNBuiltinsRule(AnsibleLintRule, TransformMixin):

def matchtask(
self,
task: dict[str, Any],
task: Task,
file: Lintable | None = None,
) -> list[MatchError]:
result = []
Expand Down
5 changes: 3 additions & 2 deletions src/ansiblelint/rules/ignore_errors.py
Expand Up @@ -2,12 +2,13 @@
from __future__ import annotations

import sys
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING

from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
from ansiblelint.file_utils import Lintable
from ansiblelint.utils import Task


class IgnoreErrorsRule(AnsibleLintRule):
Expand All @@ -26,7 +27,7 @@ class IgnoreErrorsRule(AnsibleLintRule):

def matchtask(
self,
task: dict[str, Any],
task: Task,
file: Lintable | None = None,
) -> bool | str:
if (
Expand Down
6 changes: 3 additions & 3 deletions src/ansiblelint/rules/inline_env_var.py
Expand Up @@ -20,11 +20,11 @@
# THE SOFTWARE.
from __future__ import annotations

from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING

from ansiblelint.constants import FILENAME_KEY, LINE_NUMBER_KEY
from ansiblelint.rules import AnsibleLintRule
from ansiblelint.utils import get_first_cmd_arg
from ansiblelint.utils import Task, get_first_cmd_arg

if TYPE_CHECKING:
from ansiblelint.file_utils import Lintable
Expand Down Expand Up @@ -61,7 +61,7 @@ class EnvVarsInCommandRule(AnsibleLintRule):

def matchtask(
self,
task: dict[str, Any],
task: Task,
file: Lintable | None = None,
) -> bool | str:
if task["action"]["__ansible_module__"] in ["command"]:
Expand Down
3 changes: 2 additions & 1 deletion src/ansiblelint/rules/jinja.py
Expand Up @@ -24,6 +24,7 @@

if TYPE_CHECKING:
from ansiblelint.errors import MatchError
from ansiblelint.utils import Task


_logger = logging.getLogger(__package__)
Expand Down Expand Up @@ -77,7 +78,7 @@ def _msg(self, tag: str, value: str, reformatted: str) -> str:
# pylint: disable=too-many-branches,too-many-locals
def matchtask( # noqa: C901
self,
task: dict[str, Any],
task: Task,
file: Lintable | None = None,
) -> list[MatchError]:
result = []
Expand Down
5 changes: 3 additions & 2 deletions src/ansiblelint/rules/key_order.py
Expand Up @@ -3,13 +3,14 @@

import functools
import sys
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING

from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
from ansiblelint.errors import MatchError
from ansiblelint.file_utils import Lintable
from ansiblelint.utils import Task


SORTER_TASKS = (
Expand Down Expand Up @@ -57,7 +58,7 @@ class KeyOrderRule(AnsibleLintRule):

def matchtask(
self,
task: dict[str, Any],
task: Task,
file: Lintable | None = None,
) -> list[MatchError]:
result = []
Expand Down

0 comments on commit 6921d99

Please sign in to comment.