Skip to content

Commit

Permalink
improvement: Hide instead of remove the plays without roles
Browse files Browse the repository at this point in the history
  • Loading branch information
haidaraM committed Jan 1, 2025
1 parent 5fca79e commit 8931b8e
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 18 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ Comparison of the renderers:
| Highlight on hover ||| ❌: NA |
| Change graph orientation ||| ❌: NA |
| Group roles by name ||| ✅: the roles with the same names will have the same IDs. |
| Hide empty roles and blocks ||| ❌: They are kept in the JSON output |
| View the output file in your the OS default viewer || ✅ on https://mermaid.live/ ||
| Tests of the output | Automatic | Manual (need a parser) | Automatic |

Expand Down
2 changes: 1 addition & 1 deletion ansibleplaybookgrapher/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def run(self):
p.remove_empty_plays()

if self.options.hide_plays_without_roles:
p.remove_plays_without_roles()
p.hide_plays_without_roles()

if self.options.only_roles:
p.hide_task_nodes()
Expand Down
11 changes: 5 additions & 6 deletions ansibleplaybookgrapher/graph_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,15 +492,14 @@ def remove_empty_plays(self):
for play in to_exclude:
self.remove_node("plays", play)

def remove_plays_without_roles(self):
"""Remove the plays that do not have roles from the playbook.
def hide_plays_without_roles(self):
"""Hide the plays that do not have at least one role.
:return:
"""
to_exclude = [play for play in self.plays if not play.has_node_type(RoleNode)]

for play in to_exclude:
self.remove_node("plays", play)
for play in self.plays:
if not play.has_node_type(RoleNode):
play.is_hidden = True


class PlayNode(CompositeNode):
Expand Down
9 changes: 5 additions & 4 deletions ansibleplaybookgrapher/renderer/graphviz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,11 @@ def build_playbook(
)

for play in self.playbook_node.plays:
with self.digraph.subgraph(name=play.name) as play_subgraph:
self.build_play(
play, digraph=play_subgraph, show_handlers=show_handlers, **kwargs
)
if not play.is_hidden:
with self.digraph.subgraph(name=play.name) as play_subgraph:
self.build_play(
play, digraph=play_subgraph, show_handlers=show_handlers, **kwargs
)

return self.digraph.source

Expand Down
3 changes: 2 additions & 1 deletion ansibleplaybookgrapher/renderer/mermaid.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ def build_playbook(
self._indentation_level += 1

for play_node in self.playbook_node.plays:
self.build_play(play_node, show_handlers=show_handlers, **kwargs)
if not play_node.is_hidden:
self.build_play(play_node, show_handlers=show_handlers, **kwargs)
self._indentation_level -= 1

self.add_comment(f"End of the playbook '{self.playbook_node.display_name()}'\n")
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/roles/display_some_facts/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
- name: ansible_architecture
debug:
var: ansible_architecture
tags:
- test

- name: ansible_date_time
debug:
Expand Down
7 changes: 5 additions & 2 deletions tests/test_graph_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ def test_remove_plays_without_roles() -> None:
playbook.add_node("plays", play_2)

assert len(playbook.plays) == 2, "There should be 2 plays"
playbook.remove_plays_without_roles()
assert len(playbook.plays) == 1, "There should be only one play"
assert not play_1.is_hidden
assert not play_2.is_hidden
playbook.hide_plays_without_roles()
assert not play_1.is_hidden
assert play_2.is_hidden


def test_get_all_tasks_nodes() -> None:
Expand Down
27 changes: 24 additions & 3 deletions tests/test_json_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ def test_with_block(request: pytest.FixtureRequest) -> None:
def test_with_block_with_skip_tags(
request: pytest.FixtureRequest,
) -> None:
"""
:return:
"""Test with the --skip-tags option with a block.
:return:
"""
json_path, playbook_paths = run_grapher(
["with_block.yml"],
Expand Down Expand Up @@ -267,6 +267,7 @@ def test_with_block_with_skip_tags(
def test_group_roles_by_name(request: pytest.FixtureRequest, flag: str) -> None:
"""Test when grouping roles by name. This doesn't really affect the JSON renderer: multiple nodes will have the same ID.
This test ensures that regardless of the flag '--group-roles-by-name', we get the same nodes in the output.
:param request:
:return:
"""
Expand Down Expand Up @@ -372,6 +373,27 @@ def test_handler_in_a_role(
)


def test_hide_plays_without_roles(request: pytest.FixtureRequest) -> None:
"""Test the --hide-plays-without-roles flag.
:param request:
:return:
"""
json_path, playbook_paths = run_grapher(
["play-hiding.yml"],
output_filename=request.node.name,
additional_args=[
"--hide-plays-without-roles",
],
)
_common_tests(
json_path,
plays_number=2,
roles_number=2,
tasks_number=1,
)


@pytest.mark.parametrize(
("include_role_tasks_option", "expected_roles_number"),
[("--", 4), ("--include-role-tasks", 6)],
Expand All @@ -384,7 +406,6 @@ def test_only_roles_with_nested_include_roles(
) -> None:
"""Test graphing a playbook with the --only-roles flag.
For the JSON renderer, the empty roles are not excluded by design. As a result, the number of roles is different
:param request:
:return:
"""
Expand Down

0 comments on commit 8931b8e

Please sign in to comment.