-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added the new validation * added UT * added the validation to the config file * added changelog * fixed description * fixed cr notes * changed to task field * update CR notes * update CR notes * update CR notes
- Loading branch information
Showing
4 changed files
with
151 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
changes: | ||
- description: Added the PB103 to the new validation format. Validate whether there is an unconnected task. | ||
type: internal | ||
pr_number: 4340 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
.../commands/validate/validators/PB_validators/PB103_does_playbook_have_unconnected_tasks.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any, Iterable, List | ||
|
||
from demisto_sdk.commands.content_graph.objects.playbook import Playbook | ||
from demisto_sdk.commands.validate.validators.base_validator import ( | ||
BaseValidator, | ||
ValidationResult, | ||
) | ||
|
||
ContentTypes = Playbook | ||
ERROR_MSG = "The following tasks ids have no previous tasks: {orphan_tasks}." | ||
|
||
|
||
class DoesPlaybookHaveUnconnectedTasks(BaseValidator[ContentTypes]): | ||
error_code = "PB103" | ||
description = "Validate whether there is an unconnected task." | ||
rationale = "Make sure there are no unconnected tasks to ensure the playbook will work as expected." | ||
error_message = ERROR_MSG | ||
related_field = "tasks" | ||
is_auto_fixable = False | ||
|
||
@staticmethod | ||
def is_unconnected_task(playbook: ContentTypes) -> set[Any]: | ||
"""Checks whether a playbook has an unconnected task. | ||
Args: | ||
- playbook (ContentTypes): The playbook to check. | ||
Return: | ||
- bool. True if the playbook has an unconnected task, False otherwise. | ||
""" | ||
start_task_id = playbook.data.get("starttaskid") | ||
tasks = playbook.tasks | ||
tasks_bucket = set() | ||
next_tasks_bucket = set() | ||
|
||
for task_id, task in tasks.items(): | ||
if task_id != start_task_id: | ||
tasks_bucket.add(task_id) | ||
if next_tasks := task.nexttasks: | ||
for next_task_ids in next_tasks.values(): | ||
if next_task_ids: | ||
next_tasks_bucket.update(next_task_ids) | ||
orphan_tasks = tasks_bucket.difference(next_tasks_bucket) | ||
return orphan_tasks | ||
|
||
def is_valid(self, content_items: Iterable[ContentTypes]) -> List[ValidationResult]: | ||
return [ | ||
ValidationResult( | ||
validator=self, | ||
message=self.error_message.format(orphan_tasks=sorted(orphan_tasks)), | ||
content_object=content_item, | ||
) | ||
for content_item in content_items | ||
if (orphan_tasks := self.is_unconnected_task(content_item)) | ||
] |