diff --git a/demisto_sdk/commands/common/hook_validations/pack_unique_files.py b/demisto_sdk/commands/common/hook_validations/pack_unique_files.py index fbd9b8a3a1..7120a10798 100644 --- a/demisto_sdk/commands/common/hook_validations/pack_unique_files.py +++ b/demisto_sdk/commands/common/hook_validations/pack_unique_files.py @@ -6,18 +6,12 @@ import os import re -from demisto_sdk.commands.common.constants import (API_MODULES_PACK, - PACK_METADATA_CATEGORIES, - PACK_METADATA_DEPENDENCIES, - PACK_METADATA_FIELDS, - PACK_METADATA_KEYWORDS, - PACK_METADATA_PRICE, - PACK_METADATA_TAGS, - PACK_METADATA_USE_CASES, - PACKS_PACK_IGNORE_FILE_NAME, - PACKS_PACK_META_FILE_NAME, - PACKS_README_FILE_NAME, - PACKS_WHITELIST_FILE_NAME) +from demisto_sdk.commands.common.constants import ( # PACK_METADATA_PRICE, + API_MODULES_PACK, PACK_METADATA_CATEGORIES, PACK_METADATA_DEPENDENCIES, + PACK_METADATA_FIELDS, PACK_METADATA_KEYWORDS, PACK_METADATA_TAGS, + PACK_METADATA_USE_CASES, PACKS_PACK_IGNORE_FILE_NAME, + PACKS_PACK_META_FILE_NAME, PACKS_README_FILE_NAME, + PACKS_WHITELIST_FILE_NAME) from demisto_sdk.commands.common.tools import pack_name_to_path @@ -151,12 +145,13 @@ def _is_pack_meta_file_structure_valid(self): if not isinstance(dependencies_field, dict): self._add_error('The dependencies field in the pack must be a dictionary.') return False - price_field = metadata[PACK_METADATA_PRICE] - try: - int(price_field) - except Exception: - self._add_error('The price field in the pack must be a number.') - return False + # TODO: add it back after #23546 is ready. + # price_field = metadata.get(PACK_METADATA_PRICE) + # try: + # int(price_field) + # except Exception: + # self._add_error('The price field in the pack must be a number.') + # return False for list_field in (PACK_METADATA_KEYWORDS, PACK_METADATA_TAGS, PACK_METADATA_CATEGORIES, PACK_METADATA_USE_CASES): field = metadata[list_field] diff --git a/demisto_sdk/commands/common/tests/pack_metadata_validator_test.py b/demisto_sdk/commands/common/tests/pack_metadata_validator_test.py index bdf5103598..2c64eb9515 100644 --- a/demisto_sdk/commands/common/tests/pack_metadata_validator_test.py +++ b/demisto_sdk/commands/common/tests/pack_metadata_validator_test.py @@ -20,8 +20,13 @@ def test_metadata_validator_valid(self, mocker): validator = PackUniqueFilesValidator('fake') assert validator.validate_pack_meta_file() + # TODO: add the validation for price after #23546 is ready. @pytest.mark.parametrize('metadata', [os.path.join(FILES_PATH, 'pack_metadata_missing_fields.json'), - os.path.join(FILES_PATH, 'pack_metadata_invalid_price.json'), + # os.path.join(FILES_PATH, 'pack_metadata_invalid_price.json'), + os.path.join(FILES_PATH, 'pack_metadata_invalid_dependencies.json'), + os.path.join(FILES_PATH, 'pack_metadata_list_dependencies.json'), + os.path.join(FILES_PATH, 'pack_metadata_empty_category.json'), + os.path.join(FILES_PATH, 'pack_metadata_invalid_keywords.json'), os.path.join(FILES_PATH, 'pack_metadata_invalid_tags.json'), os.path.join(FILES_PATH, 'pack_metadata_list.json')]) def test_metadata_validator_invalid(self, mocker, metadata): diff --git a/demisto_sdk/commands/init/initiator.py b/demisto_sdk/commands/init/initiator.py index 3330484413..deda0742fa 100644 --- a/demisto_sdk/commands/init/initiator.py +++ b/demisto_sdk/commands/init/initiator.py @@ -207,7 +207,7 @@ def create_metadata(fill_manually: bool) -> Dict: 'certification': 'certified', 'useCases': [], 'keywords': [], - 'price': '0', + # 'price': '0', 'dependencies': {}, } @@ -232,9 +232,9 @@ def create_metadata(fill_manually: bool) -> Dict: tags = input("\nTags of the pack, comma separated values: ") tags_list = [t.strip() for t in tags.split(',')] metadata['tags'] = tags_list - - price = input("\nThe price of the pack: ") - metadata['price'] = price + # TODO: add it back after #23546 is ready. + # price = input("\nThe price of the pack: ") + # metadata['price'] = price return metadata @@ -269,19 +269,6 @@ def get_valid_user_input(options_list: List[str], option_message: str) -> str: return options_list[user_choice - 1] - @staticmethod - def get_yml_data_as_dict(file_path: str) -> Dict: - """Converts YML file data to Dict. - - Args: - file_path (str): The path to the .yml file - - Returns: - Dict. Data from YML. - """ - with open(file_path) as f: - return yaml.load(f, Loader=yamlordereddictloader.SafeLoader) - def integration_init(self) -> bool: """Creates a new integration according to a template. @@ -365,7 +352,8 @@ def yml_reformatting(self, current_suffix: str, integration: bool = False): current_suffix (str): The yml file name (HelloWorld or HelloWorldScript) integration (bool): Indicates if integration yml is being reformatted. """ - yml_dict = self.get_yml_data_as_dict(file_path=os.path.join(self.full_output_path, f"{current_suffix}.yml")) + with open(os.path.join(self.full_output_path, f"{current_suffix}.yml")) as f: + yml_dict = yaml.load(f, Loader=yamlordereddictloader.SafeLoader) yml_dict["commonfields"]["id"] = self.id yml_dict['name'] = self.id if integration: diff --git a/demisto_sdk/commands/init/tests/initiator_test.py b/demisto_sdk/commands/init/tests/initiator_test.py index 0b14f31f4f..f5dc5f3b43 100644 --- a/demisto_sdk/commands/init/tests/initiator_test.py +++ b/demisto_sdk/commands/init/tests/initiator_test.py @@ -1,9 +1,12 @@ import os -from collections import deque +from collections import OrderedDict, deque from datetime import datetime +from pathlib import Path from typing import Callable import pytest +import yaml +import yamlordereddictloader from demisto_sdk.commands.common.constants import (INTEGRATION_CATEGORIES, PACK_INITIAL_VERSION, PACK_SUPPORT_OPTIONS) @@ -17,7 +20,7 @@ PACK_URL = 'https://www.github.com/pack' PACK_EMAIL = 'author@mail.com' PACK_TAGS = 'Tag1,Tag2' -PACK_PRICE = '0' +# PACK_PRICE = '0' @pytest.fixture @@ -61,6 +64,7 @@ def test_get_object_id(monkeypatch, initiator): assert initiator.id == 'SomeIntegrationID' +# TODO: add the validation for price after #23546 is ready. def test_create_metadata(monkeypatch, initiator): # test create_metadata without user filling manually pack_metadata = initiator.create_metadata(False) @@ -82,7 +86,7 @@ def test_create_metadata(monkeypatch, initiator): 'certification': 'certified', 'useCases': [], 'keywords': [], - 'price': '0', + # 'price': '0', 'dependencies': {}, } @@ -92,7 +96,8 @@ def test_create_metadata(monkeypatch, initiator): generate_multiple_inputs( deque([ PACK_NAME, PACK_DESC, '1', PACK_SERVER_MIN_VERSION, PACK_AUTHOR, - PACK_URL, PACK_EMAIL, '1', PACK_TAGS, PACK_PRICE + PACK_URL, PACK_EMAIL, '1', PACK_TAGS + # PACK_PRICE ]) ) ) @@ -109,7 +114,7 @@ def test_create_metadata(monkeypatch, initiator): 'email': PACK_EMAIL, 'keywords': [], 'name': PACK_NAME, - 'price': PACK_PRICE, + # 'price': PACK_PRICE, 'serverMinVersion': PACK_SERVER_MIN_VERSION, 'support': PACK_SUPPORT_OPTIONS[0], 'tags': ['Tag1', 'Tag2'], @@ -143,3 +148,37 @@ def test_create_new_directory(mocker, monkeypatch, initiator): # fail to create pack cause of existing dir without overriding it monkeypatch.setattr('builtins.input', lambda _: 'N') assert initiator.create_new_directory() is False + + +def test_yml_reformatting(tmp_path, initiator): + integration_id = 'HelloWorld' + initiator.id = integration_id + d = tmp_path / integration_id + d.mkdir() + full_output_path = Path(d) + initiator.full_output_path = full_output_path + + p = d / f'{integration_id}.yml' + with p.open(mode='w') as f: + yaml.dump( + { + 'commonfields': { + 'id': '' + }, + 'name': '', + 'display': '' + }, + f + ) + dir_name = 'HelloWorldTest' + initiator.dir_name = dir_name + initiator.yml_reformatting(current_suffix=initiator.HELLO_WORLD_INTEGRATION, integration=True) + with open(full_output_path / f'{dir_name}.yml', 'r') as f: + yml_dict = yaml.load(f, Loader=yamlordereddictloader.SafeLoader) + assert yml_dict == OrderedDict({ + 'commonfields': OrderedDict({ + 'id': 'HelloWorld' + }), + 'display': 'HelloWorld', + 'name': 'HelloWorld' + }) diff --git a/demisto_sdk/tests/test_files/pack_metadata_empty_category.json b/demisto_sdk/tests/test_files/pack_metadata_empty_category.json new file mode 100644 index 0000000000..5f4b27b978 --- /dev/null +++ b/demisto_sdk/tests/test_files/pack_metadata_empty_category.json @@ -0,0 +1,28 @@ +{ + "name": "AWS Feed", + "description": "Indicators feed from AWS", + "support": "Cortex XSOAR", + "serverMinVersion": "5.5.0", + "currentVersion": "1.0.0", + "author": "Cortex XSOAR", + "url": "https://www.paloaltonetworks.com/cortex", + "email": "", + "categories": [""], + "tags": [], + "created": "2020-03-09T16:04:45Z", + "beta": false, + "deprecated": false, + "certification": "certified", + "useCases": [], + "keywords": ["AWS", "Feed"], + "price": 0, + "dependencies": { + "Base": { + "mandatory": true, + "minVersion": "1.0.0", + "name": "Base", + "certification": "certified", + "author": "Cortex XSOAR" + } + } +} diff --git a/demisto_sdk/tests/test_files/pack_metadata_invalid_dependencies.json b/demisto_sdk/tests/test_files/pack_metadata_invalid_dependencies.json new file mode 100644 index 0000000000..a49031aebe --- /dev/null +++ b/demisto_sdk/tests/test_files/pack_metadata_invalid_dependencies.json @@ -0,0 +1,29 @@ +{ + "name": "AWS Feed", + "description": "Indicators feed from AWS", + "support": "Cortex XSOAR", + "serverMinVersion": "5.5.0", + "currentVersion": "1.0.0", + "author": "Cortex XSOAR", + "url": "https://www.paloaltonetworks.com/cortex", + "email": "", + "categories": [ + "Data Enrichment & Threat Intelligence" + ], + "tags": [""], + "created": "2020-03-09T16:04:45Z", + "beta": false, + "deprecated": false, + "certification": "certified", + "useCases": [], + "keywords": ["AWS", "Feed", "test"], + "price": 0, + "dependencies": { + "Base": { + "mandatory": true, + "minVersion": "1.0.0", + "name": "Base", + "certification": "certified" + } + } +} diff --git a/demisto_sdk/tests/test_files/pack_metadata_invalid_keywords.json b/demisto_sdk/tests/test_files/pack_metadata_invalid_keywords.json new file mode 100644 index 0000000000..6c653ffe65 --- /dev/null +++ b/demisto_sdk/tests/test_files/pack_metadata_invalid_keywords.json @@ -0,0 +1,30 @@ +{ + "name": "AWS Feed", + "description": "Indicators feed from AWS", + "support": "Cortex XSOAR", + "serverMinVersion": "5.5.0", + "currentVersion": "1.0.0", + "author": "Cortex XSOAR", + "url": "https://www.paloaltonetworks.com/cortex", + "email": "", + "categories": [ + "Data Enrichment & Threat Intelligence" + ], + "tags": [], + "created": "2020-03-09T16:04:45Z", + "beta": false, + "deprecated": false, + "certification": "certified", + "useCases": [], + "keywords": [""], + "price": 0, + "dependencies": { + "Base": { + "mandatory": true, + "minVersion": "1.0.0", + "name": "Base", + "certification": "certified", + "author": "Cortex XSOAR" + } + } +} diff --git a/demisto_sdk/tests/test_files/pack_metadata_list_dependencies.json b/demisto_sdk/tests/test_files/pack_metadata_list_dependencies.json new file mode 100644 index 0000000000..8880bcf135 --- /dev/null +++ b/demisto_sdk/tests/test_files/pack_metadata_list_dependencies.json @@ -0,0 +1,30 @@ +{ + "name": "AWS Feed", + "description": "Indicators feed from AWS", + "support": "Cortex XSOAR", + "serverMinVersion": "5.5.0", + "currentVersion": "1.0.0", + "author": "Cortex XSOAR", + "url": "https://www.paloaltonetworks.com/cortex", + "email": "", + "categories": [ + "Data Enrichment & Threat Intelligence" + ], + "tags": [], + "created": "2020-03-09T16:04:45Z", + "beta": false, + "deprecated": false, + "certification": "certified", + "useCases": [], + "keywords": ["AWS", "Feed"], + "price": 0, + "dependencies": [{ + "Base": { + "mandatory": true, + "minVersion": "1.0.0", + "name": "Base", + "certification": "certified", + "author": "Cortex XSOAR" + } + }] +}