Skip to content

Commit fd28eae

Browse files
committed
[validate] Add AG108 validator for agent RGB color
1 parent ee54e39 commit fd28eae

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

.changelog/5092.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
changes:
2+
- description: Added validation AG108 to ensure the agent color is a valid 6-digit RGB hex format.
3+
type: feature
4+
pr_number: 5092

demisto_sdk/commands/validate/sdk_validation_config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ select = [
162162
"AG101",
163163
"AG102",
164164
"AG103",
165+
"AG104",
165166
"DS100",
166167
"DS101",
167168
"DS105",
@@ -323,6 +324,7 @@ select = [
323324
"AG101",
324325
"AG102",
325326
"AG103",
327+
"AG104",
326328
"PA100",
327329
"PA101",
328330
"PA102",

demisto_sdk/commands/validate/tests/AG_validators_test.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
from demisto_sdk.commands.content_graph.objects.agentix_action import AgentixAction
44
from demisto_sdk.commands.content_graph.objects.agentix_agent import AgentixAgent
55
from demisto_sdk.commands.content_graph.objects.script import Script
6+
from demisto_sdk.commands.validate.tests.test_tools import create_agentix_agent_object
67
from demisto_sdk.commands.validate.validators.AG_validators.AG100_is_forbidden_content_item import (
78
IsForbiddenContentItemValidator,
89
)
910
from demisto_sdk.commands.validate.validators.AG_validators.AG101_is_correct_mp import (
1011
IsCorrectMPValidator,
1112
)
13+
from demisto_sdk.commands.validate.validators.AG_validators.AG104_is_valid_rgb_color import (
14+
IsValidColorValidator,
15+
)
1216

1317

1418
def test_is_forbidden_content_item():
@@ -215,3 +219,51 @@ def test_is_correct_marketplace():
215219
assert results[0].message == (
216220
"The following Agentix related content item 'test' should have only marketplace 'platform'."
217221
)
222+
223+
224+
def test_is_valid_color():
225+
"""
226+
Given:
227+
- Two AgentixAgent items, one with a valid color and one with an invalid color.
228+
229+
When:
230+
- Calling the IsValidColorValidator obtain_invalid_content_items function.
231+
232+
Then:
233+
- Make sure one failure is returned for the invalid color and the error message is correct.
234+
"""
235+
content_items = [
236+
create_agentix_agent_object(
237+
paths=["color", "display"],
238+
values=["#FF0000", "Valid Color Agent"],
239+
),
240+
create_agentix_agent_object(
241+
paths=["color", "display"],
242+
values=["invalid_color", "Invalid Color Agent"],
243+
),
244+
create_agentix_agent_object(
245+
paths=["color", "display"],
246+
values=["#12345G", "Invalid Hex Agent"],
247+
),
248+
create_agentix_agent_object(
249+
paths=["color", "display"],
250+
values=["#FFF", "Short Hex Agent"],
251+
),
252+
]
253+
254+
results = IsValidColorValidator().obtain_invalid_content_items(content_items)
255+
256+
assert len(results) == 3
257+
error_messages = [result.message for result in results]
258+
assert (
259+
"The Agentix-agent 'Invalid Color Agent' color 'invalid_color' is not a valid RGB hex color.\n"
260+
"Please make sure that the color is a valid 6-digit hex color string, starting with '#'. For example: '#FFFFFF'."
261+
) in error_messages
262+
assert (
263+
"The Agentix-agent 'Invalid Hex Agent' color '#12345G' is not a valid RGB hex color.\n"
264+
"Please make sure that the color is a valid 6-digit hex color string, starting with '#'. For example: '#FFFFFF'."
265+
) in error_messages
266+
assert (
267+
"The Agentix-agent 'Short Hex Agent' color '#FFF' is not a valid RGB hex color.\n"
268+
"Please make sure that the color is a valid 6-digit hex color string, starting with '#'. For example: '#FFFFFF'."
269+
) in error_messages
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from __future__ import annotations
2+
3+
import re
4+
from typing import Iterable, List
5+
6+
from demisto_sdk.commands.common.constants import GitStatuses
7+
from demisto_sdk.commands.content_graph.objects.agentix_agent import AgentixAgent
8+
from demisto_sdk.commands.validate.validators.base_validator import (
9+
BaseValidator,
10+
ValidationResult,
11+
)
12+
13+
ContentTypes = AgentixAgent
14+
15+
16+
class IsValidColorValidator(BaseValidator[ContentTypes]):
17+
error_code = "AG104"
18+
description = "Validate that the Agentix-agent color is a valid RGB hex color."
19+
rationale = "The color field must be a valid RGB hex color string to be displayed correctly in the UI."
20+
error_message = (
21+
"The Agentix-agent '{0}' color '{1}' is not a valid RGB hex color.\n"
22+
"Please make sure that the color is a valid 6-digit hex color string, starting with '#'. For example: '#FFFFFF'."
23+
)
24+
related_field = "color"
25+
is_auto_fixable = False
26+
27+
def obtain_invalid_content_items(
28+
self, content_items: Iterable[ContentTypes]
29+
) -> List[ValidationResult]:
30+
return [
31+
ValidationResult(
32+
validator=self,
33+
message=self.error_message.format(
34+
content_item.display_name,
35+
content_item.color,
36+
),
37+
content_object=content_item,
38+
)
39+
for content_item in content_items
40+
if not self.is_valid_color(content_item.color)
41+
]
42+
43+
def is_valid_color(self, color: str) -> bool:
44+
"""Checks if a string is a valid hex color."""
45+
return bool(re.match(r"^#[0-9a-fA-F]{6}$", color))

0 commit comments

Comments
 (0)