def validate_added_files(self, added_files): """Validate the added files from your branch. In case we encounter an invalid file we set the self._is_valid param to False. Args: added_files (set): A set of the modified files in the current branch. """ for file_path in added_files: print("Validating {}".format(file_path)) structure_validator = StructureValidator(file_path, is_added_file=True) if not structure_validator.is_file_valid(): self._is_valid = False if not self.id_set_validator.is_file_valid_in_set(file_path): self._is_valid = False if self.id_set_validator.is_file_has_used_id(file_path): self._is_valid = False if re.match(TEST_PLAYBOOK_REGEX, file_path, re.IGNORECASE): if not self.conf_json_validator.is_test_in_conf_json(collect_ids(file_path)): self._is_valid = False elif re.match(INTEGRATION_REGEX, file_path, re.IGNORECASE) or \ re.match(INTEGRATION_YML_REGEX, file_path, re.IGNORECASE) or \ re.match(IMAGE_REGEX, file_path, re.IGNORECASE): image_validator = ImageValidator(file_path) if not image_validator.is_valid(): self._is_valid = False description_validator = DescriptionValidator(file_path) if not description_validator.is_valid(): self._is_valid = False integration_validator = IntegrationValidator(file_path) if not integration_validator.is_valid_integration(): self._is_valid = False elif re.match(BETA_INTEGRATION_REGEX, file_path, re.IGNORECASE) or \ re.match(BETA_INTEGRATION_YML_REGEX, file_path, re.IGNORECASE): description_validator = DescriptionValidator(file_path) if not description_validator.is_valid_beta_description(): self._is_valid = False integration_validator = IntegrationValidator(file_path) if not integration_validator.is_valid_beta_integration(is_new=True): self._is_valid = False elif re.match(IMAGE_REGEX, file_path, re.IGNORECASE): image_validator = ImageValidator(file_path) if not image_validator.is_valid(): self._is_valid = False elif re.match(INCIDENT_FIELD_REGEX, file_path, re.IGNORECASE) or \ re.match(INCIDENT_FIELDS_REGEX, file_path, re.IGNORECASE): incident_field_validator = IncidentFieldValidator(file_path) if not incident_field_validator.is_valid(): self._is_valid = False
def test_valid_new_beta_integration(): validator = IntegrationValidator("temp_file", check_git=False) validator.old_integration = {} validator.current_integration = { "commonfields": { "id": "newIntegration" }, "name": "newIntegration", "display": "newIntegration (Beta)", "beta": True, } assert validator.is_valid_beta_integration(is_new=True) is True, \ "The Beta validator did not validate a new valid integration"
def test_new_beta_integration_missing_beta_in_display(): validator = IntegrationValidator("temp_file", check_git=False) validator.old_integration = {} validator.current_integration = { "commonfields": { "id": "newIntegration" }, "name": "newIntegration", "display": "newIntegration", "beta": True, } assert validator.is_valid_beta_integration(is_new=True) is False, \ "The Beta validator approved the integration" \ "but it should have fail it for missing beta substring in 'display' field"
def test_new_beta_integration_with_beta_substring_in_name(): validator = IntegrationValidator("temp_file", check_git=False) validator.old_integration = {} validator.current_integration = { "commonfields": { "id": "newIntegration" }, "name": "newIntegration beta", "display": "newIntegration (Beta)", "beta": True, } assert validator.is_valid_beta_integration(is_new=True) is False, \ "The beta validator approved the new beta integration," \ " but it should fail it because the 'name' field has a 'beta' substring in it. " \ "the validator should not allow it for new integration"
def test_changed_beta_integration_without_beta_field(): validator = IntegrationValidator("temp_file", check_git=False) validator.old_integration = { "commonfields": { "id": "newIntegration beta" }, "name": "newIntegration beta", "display": "newIntegration (Beta)", } validator.current_integration = { "commonfields": { "id": "newIntegration beta" }, "name": "newIntegration beta", "display": "newIntegration changed (Beta)", } assert validator.is_valid_beta_integration() is False, \ "The Beta validator approved the integration" \ "but it should have fail it because it is missing 'beta' field with the value true"
def test_cahnged_beta_integration_with_beta_substring_in_is_and_name(): validator = IntegrationValidator("temp_file", check_git=False) validator.old_integration = { "commonfields": { "id": "newIntegration beta" }, "name": "newIntegration beta", "display": "newIntegration (Beta)", "beta": True, } validator.current_integration = { "commonfields": { "id": "newIntegration beta" }, "name": "newIntegration beta", "display": "newIntegration changed (Beta)", "beta": True, } assert validator.is_valid_beta_integration() is True, \ "The Beta validator failed the integration" \ "but it should have approved"
def validate_modified_files(self, modified_files, is_backward_check=True, old_branch='master'): """Validate the modified files from your branch. In case we encounter an invalid file we set the self._is_valid param to False. Args: modified_files (set): A set of the modified files in the current branch. is_backward_check (bool): When set to True will run backward compatibility checks old_branch (str): Old git branch to compare backward compatibility check to """ for file_path in modified_files: old_file_path = None if isinstance(file_path, tuple): old_file_path, file_path = file_path is_python_file = FilesValidator.is_py_script_or_integration(file_path) print('Validating {}'.format(file_path)) if not checked_type(file_path): print_warning('- Skipping validation of non-content entity file.') continue structure_validator = StructureValidator(file_path, is_added_file=not (False or is_backward_check), is_renamed=old_file_path is not None) if not structure_validator.is_file_valid(): self._is_valid = False if not self.id_set_validator.is_file_valid_in_set(file_path): self._is_valid = False elif re.match(INTEGRATION_REGEX, file_path, re.IGNORECASE) or \ re.match(INTEGRATION_YML_REGEX, file_path, re.IGNORECASE): image_validator = ImageValidator(file_path) if not image_validator.is_valid(): self._is_valid = False description_validator = DescriptionValidator(file_path) if not description_validator.is_valid(): self._is_valid = False integration_validator = IntegrationValidator(file_path, old_file_path=old_file_path, old_git_branch=old_branch) if is_backward_check and not integration_validator.is_backward_compatible(): self._is_valid = False if not integration_validator.is_valid_integration(): self._is_valid = False if is_python_file: docker_image_validator = DockerImageValidator(file_path, is_modified_file=True) if not docker_image_validator.is_docker_image_valid(): self._is_valid = False elif re.match(BETA_INTEGRATION_REGEX, file_path, re.IGNORECASE) or \ re.match(BETA_INTEGRATION_YML_REGEX, file_path, re.IGNORECASE): description_validator = DescriptionValidator(file_path) if not description_validator.is_valid_beta_description(): self._is_valid = False integration_validator = IntegrationValidator(file_path, old_file_path=old_file_path) if not integration_validator.is_valid_beta_integration(): self._is_valid = False if is_python_file: docker_image_validator = DockerImageValidator(file_path, is_modified_file=True) if not docker_image_validator.is_docker_image_valid(): self._is_valid = False elif re.match(SCRIPT_REGEX, file_path, re.IGNORECASE): script_validator = ScriptValidator(file_path, old_file_path=old_file_path, old_git_branch=old_branch) if is_backward_check and not script_validator.is_backward_compatible(): self._is_valid = False if not script_validator.is_valid_script(): self._is_valid = False if is_python_file: docker_image_validator = DockerImageValidator(file_path, is_modified_file=True) if not docker_image_validator.is_docker_image_valid(): self._is_valid = False elif re.match(SCRIPT_YML_REGEX, file_path, re.IGNORECASE) or \ re.match(SCRIPT_PY_REGEX, file_path, re.IGNORECASE) or \ re.match(SCRIPT_JS_REGEX, file_path, re.IGNORECASE): yml_path, _ = get_script_package_data(os.path.dirname(file_path)) script_validator = ScriptValidator(yml_path, old_file_path=old_file_path, old_git_branch=old_branch) if is_backward_check and not script_validator.is_backward_compatible(): self._is_valid = False if is_python_file: docker_image_validator = DockerImageValidator(file_path, is_modified_file=True) if not docker_image_validator.is_docker_image_valid(): self._is_valid = False elif re.match(IMAGE_REGEX, file_path, re.IGNORECASE): image_validator = ImageValidator(file_path) if not image_validator.is_valid(): self._is_valid = False elif re.match(INCIDENT_FIELD_REGEX, file_path, re.IGNORECASE): incident_field_validator = IncidentFieldValidator(file_path, old_file_path=old_file_path, old_git_branch=old_branch) if not incident_field_validator.is_valid(): self._is_valid = False if is_backward_check and not incident_field_validator.is_backward_compatible(): self._is_valid = False