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

Update task name in notify for a task against name[casing] error #4038

Merged
merged 2 commits into from
Feb 26, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion examples/playbooks/name-case.transformed.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
---
- name: This lacks a capitalization
hosts: localhost
tasks: []
tasks:
- name: Task that always changes
ansible.builtin.debug:
msg: I always change!
changed_when: true
notify: My handler

- name: Task with notify as list
ansible.builtin.debug:
msg: I always change!
changed_when: true
notify:
- my handler 1
- My handler
- my handler 2

handlers:
- name: My handler
ansible.builtin.debug:
msg: I never run :(

- name: Test task for listen
ansible.builtin.debug:
msg: I never run :(
listen: My handler
26 changes: 25 additions & 1 deletion examples/playbooks/name-case.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
---
- name: this lacks a capitalization
hosts: localhost
tasks: []
tasks:
- name: Task that always changes
ansible.builtin.debug:
msg: I always change!
changed_when: true
notify: my handler

- name: Task with notify as list
ansible.builtin.debug:
msg: I always change!
changed_when: true
notify:
- my handler 1
- my handler
- my handler 2

handlers:
- name: my handler
ansible.builtin.debug:
msg: I never run :(

- name: Test task for listen
ansible.builtin.debug:
msg: I never run :(
listen: "my handler"
48 changes: 37 additions & 11 deletions src/ansiblelint/rules/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
from typing import TYPE_CHECKING, Any

from ansiblelint.constants import LINE_NUMBER_KEY
from ansiblelint.file_utils import Lintable
from ansiblelint.rules import AnsibleLintRule, TransformMixin

if TYPE_CHECKING:
from ruamel.yaml.comments import CommentedMap, CommentedSeq

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


Expand Down Expand Up @@ -173,22 +173,48 @@ def transform(
data: CommentedMap | CommentedSeq | str,
) -> None:
if match.tag == "name[casing]":
target_task = self.seek(match.yaml_path, data)
# Not using capitalize(), since that rewrites the rest of the name to lower case
task_name = target_task.get("name", None)
if task_name:

def update_task_name(task_name: str) -> str:
"""Capitalize the first work of the task name."""
# Not using capitalize(), since that rewrites the rest of the name to lower case
if "|" in task_name: # if using prefix
[file_name, update_task_name] = task_name.split("|")
target_task["name"] = (
f"{file_name.strip()} | {update_task_name.strip()[:1].upper()}{update_task_name.strip()[1:]}"
)
else:
target_task["name"] = f"{task_name[:1].upper()}{task_name[1:]}"
return f"{file_name.strip()} | {update_task_name.strip()[:1].upper()}{update_task_name.strip()[1:]}"

return f"{task_name[:1].upper()}{task_name[1:]}"

target_task = self.seek(match.yaml_path, data)
orig_task_name = target_task.get("name", None)
# pylint: disable=too-many-nested-blocks
if orig_task_name:
updated_task_name = update_task_name(orig_task_name)
for item in data:
if isinstance(item, dict) and "tasks" in item:
for task in item["tasks"]:
if (
isinstance(task["notify"], str)
and orig_task_name == task["notify"]
):
task["notify"] = updated_task_name
elif isinstance(task["notify"], list):
for idx in range(len(task["notify"])):
if orig_task_name == task["notify"][idx]:
task["notify"][idx] = updated_task_name

if isinstance(item, dict) and "handlers" in item:
for task in item["handlers"]:
listener_task_name = task.get("listen", None)
if (
listener_task_name
and listener_task_name == orig_task_name
):
task["listen"] = updated_task_name

target_task["name"] = updated_task_name
match.fixed = True


if "pytest" in sys.modules:
from ansiblelint.file_utils import Lintable
from ansiblelint.rules import RulesCollection
from ansiblelint.runner import Runner

Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/schemas/__store__.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/requirements.json"
},
"role-arg-spec": {
"etag": "c1eadbf5b551aeb9b8f2781728fb6d66453609b44a8921bcc65984ab3daf4bdc",
"etag": "e0f25bc37f7b43d55b8e8f41929cab803179550e237d63c1134d51dfdd985ddf",
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/role-arg-spec.json"
},
"rulebook": {
Expand Down
4 changes: 2 additions & 2 deletions test/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def fixture_runner_result(
id="strings",
),
pytest.param("examples/playbooks/vars/empty.yml", 1, False, True, id="empty"),
pytest.param("examples/playbooks/name-case.yml", 1, True, True, id="name_case"),
pytest.param("examples/playbooks/name-case.yml", 2, True, True, id="name_case"),
pytest.param("examples/playbooks/fqcn.yml", 3, True, True, id="fqcn"),
pytest.param(
"examples/playbooks/multi_yaml_doc.yml",
Expand Down Expand Up @@ -182,7 +182,7 @@ def fixture_runner_result(
2,
True,
True,
id="name_case",
id="name_case_roles",
),
),
)
Expand Down
1 change: 0 additions & 1 deletion test/test_yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ def test_formatted_yaml_loader_dumper(
version: tuple[int, int],
) -> None:
"""Ensure that FormattedYAML loads/dumps formatting fixtures consistently."""
# pylint: disable=unused-argument
before_content, prettier_content, after_content = load_yaml_formatting_fixtures(
fixture_filename,
)
Expand Down
Loading