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

Add PB108 to the new validate #4359

Merged
merged 15 commits into from
Jun 18, 2024
4 changes: 4 additions & 0 deletions .changelog/4359.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- description: Moved PB108 to the new validate
RotemAmit marked this conversation as resolved.
Show resolved Hide resolved
type: internal
pr_number: 4359
3 changes: 2 additions & 1 deletion demisto_sdk/commands/validate/sdk_validation_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ select = [
"IN100", "IN101", "IN102", "IN104", "IN106", "IN107", "IN108", "IN109", "IN110", "IN112", "IN113", "IN114", "IN115", "IN117", "IN118", "IN121", "IN122", "IN123", "IN124", "IN125", "IN126", "IN127", "IN130",
"IN131", "IN134", "IN135", "IN139", "IN141", "IN142", "IN144", "IN145", "IN146", "IN149", "IN150", "IN151", "IN152", "IN153", "IN154", "IN156", "IN158", "IN159", "IN160",
"IN161", "IN162",
"PB100", "PB101","PB104","PB118", "PB123",
"PB100", "PB101","PB104","PB108","PB118", "PB123",
"DO100", "DO101", "DO102", "DO103", "DO104", "DO105", "DO106",
"DS100", "DS107",
"SC100", "SC105", "SC106", "SC109",
Expand Down Expand Up @@ -67,6 +67,7 @@ select = [
"RP101",
"IT102", "IT103",
"MR100",
"PB108"
]
warning = [
"IN101",
Expand Down
58 changes: 58 additions & 0 deletions demisto_sdk/commands/validate/tests/PB_validators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from demisto_sdk.commands.validate.validators.PB_validators.PB104_deprecated_description import (
DeprecatedDescriptionValidator,
)
from demisto_sdk.commands.validate.validators.PB_validators.PB108_is_valid_task_id import (
IsValidTaskIdValidator,
)
from demisto_sdk.commands.validate.validators.PB_validators.PB118_is_input_key_not_in_tasks import (
IsInputKeyNotInTasksValidator,
)
Expand Down Expand Up @@ -222,3 +225,58 @@ def test_IsAskConditionHasUnhandledReplyOptionsValidator():
)
}
assert IsAskConditionHasUnhandledReplyOptionsValidator().is_valid([playbook])


def create_invalid_playbook(field: str):
"""Create an invalid playbook that has an invalid taskid or the 'id' under the 'task' field is invalid
Args:
- field str: which field to update taskid or task.id.
Return:
- a playbook object
"""
playbook = create_playbook_object()
tasks = playbook.tasks
for task_id in tasks:
task_obj = tasks[task_id]
if field == "taskid":
task_obj.taskid = task_obj.taskid + "1234"
else:
task_obj.task.id = task_obj.task.id + "1234"
break
return playbook


def test_IsValidTaskIdValidator(playbook):
"""
Given:
- A playbook
Case 1: The playbook is valid.
Case 2: The playbook isn't valid, it has invalid taskid.
Case 3: The playbook isn't valid, the 'id' under the 'task' field is invalid.

When:
- calling IsValidTaskIdValidator.is_valid.

Then:
- The results should be as expected:
Case 1: The playbook is valid
Case 2: The playbook is invalid
Case 3: The playbook is invalid
"""
# Case 1
playbook_valid = create_playbook_object()
results_valid = IsValidTaskIdValidator().is_valid([playbook_valid])

# Case 2
playbook_invalid_taskid = create_invalid_playbook("taskid")
results_invalid_taskid = IsValidTaskIdValidator().is_valid(
[playbook_invalid_taskid]
)

# Case 3
playbook_invalid_id = create_invalid_playbook("id")
results_invalid_id = IsValidTaskIdValidator().is_valid([playbook_invalid_id])

assert not results_valid
assert results_invalid_taskid
assert results_invalid_id
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from __future__ import annotations

from typing import Iterable, List

from demisto_sdk.commands.common.tools import is_string_uuid
from demisto_sdk.commands.content_graph.objects.base_playbook import TaskConfig
from demisto_sdk.commands.content_graph.objects.playbook import Playbook
from demisto_sdk.commands.validate.validators.base_validator import (
BaseValidator,
ValidationResult,
)

ContentTypes = Playbook


class IsValidTaskIdValidator(BaseValidator[ContentTypes]):
error_code = "PB108"
description = "Validate that the task ID and the 'id' under the 'task' field are from UUID format"
RotemAmit marked this conversation as resolved.
Show resolved Hide resolved
rationale = ""
RotemAmit marked this conversation as resolved.
Show resolved Hide resolved
error_message = (
"On task: {0}, the field 'taskid': {1} and the 'id' under the 'task' field: {2}, must be from "
"uuid format."
)
related_field = "taskid"
is_auto_fixable = False
# expected_git_statuses = [GitStatuses.ADDED, GitStatuses.MODIFIED]
RotemAmit marked this conversation as resolved.
Show resolved Hide resolved

def is_valid(self, content_items: Iterable[ContentTypes]) -> List[ValidationResult]:
"""Check whether all playbook tasks has valid taskid and the 'id' under the 'task' field is valid as well.
Args:
- content_items (Iterable[ContentTypes]): The content items to check.
Return:
- List[ValidationResult]. List of ValidationResults objects.
"""
results: List[ValidationResult] = []
for content_item in content_items:
invalid_tasks = self.invalid_tasks(content_item.tasks)
for task_id in invalid_tasks:
taskid = invalid_tasks[task_id].taskid
inner_id = invalid_tasks[task_id].task.id
results.append(
ValidationResult(
validator=self,
message=self.error_message.format(task_id, taskid, inner_id),
content_object=content_item,
)
)
return results
RotemAmit marked this conversation as resolved.
Show resolved Hide resolved

def invalid_tasks(self, tasks: dict[str, TaskConfig]) -> dict[str, TaskConfig]:
"""Check which tasks has invalid taskid or the 'id' under the 'task' field is invalid
Args:
- tasks dict[str, TaskConfig]: The playbook tasks.
Return:
- dict[str, TaskConfig] that contains the invalid tasks.
"""
invalid_tasks = {}
for task_id in tasks:
task = tasks[task_id]
RotemAmit marked this conversation as resolved.
Show resolved Hide resolved
taskid = task.taskid
inner_id = task.task.id
is_valid_task = is_string_uuid(taskid) and is_string_uuid(inner_id)
if not is_valid_task:
invalid_tasks[task_id] = task
return invalid_tasks
Loading