示例#1
0
    def build_custom_content_object(self, file_path: str) -> dict:
        """
        Build the custom content object represents a custom content entity instance.
        For example: integration-HelloWorld.yml downloaded from Demisto.
        """
        file_data, file_ending = get_dict_from_file(
            file_path)  # For example: yml, for integration files
        file_type = find_type(
            path=file_path, _dict=file_data,
            file_type=file_ending)  # For example: integration
        if file_type:
            file_type = file_type.value

        file_entity = self.file_type_to_entity(
            file_data, file_type)  # For example: Integrations
        file_id: str = get_entity_id_by_entity_type(file_data, file_entity)
        file_name: str = get_entity_name_by_entity_type(file_data, file_entity)

        custom_content_object: dict = {
            'id': file_id,
            'name': file_name,
            'path': file_path,
            'entity': file_entity,
            'type': file_type,
            'file_ending': file_ending,
        }

        file_code_language = get_code_lang(file_data, file_entity)
        if file_code_language:
            custom_content_object['code_lang'] = file_code_language

        return custom_content_object
示例#2
0
    def get_main_file_details(content_entity: str,
                              entity_instance_path: str) -> tuple:
        """
        Returns the details of the "main" file within an entity instance.
        For example: In the HelloWorld integration under Packs/HelloWorld, the main file is the yml file.
        It contains all relevant ids and names for all the files under the HelloWorld integration dir.
        :param content_entity: The content entity, for example Integrations
        :param entity_instance_path: For example: ~/.../content/Packs/TestPack/Integrations/HelloWorld
        :return: The main file id & name
        """
        main_file_data: dict = dict()
        main_file_path: str = str()

        # Entities which contain yml files
        if content_entity in (INTEGRATIONS_DIR, SCRIPTS_DIR, PLAYBOOKS_DIR,
                              TEST_PLAYBOOKS_DIR):
            if os.path.isdir(entity_instance_path):
                _, main_file_path = get_yml_paths_in_dir(entity_instance_path)
            elif os.path.isfile(entity_instance_path):
                main_file_path = entity_instance_path

            if main_file_path:
                main_file_data = get_yaml(main_file_path)

        # Entities which are json files (md files are ignored - changelog/readme)
        else:
            if os.path.isfile(entity_instance_path) and retrieve_file_ending(
                    entity_instance_path) == 'json':
                main_file_data = get_json(entity_instance_path)

        main_id = get_entity_id_by_entity_type(main_file_data, content_entity)
        main_name = get_entity_name_by_entity_type(main_file_data,
                                                   content_entity)

        return main_id, main_name
示例#3
0
 def update_tests(self) -> None:
     """
     If there are no tests configured: Prompts a question to the cli that asks the user whether he wants to add
     'No tests' under 'tests' key or not and format the file according to the answer
     """
     if not self.data.get('tests', ''):
         # try to get the test playbook files from the TestPlaybooks dir in the pack
         pack_path = os.path.dirname(os.path.dirname(os.path.abspath(self.source_file)))
         test_playbook_dir_path = os.path.join(pack_path, TEST_PLAYBOOKS_DIR)
         test_playbook_ids = []
         try:
             test_playbooks_files = os.listdir(test_playbook_dir_path)
             if test_playbooks_files:
                 for file_path in test_playbooks_files:  # iterate over the test playbooks in the dir
                     is_yml_file = file_path.endswith('.yml')
                     # concat as we might not be in content repo
                     tpb_file_path = os.path.join(test_playbook_dir_path, file_path)
                     if is_yml_file and find_type(tpb_file_path) == FileType.TEST_PLAYBOOK:
                         test_playbook_data = get_yaml(tpb_file_path)
                         test_playbook_id = get_entity_id_by_entity_type(test_playbook_data,
                                                                         content_entity='')
                         test_playbook_ids.append(test_playbook_id)
                 self.data['tests'] = test_playbook_ids
         except FileNotFoundError:
             pass
         if not test_playbook_ids:
             should_modify_yml_tests = click.confirm(f'The file {self.source_file} has no test playbooks '
                                                     f'configured. Do you want to configure it with "No tests"?')
             if should_modify_yml_tests:
                 click.echo(f'Formatting {self.output_file} with "No tests"')
                 self.data['tests'] = ['No tests (auto formatted)']
示例#4
0
 def test_get_entity_id_by_entity_type(self, data, entity):
     assert get_entity_id_by_entity_type(data, entity) == 1
示例#5
0
    def update_tests(self) -> None:
        """
        If there are no tests configured: Prompts a question to the cli that asks the user whether he wants to add
        'No tests' under 'tests' key or not and format the file according to the answer
        """
        if not self.data.get('tests', ''):
            # try to get the test playbook files from the TestPlaybooks dir in the pack
            pack_path = os.path.dirname(
                os.path.dirname(os.path.abspath(self.source_file)))
            test_playbook_dir_path = os.path.join(pack_path,
                                                  TEST_PLAYBOOKS_DIR)
            test_playbook_ids = []
            file_entity_type = find_type(self.source_file,
                                         _dict=self.data,
                                         file_type='yml')
            file_id = get_entity_id_by_entity_type(
                self.data, ENTITY_TYPE_TO_DIR.get(file_entity_type.value, ""))
            commands, scripts = get_scripts_and_commands_from_yml_data(
                self.data, file_entity_type)
            commands_names = [command.get('id') for command in commands]
            try:
                # Collecting the test playbooks
                test_playbooks_files = [
                    tpb_file
                    for tpb_file in listdir_fullpath(test_playbook_dir_path)
                    if find_type(tpb_file) == FileType.TEST_PLAYBOOK
                ]
                for tpb_file_path in test_playbooks_files:  # iterate over the test playbooks in the dir
                    test_playbook_data = get_yaml(tpb_file_path)
                    test_playbook_id = get_entity_id_by_entity_type(
                        test_playbook_data, content_entity='')
                    if not scripts and not commands:  # Better safe than sorry
                        test_playbook_ids.append(test_playbook_id)
                    else:
                        added = False
                        tpb_commands, tpb_scripts = get_scripts_and_commands_from_yml_data(
                            test_playbook_data, FileType.TEST_PLAYBOOK)

                        for tpb_command in tpb_commands:
                            tpb_command_name = tpb_command.get('id')
                            tpb_command_source = tpb_command.get('source', '')
                            if tpb_command_source and file_id and file_id != tpb_command_source:
                                continue

                            if not added and tpb_command_name in commands_names:
                                command_source = commands[commands_names.index(
                                    tpb_command_name)].get('source', '')
                                if command_source == tpb_command_source or command_source == '':
                                    test_playbook_ids.append(test_playbook_id)
                                    added = True
                                    break

                        if not added:
                            for tpb_script in tpb_scripts:
                                if tpb_script in scripts:
                                    test_playbook_ids.append(test_playbook_id)
                                    break

                self.data['tests'] = test_playbook_ids
            except FileNotFoundError:
                pass

            if not test_playbook_ids:
                # In case no_interactive flag was given - modify the tests without confirmation
                if self.assume_yes or not self.add_tests:
                    should_modify_yml_tests = True
                else:
                    should_modify_yml_tests = click.confirm(
                        f'The file {self.source_file} has no test playbooks '
                        f'configured. Do you want to configure it with "No tests"?'
                    )
                if should_modify_yml_tests:
                    click.echo(
                        f'Formatting {self.output_file} with "No tests"')
                    self.data['tests'] = ['No tests (auto formatted)']