Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IM101 #4199

Merged
merged 10 commits into from
Apr 11, 2024
Merged

IM101 #4199

Show file tree
Hide file tree
Changes from 3 commits
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: 2 additions & 2 deletions demisto_sdk/commands/validate/sdk_validation_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ select = [
"CL100",
"GF100", "GF101", "GF102",
"IF100", "IF101", "IF102", "IF103", "IF104", "IF105", "IF106",
"IM100", "IM108", "IM109",
"IM100", "IM101", "IM108", "IM109",
"RP101", "BC106", "BC107"
]
warning = []
Expand All @@ -57,7 +57,7 @@ select = [
"CL100",
"GF100", "GF101",
"IF100", "IF101", "IF102", "IF103", "IF104", "IF105", "IF106",
"IM108",
"IM101", "IM108",
"RP101"
]
warning = ["IN101"]
Expand Down
31 changes: 31 additions & 0 deletions demisto_sdk/commands/validate/tests/IM_validators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from demisto_sdk.commands.validate.validators.IM_validators.IM100_image_exists_validation import (
ImageExistsValidator,
)
from demisto_sdk.commands.validate.validators.IM_validators.IM101_image_too_large import (
ImageTooLargeValidator,
)
from demisto_sdk.commands.validate.validators.IM_validators.IM108_author_image_is_empty import (
AuthorImageIsEmptyValidator,
)
Expand Down Expand Up @@ -41,6 +44,34 @@ def test_ImageExistsValidator_is_valid_image_path():
)


def test_ImageTooLargeValidator_is_valid():
"""
Given:
content_items with 2 integrations:
- One integration with an image that is not in a valid size.
- One integration with an image that is in a valid size.

When:
- Calling the ImageTooLargeValidator is_valid function.

Then:
- Make sure the right amount of integration image path failed, and that the right error message is returned.
- Case 1: Should fail.
- Case 2: Shouldn't fail.
"""
content_items = [
BEAdi marked this conversation as resolved.
Show resolved Hide resolved
create_integration_object(image="short image"),
create_integration_object(image="A very big image" * 1000),
]
results = ImageTooLargeValidator().is_valid(content_items)
assert len(results) == 1
assert all(
"You've created/modified a yml or package with a large sized image. Please make sure to change the image dimensions at: TestIntegration_image.png."
in result.message
for result in results
)


def test_AuthorImageExistsValidator_is_valid_image_path():
"""
Given:
Expand Down
3 changes: 3 additions & 0 deletions demisto_sdk/commands/validate/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def create_integration_object(
values: Optional[List[Any]] = None,
pack_info: Optional[Dict[str, Any]] = None,
readme_content: Optional[str] = None,
image: Optional[str] = None,
code: Optional[str] = None,
) -> Integration:
"""Creating an integration object with altered fields from a default integration yml structure.
Expand All @@ -57,6 +58,8 @@ def create_integration_object(
integration = pack.create_integration(yml=yml_content, **additional_params)
code = code or "from MicrosoftApiModule import *"
integration.code.write(code)
if image is not None:
integration.image.write(image)
return BaseContent.from_path(Path(integration.path)) # type:ignore


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from __future__ import annotations

from typing import Iterable, List

from demisto_sdk.commands.common.constants import GitStatuses
from demisto_sdk.commands.content_graph.objects.integration import Integration
from demisto_sdk.commands.content_graph.parsers.related_files import RelatedFileType
from demisto_sdk.commands.validate.validators.base_validator import (
BaseValidator,
ValidationResult,
)

ContentTypes = Integration


class ImageTooLargeValidator(BaseValidator[ContentTypes]):
error_code = "IM101"
description = "Checks that the image file dimensions are matching the requirements."
rationale = "Image needs to fit its place in the UI. For more information see: https://xsoar.pan.dev/docs/integrations/integration-logo"
error_message = "You've created/modified a yml or package with a large sized image. Please make sure to change the image dimensions at:"
BEAdi marked this conversation as resolved.
Show resolved Hide resolved
related_field = "image"
is_auto_fixable = False
expected_git_statuses = [GitStatuses.ADDED, GitStatuses.MODIFIED]
BEAdi marked this conversation as resolved.
Show resolved Hide resolved
related_file_type = [RelatedFileType.IMAGE]

def is_valid(self, content_items: Iterable[ContentTypes]) -> List[ValidationResult]:
image_max_size = 10 * 1024 # 10kB
BEAdi marked this conversation as resolved.
Show resolved Hide resolved
return [
ValidationResult(
validator=self,
message=f"{self.error_message} {content_item.name}_image.png.",
content_object=content_item,
)
for content_item in content_items
if content_item.image.get_file_size().st_size > image_max_size
]