diff --git a/.changelog/5092.yml b/.changelog/5092.yml new file mode 100644 index 0000000000..7f9a320a72 --- /dev/null +++ b/.changelog/5092.yml @@ -0,0 +1,4 @@ +changes: +- description: Added validation AG104 to ensure the agent color is a valid 6-digit RGB hex format. + type: feature +pr_number: 5092 diff --git a/demisto_sdk/commands/validate/sdk_validation_config.toml b/demisto_sdk/commands/validate/sdk_validation_config.toml index 17daf64ffc..8a699c145f 100644 --- a/demisto_sdk/commands/validate/sdk_validation_config.toml +++ b/demisto_sdk/commands/validate/sdk_validation_config.toml @@ -162,6 +162,7 @@ select = [ "AG101", "AG102", "AG103", + "AG104", "DS100", "DS101", "DS105", @@ -323,6 +324,7 @@ select = [ "AG101", "AG102", "AG103", + "AG104", "PA100", "PA101", "PA102", diff --git a/demisto_sdk/commands/validate/tests/AG_validators_test.py b/demisto_sdk/commands/validate/tests/AG_validators_test.py index 71c7b01ba5..c8fc053f05 100644 --- a/demisto_sdk/commands/validate/tests/AG_validators_test.py +++ b/demisto_sdk/commands/validate/tests/AG_validators_test.py @@ -3,12 +3,16 @@ from demisto_sdk.commands.content_graph.objects.agentix_action import AgentixAction from demisto_sdk.commands.content_graph.objects.agentix_agent import AgentixAgent from demisto_sdk.commands.content_graph.objects.script import Script +from demisto_sdk.commands.validate.tests.test_tools import create_agentix_agent_object from demisto_sdk.commands.validate.validators.AG_validators.AG100_is_forbidden_content_item import ( IsForbiddenContentItemValidator, ) from demisto_sdk.commands.validate.validators.AG_validators.AG101_is_correct_mp import ( IsCorrectMPValidator, ) +from demisto_sdk.commands.validate.validators.AG_validators.AG104_is_valid_rgb_color import ( + IsValidColorValidator, +) def test_is_forbidden_content_item(): @@ -215,3 +219,51 @@ def test_is_correct_marketplace(): assert results[0].message == ( "The following Agentix related content item 'test' should have only marketplace 'platform'." ) + + +def test_is_valid_color(): + """ + Given: + - Two AgentixAgent items, one with a valid color and one with an invalid color. + + When: + - Calling the IsValidColorValidator obtain_invalid_content_items function. + + Then: + - Make sure one failure is returned for the invalid color and the error message is correct. + """ + content_items = [ + create_agentix_agent_object( + paths=["color", "display"], + values=["#FF0000", "Valid Color Agent"], + ), + create_agentix_agent_object( + paths=["color", "display"], + values=["invalid_color", "Invalid Color Agent"], + ), + create_agentix_agent_object( + paths=["color", "display"], + values=["#12345G", "Invalid Hex Agent"], + ), + create_agentix_agent_object( + paths=["color", "display"], + values=["#FFF", "Short Hex Agent"], + ), + ] + + results = IsValidColorValidator().obtain_invalid_content_items(content_items) + + assert len(results) == 3 + error_messages = [result.message for result in results] + assert ( + "The Agentix-agent 'Invalid Color Agent' color 'invalid_color' is not a valid RGB hex color.\n" + "Please make sure that the color is a valid 6-digit hex color string, starting with '#'. For example: '#FFFFFF'." + ) in error_messages + assert ( + "The Agentix-agent 'Invalid Hex Agent' color '#12345G' is not a valid RGB hex color.\n" + "Please make sure that the color is a valid 6-digit hex color string, starting with '#'. For example: '#FFFFFF'." + ) in error_messages + assert ( + "The Agentix-agent 'Short Hex Agent' color '#FFF' is not a valid RGB hex color.\n" + "Please make sure that the color is a valid 6-digit hex color string, starting with '#'. For example: '#FFFFFF'." + ) in error_messages diff --git a/demisto_sdk/commands/validate/validators/AG_validators/AG104_is_valid_rgb_color.py b/demisto_sdk/commands/validate/validators/AG_validators/AG104_is_valid_rgb_color.py new file mode 100644 index 0000000000..c383a11fe2 --- /dev/null +++ b/demisto_sdk/commands/validate/validators/AG_validators/AG104_is_valid_rgb_color.py @@ -0,0 +1,45 @@ +from __future__ import annotations + +import re +from typing import Iterable, List + +from demisto_sdk.commands.common.constants import GitStatuses +from demisto_sdk.commands.content_graph.objects.agentix_agent import AgentixAgent +from demisto_sdk.commands.validate.validators.base_validator import ( + BaseValidator, + ValidationResult, +) + +ContentTypes = AgentixAgent + + +class IsValidColorValidator(BaseValidator[ContentTypes]): + error_code = "AG104" + description = "Validate that the Agentix-agent color is a valid RGB hex color." + rationale = "The color field must be a valid RGB hex color string to be displayed correctly in the UI." + error_message = ( + "The Agentix-agent '{0}' color '{1}' is not a valid RGB hex color.\n" + "Please make sure that the color is a valid 6-digit hex color string, starting with '#'. For example: '#FFFFFF'." + ) + related_field = "color" + is_auto_fixable = False + + def obtain_invalid_content_items( + self, content_items: Iterable[ContentTypes] + ) -> List[ValidationResult]: + return [ + ValidationResult( + validator=self, + message=self.error_message.format( + content_item.display_name, + content_item.color, + ), + content_object=content_item, + ) + for content_item in content_items + if not self.is_valid_color(content_item.color) + ] + + def is_valid_color(self, color: str) -> bool: + """Checks if a string is a valid hex color.""" + return bool(re.match(r"^#[0-9a-fA-F]{6}$", color))