Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changelog/5092.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- description: Added validation AG108 to ensure the agent color is a valid 6-digit RGB hex format.
type: feature
pr_number: 5092
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 @@ -162,6 +162,7 @@ select = [
"AG101",
"AG102",
"AG103",
"AG104",
"DS100",
"DS101",
"DS105",
Expand Down Expand Up @@ -323,6 +324,7 @@ select = [
"AG101",
"AG102",
"AG103",
"AG104",
"PA100",
"PA101",
"PA102",
Expand Down
52 changes: 52 additions & 0 deletions demisto_sdk/commands/validate/tests/AG_validators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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))
Loading