def update_conf_json(self, file_type: str) -> None: """ Updates conf.json with the file's test playbooks if not registered already according to user's answer to the prompt message Args: file_type: The typr of the file, can be integration, playbook or script """ test_playbooks = self.data.get('tests', []) if not test_playbooks: return no_test_playbooks_explicitly = any(test for test in test_playbooks if 'no test' in test.lower()) if no_test_playbooks_explicitly: return try: conf_json_content = self._load_conf_file() except FileNotFoundError: if self.verbose: click.secho( f'Unable to find {self.CONF_PATH} - skipping update.', fg='yellow') return conf_json_test_configuration = conf_json_content['tests'] content_item_id = _get_file_id(file_type, self.data) not_registered_tests = get_not_registered_tests( conf_json_test_configuration, content_item_id, file_type, test_playbooks) if not_registered_tests: not_registered_tests_string = '\n'.join(not_registered_tests) if self.assume_yes: should_edit_conf_json = True else: should_edit_conf_json = click.confirm( f'The following test playbooks are not configured in conf.json file ' f'{not_registered_tests_string}\n' f'Would you like to add them now?') if should_edit_conf_json: conf_json_content['tests'].extend( self.get_test_playbooks_configuration( not_registered_tests, content_item_id, file_type)) self._save_to_conf_json(conf_json_content) click.echo('Added test playbooks to conf.json successfully') else: click.echo('Skipping test playbooks configuration')
def _is_id_equals_name(self, file_type): """Validate that the id of the file equals to the name. Args: file_type (str): the file type. can be 'integration', 'script', 'playbook', 'dashboard', 'id' Returns: bool. Whether the file's id is equal to to its name """ file_id = _get_file_id(file_type, self.current_file) name = self.current_file.get('name', '') if file_id != name: print_error( "The File's name, which is: '{}', should be equal to its ID, which is: '{}'." " please update the file (path to file: {}).".format( name, file_id, self.file_path)) print_error(Errors.suggest_fix(self.file_path)) return False return True
def _is_id_equals_name(self, file_type): """Validate that the id of the file equals to the name. Args: file_type (str): the file type. can be 'integration', 'script', 'playbook', 'dashboard', 'id' Returns: bool. Whether the file's id is equal to to its name """ file_id = _get_file_id(file_type, self.current_file) name = self.current_file.get('name', '') if file_id != name: error_message, error_code = Errors.id_should_equal_name( name, file_id) if self.handle_error(error_message, error_code, file_path=self.file_path, suggested_fix=Errors.suggest_fix( self.file_path)): return False return True
def are_tests_registered_in_conf_json_file_or_yml_file( self, test_playbooks: list) -> bool: """ If the file is a test playbook: Validates it is registered in conf.json file If the file is an integration: Validating it is registered in conf.json file or that the yml file has 'No tests' under 'tests' key Args: test_playbooks: The yml file's list of test playbooks Returns: True if all test playbooks are configured in conf.json """ no_tests_explicitly = any(test for test in test_playbooks if 'no test' in test.lower()) if no_tests_explicitly: return True conf_json_tests = self._load_conf_file()['tests'] content_item_id = _get_file_id(self.structure_validator.scheme_name, self.current_file) file_type = self.structure_validator.scheme_name # Test playbook case if 'TestPlaybooks' in self.file_path and file_type == 'playbook': is_configured_test = any( test_config for test_config in conf_json_tests if is_test_config_match(test_config, test_playbook_id=content_item_id)) if not is_configured_test: missing_test_playbook_configurations = json.dumps( {'playbookID': content_item_id}, indent=4) missing_integration_configurations = json.dumps( { 'integrations': '<integration ID>', 'playbookID': content_item_id }, indent=4) error_message = \ f'The TestPlaybook {content_item_id} is not registered in {self.CONF_PATH} file.\n' \ f'Please add\n{missing_test_playbook_configurations}\n' \ f'or if this test playbook is for an integration\n{missing_integration_configurations}\n' \ f'to {self.CONF_PATH} path under \'tests\' key.' print_error(error_message) return False # Integration case elif file_type == 'integration': is_configured_test = any( test_config for test_config in conf_json_tests if is_test_config_match(test_config, integration_id=content_item_id)) if not is_configured_test: missing_test_playbook_configurations = json.dumps( { 'integrations': content_item_id, 'playbookID': '<TestPlaybook ID>' }, indent=4) no_tests_key = yaml.dump({'tests': ['No tests']}) error_message = \ f'The following integration is not registered in {self.CONF_PATH} file.\n' \ f'Please add\n{missing_test_playbook_configurations}\nto {self.CONF_PATH} ' \ f'path under \'tests\' key.\n' \ f'If you don\'t want to add a test playbook for this integration, ' \ f'please add \n{no_tests_key}to the ' \ f'file {self.file_path} or run \'demisto-sdk format -p {self.file_path}\'' print_error(error_message) return False return True
def are_tests_registered_in_conf_json_file_or_yml_file( self, test_playbooks: list) -> bool: """ If the file is a test playbook: Validates it is registered in conf.json file If the file is an integration: Validating it is registered in conf.json file or that the yml file has 'No tests' under 'tests' key Args: test_playbooks: The yml file's list of test playbooks Returns: True if all test playbooks are configured in conf.json """ no_tests_explicitly = any(test for test in test_playbooks if 'no test' in test.lower()) if no_tests_explicitly: return True conf_json_tests = self._load_conf_file()['tests'] file_type = self.structure_validator.scheme_name if not isinstance(file_type, str): file_type = file_type.value # type: ignore content_item_id = _get_file_id(file_type, self.current_file) # Test playbook case if file_type == 'testplaybook': is_configured_test = any( test_config for test_config in conf_json_tests if is_test_config_match(test_config, test_playbook_id=content_item_id)) if not is_configured_test: missing_test_playbook_configurations = json.dumps( {'playbookID': content_item_id}, indent=4) missing_integration_configurations = json.dumps( { 'integrations': '<integration ID>', 'playbookID': content_item_id }, indent=4) error_message, error_code = Errors.test_playbook_not_configured( content_item_id, missing_test_playbook_configurations, missing_integration_configurations) if self.handle_error(error_message, error_code, file_path=self.file_path): return False # Integration case elif file_type == 'integration': is_configured_test = any( test_config for test_config in conf_json_tests if is_test_config_match(test_config, integration_id=content_item_id)) if not is_configured_test: missing_test_playbook_configurations = json.dumps( { 'integrations': content_item_id, 'playbookID': '<TestPlaybook ID>' }, indent=4) no_tests_key = yaml.dump({'tests': ['No tests']}) error_message, error_code = Errors.integration_not_registered( self.file_path, missing_test_playbook_configurations, no_tests_key) if self.handle_error(error_message, error_code, file_path=self.file_path): return False return True