Skip to content

Commit

Permalink
Fixed code
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Sep 23, 2022
1 parent d527006 commit a222b48
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 33 deletions.
4 changes: 4 additions & 0 deletions src/ansiblelint/rules/name.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ This rule can produce messages such:
languages that support it.
- `name[missing]` - All tasks should be named.
- `name[play]` - All plays should be named.
- `name[template]` - Jinja templates should only be at the end of 'name'. This
helps with the identification of tasks inside the source code when they fail.
The use of templating inside `name` keys is discouraged as there
are multiple cases where the rendering of the name template is not possible.

If you want to ignore some of the messages above, you can add any of them to
the `skip_list`.
Expand Down
42 changes: 9 additions & 33 deletions src/ansiblelint/rules/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class NameRule(AnsibleLintRule):
severity = "MEDIUM"
tags = ["idiom"]
version_added = "v6.5.0 (last update)"
_re_templated_inside = re.compile(r".*\{\{.*\}\}(.+)$")

def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]:
"""Return matches found for a specific play (entry in playbook)."""
Expand Down Expand Up @@ -82,40 +83,15 @@ def _check_name(
filename=lintable,
)
)
if name.count("}}") == 1 and name.count("{{") == 1:
if re.search("{{ .* }}$", name) == None:
results.append(
self.create_matcherror(
message="If names has {{ }} it should be at the end of sentence.",
linenumber=linenumber,
tag="name[formatting]",
filename=lintable,
)
)
if name.count("}}") == 1 and name.count("{{") == 1:
if re.search("{{ .* }}$", name):
name_word = name.split(" ")[-2]
print(name_word)
if re.search(r"item.\d", name_word) or re.search(r"item", name_word):
results.append(
self.create_matcherror(
message="The named template should be meaningful in {{ }}",
linenumber=linenumber,
tag="name[formatting]",
filename=lintable,
)
)
if name.count("}}") > 1 or name.count("{{") > 1:
if name.endswith("}}") == True:
results.append(
self.create_matcherror(
message="If names has {{ }} it should be at the end of sentence.",
linenumber=linenumber,
tag="name[formatting]",
filename=lintable,
)
if self._re_templated_inside.match(name):
results.append(
self.create_matcherror(
message="Jinja templates should only be at the end of 'name'",
linenumber=linenumber,
tag="name[template]",
filename=lintable,
)

)
return results


Expand Down

0 comments on commit a222b48

Please sign in to comment.