diff --git a/.changelog/4662.yml b/.changelog/4662.yml new file mode 100644 index 00000000000..5ae6ff04424 --- /dev/null +++ b/.changelog/4662.yml @@ -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 diff --git a/demisto_sdk/commands/validate/sdk_validation_config.toml b/demisto_sdk/commands/validate/sdk_validation_config.toml index 65af12ae63c..83e1e90b2d9 100644 --- a/demisto_sdk/commands/validate/sdk_validation_config.toml +++ b/demisto_sdk/commands/validate/sdk_validation_config.toml @@ -145,6 +145,7 @@ select = [ "PB125", "PB126", "PB127", + "PB130", "PB114", "BA100", "BA101", @@ -332,6 +333,7 @@ select = [ "PB114", "PB119", "PB127", + "PB130", "BC100", "BC101", "BC102", diff --git a/demisto_sdk/commands/validate/tests/PB_validators_test.py b/demisto_sdk/commands/validate/tests/PB_validators_test.py index ddb5ad8dc97..8abe89cd109 100644 --- a/demisto_sdk/commands/validate/tests/PB_validators_test.py +++ b/demisto_sdk/commands/validate/tests/PB_validators_test.py @@ -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( @@ -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) diff --git a/demisto_sdk/commands/validate/validators/PB_validators/PB130_is_silent_playbook.py b/demisto_sdk/commands/validate/validators/PB_validators/PB130_is_silent_playbook.py new file mode 100644 index 00000000000..efff549a72c --- /dev/null +++ b/demisto_sdk/commands/validate/validators/PB_validators/PB130_is_silent_playbook.py @@ -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"] + ) + ) + ]