-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
345 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
demisto_sdk/commands/validate/validators/IF_validators/IF111_is_field_type_changed.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Iterable, List, cast | ||
|
||
from demisto_sdk.commands.common.constants import GitStatuses | ||
from demisto_sdk.commands.content_graph.objects.incident_field import IncidentField | ||
from demisto_sdk.commands.validate.validators.base_validator import ( | ||
BaseValidator, | ||
FixResult, | ||
ValidationResult, | ||
) | ||
|
||
ContentTypes = IncidentField | ||
|
||
|
||
class IsFieldTypeChangedValidator(BaseValidator[ContentTypes]): | ||
error_code = "IF111" | ||
description = "Checks if the field type changed." | ||
rationale = "Changing type of IncidentField is not allowed by the platform." | ||
error_message = "Changing incident field type is not allowed." | ||
fix_message = "Changed the `type` field back to `{old_type}`." | ||
related_field = "type" | ||
is_auto_fixable = True | ||
expected_git_statuses = [GitStatuses.MODIFIED] | ||
|
||
def is_valid(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.field_type | ||
!= cast(ContentTypes, content_item.old_base_content_object).field_type | ||
) | ||
] | ||
|
||
def fix(self, content_item: ContentTypes) -> FixResult: | ||
content_item.field_type = cast( | ||
ContentTypes, content_item.old_base_content_object | ||
).field_type | ||
return FixResult( | ||
validator=self, | ||
message=self.fix_message.format(old_type=content_item.field_type), | ||
content_object=content_item, | ||
) |
71 changes: 71 additions & 0 deletions
71
demisto_sdk/commands/validate/validators/IF_validators/IF113_name_field_prefix.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Iterable, List | ||
|
||
from demisto_sdk.commands.common.constants import GitStatuses | ||
from demisto_sdk.commands.content_graph.objects.incident_field import IncidentField | ||
from demisto_sdk.commands.validate.validators.base_validator import ( | ||
BaseValidator, | ||
ValidationResult, | ||
) | ||
|
||
ContentTypes = IncidentField | ||
PACKS_IGNORE = ["Common Types", "Core Alert Fields"] | ||
|
||
|
||
class NameFieldPrefixValidator(BaseValidator[ContentTypes]): | ||
error_code = "IF113" | ||
description = "Checks if field name starts with its pack name or one of the item prefixes from pack metadata." | ||
rationale = "Required by the platform." | ||
error_message = ( | ||
"Field name must start with the relevant pack name or one of the item prefixes found in pack metadata." | ||
"\nFollowing prefixes are allowed for this IncidentField:" | ||
"\n{allowed_prefixes}" | ||
) | ||
related_field = "name" | ||
expected_git_statuses = [GitStatuses.ADDED] | ||
|
||
def is_valid(self, content_items: Iterable[ContentTypes]) -> List[ValidationResult]: | ||
return [ | ||
ValidationResult( | ||
validator=self, | ||
message=self.error_message.format( | ||
allowed_prefixes=", ".join(allowed_prefixes(content_item)) | ||
), | ||
content_object=content_item, | ||
) | ||
for content_item in content_items | ||
if ( | ||
content_item.pack_name not in PACKS_IGNORE | ||
and not name_include_allowed_prefix(content_item) | ||
) | ||
] | ||
|
||
|
||
def name_include_allowed_prefix(content_item: ContentTypes) -> bool: | ||
""" | ||
Check if the IncidentField name begins with any of the allowed prefixes. | ||
""" | ||
for prefix in allowed_prefixes(content_item): | ||
if content_item.name.startswith(prefix): | ||
return True | ||
return False | ||
|
||
|
||
def allowed_prefixes(content_item: ContentTypes) -> set: | ||
""" | ||
Collects from pack metadata all the allowed prefixes | ||
""" | ||
prefixes = {content_item.pack_name} | ||
if not content_item.in_pack: | ||
return prefixes | ||
|
||
metadata = content_item.in_pack.pack_metadata_dict or {} | ||
item_prefix = metadata.get("itemPrefix") | ||
|
||
if not item_prefix: | ||
return prefixes | ||
elif isinstance(item_prefix, list): | ||
return prefixes | set(item_prefix) | ||
else: | ||
return prefixes | {item_prefix} |
Oops, something went wrong.