Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardize task MatchError enrichment #2277

Merged
merged 1 commit into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/ansiblelint/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ def create_matcherror(
frame = frame.f_back # get the parent frame for the next iteration
return match

@staticmethod
def _enrich_matcherror_with_task_details(
match: MatchError, task: Dict[str, Any]
) -> None:
match.task = task
if not match.details:
match.details = "Task/Handler: " + ansiblelint.utils.task_to_str(task)
if match.linenumber < task[ansiblelint.utils.LINE_NUMBER_KEY]:
match.linenumber = task[ansiblelint.utils.LINE_NUMBER_KEY]

def matchlines(self, file: "Lintable") -> List[MatchError]:
matches: List[MatchError] = []
if not self.match:
Expand Down Expand Up @@ -167,14 +177,13 @@ def matchtasks(self, file: Lintable) -> List[MatchError]:
message = None
if isinstance(result, str):
message = result
task_msg = "Task/Handler: " + ansiblelint.utils.task_to_str(task)
match = self.create_matcherror(
message=message,
linenumber=task[ansiblelint.utils.LINE_NUMBER_KEY],
details=task_msg,
filename=file,
)
match.task = task

self._enrich_matcherror_with_task_details(match, task)

matches.append(match)
return matches
Expand Down
5 changes: 3 additions & 2 deletions src/ansiblelint/rules/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from ansiblelint.errors import MatchError
from ansiblelint.rules import AnsibleLintRule
from ansiblelint.utils import LINE_NUMBER_KEY

if TYPE_CHECKING:
from typing import Optional
Expand All @@ -30,14 +31,14 @@ def matchtask(
if not name:
return self.create_matcherror(
message="All tasks should be named.",
linenumber=task["__line__"],
linenumber=task[LINE_NUMBER_KEY],
tag="name[missing]",
filename=file,
)
if not name[0].isupper():
return self.create_matcherror(
message="All names should start with an uppercase letter.",
linenumber=task["__line__"],
linenumber=task[LINE_NUMBER_KEY],
tag="name[casing]",
filename=file,
)
Expand Down