Skip to content

Commit

Permalink
Validate Silent Playbook name/id (#4662)
Browse files Browse the repository at this point in the history
* added validate

* added unittest

* added changelog

* fixed comments

* pre commit
  • Loading branch information
moishce authored Dec 15, 2024
1 parent b2d6248 commit 6ad26b1
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .changelog/4662.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- description: Added validation checks whether the silent playbook name id and the isSilent key are set correctly.
type: feature
pr_number: 4662
2 changes: 2 additions & 0 deletions demisto_sdk/commands/validate/sdk_validation_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ select = [
"PB125",
"PB126",
"PB127",
"PB130",
"PB114",
"BA100",
"BA101",
Expand Down Expand Up @@ -332,6 +333,7 @@ select = [
"PB114",
"PB119",
"PB127",
"PB130",
"BC100",
"BC101",
"BC102",
Expand Down
85 changes: 85 additions & 0 deletions demisto_sdk/commands/validate/tests/PB_validators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
from demisto_sdk.commands.validate.validators.PB_validators.PB127_marketplace_keys_have_default_value import (
MarketplaceKeysHaveDefaultValidator,
)
from demisto_sdk.commands.validate.validators.PB_validators.PB130_is_silent_playbook import (
IsSilentPlaybookValidator,
)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -1338,3 +1341,85 @@ def test_MarketplaceKeysHaveDefaultValidator(
assert expected_bad_key in fix_message

assert fixed_content_item.data == expected_playbook_obj.data


@pytest.mark.parametrize(
"name, id, is_silent, result_len",
[
(
"test",
"test",
False,
0,
),
(
"silent-test",
"silent-test",
True,
0,
),
(
"test",
"silent-test",
True,
1,
),
(
"silent-test",
"test",
True,
1,
),
(
"silent-test",
"silent-test",
False,
1,
),
(
"test",
"silent-test",
False,
1,
),
(
"silent-test",
"test",
False,
1,
),
(
"test",
"test",
True,
1,
),
],
)
def test_IsSilentPlaybookValidator(name, id, is_silent, result_len):
"""
Given:
case 1: isSilent = False, and name/id do not contain silent prefix.
case 2: isSilent = True, and name/id contain silent prefix.
case 3: isSilent = True, name contain and id do not contain silent prefix.
case 4: isSilent = True, id contain and name do not contain silent prefix.
case 5: isSilent = False, and name/id contain silent prefix.
case 6: isSilent = False, name contain and id do not contain silent prefix.
case 7: isSilent = False, id contain and name do not contain silent prefix.
case 8: isSilent = True, and name/id do not contain silent prefix.
When:
- calling IsSilentPlaybookValidator.obtain_invalid_content_items.
Then:
- Checks that it only fails in the right cases.
"""
playbook = create_playbook_object()
playbook.data["id"] = id
playbook.data["name"] = name
playbook.data["isSilent"] = is_silent

invalid_content_items = IsSilentPlaybookValidator().obtain_invalid_content_items(
[playbook]
)
assert result_len == len(invalid_content_items)
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from __future__ import annotations

from typing import Iterable, List, Union

from demisto_sdk.commands.content_graph.objects.playbook import Playbook
from demisto_sdk.commands.validate.validators.base_validator import (
BaseValidator,
ValidationResult,
)

ContentTypes = Union[Playbook]


class IsSilentPlaybookValidator(BaseValidator[ContentTypes]):
error_code = "PB130"
description = "Validate that the name and ID of a silent-Playbook contain the silent prefix and include the field isSilent: true"
rationale = "In silent-Playbook the name and ID prefix should be silent and also include the field isSilent: true"
error_message = "Silent-Playbook should have silent as a prefix in the name and ID, as well as the field isSilent: true, one of them is missing."
related_field = ""
is_auto_fixable = False

def obtain_invalid_content_items(
self, content_items: Iterable[ContentTypes]
) -> List[ValidationResult]:
return [
ValidationResult(
validator=self,
message=self.error_message,
content_object=content_item,
)
for content_item in content_items
if (
content_item.data.get("isSilent")
or any(
content_item.data.get(key, "").startswith("silent")
for key in ["name", "id"]
)
)
and not (
content_item.data.get("isSilent")
and all(
content_item.data.get(key, "").startswith("silent")
for key in ["name", "id"]
)
)
]

0 comments on commit 6ad26b1

Please sign in to comment.