Beispiel #1
0
def validate(config, **kwargs):
    sys.path.append(config.configuration.env_dir)

    file_path = kwargs['path'] or kwargs['input']
    if file_path and not os.path.isfile(file_path) and not os.path.isdir(
            file_path):
        print_error(f'File {file_path} was not found')
        return 1
    else:
        is_external_repo = tools.is_external_repository()

        validator = ValidateManager(
            is_backward_check=not kwargs['no_backward_comp'],
            only_committed_files=kwargs['post_commit'],
            prev_ver=kwargs['prev_ver'],
            skip_conf_json=kwargs['no_conf_json'],
            use_git=kwargs['use_git'],
            file_path=file_path,
            validate_all=kwargs.get('validate_all'),
            validate_id_set=kwargs['id_set'],
            skip_pack_rn_validation=kwargs['skip_pack_release_notes'],
            print_ignored_errors=kwargs['print_ignored_errors'],
            is_external_repo=is_external_repo,
            print_ignored_files=kwargs['print_ignored_files'],
            no_docker_checks=kwargs['no_docker_checks'],
            silence_init_prints=kwargs['silence_init_prints'])
        return validator.run_validation()
def main():
    arg_parser = argparse.ArgumentParser()
    arg_parser.add_argument('version', help='Release version')
    arg_parser.add_argument('git_sha1', help='commit sha1 to compare changes with')
    arg_parser.add_argument('server_version', help='Server version')
    arg_parser.add_argument('-d', '--date', help='release date in the format %Y-%m-%d', required=False)
    args = arg_parser.parse_args()

    date = args.date if args.date else datetime.now().strftime('%Y-%m-%d')

    # get changed yaml/json files (filter only relevant changed files)
    validate_manager = ValidateManager()
    change_log = run_command('git diff --name-status {}'.format(args.git_sha1))
    modified_files, added_files, _, _, _ = validate_manager.filter_changed_files(change_log)

    for file_path in get_changed_content_entities(modified_files, added_files):
        if not should_clear(file_path, args.server_version):
            continue
        rn_path = get_release_notes_file_path(file_path)
        if os.path.isfile(rn_path):
            # if file exist, mark the current notes as release relevant
            with open(rn_path, 'r+') as rn_file:
                text = rn_file.read()
                rn_file.seek(0)
                text = text.replace(UNRELEASE_HEADER, CHANGE_LOG_FORMAT.format(version=args.version, date=date))
                rn_file.write(text)
        else:
            # if file doesn't exist, create it with new header
            with open(rn_path, 'w') as rn_file:
                text = CHANGE_LOG_FORMAT.format(version=args.version, date=date) + get_new_header(file_path)
                rn_file.write(text)
            run_command('git add {}'.format(rn_path))
def test_update_release_on_matadata_change(demisto_client, mocker, repo):
    """
    Given
    - change only in metadata

    When
    - Running demisto-sdk update-release-notes command.

    Then
    - Ensure not find changes which would belong in release notes .
    """
    pack = repo.create_pack('FeedAzureValid')
    pack.pack_metadata.write_json(open('demisto_sdk/tests/test_files/1.pack_metadata.json').read())
    validate_manager = ValidateManager(skip_pack_rn_validation=True,
                                       silence_init_prints=True, skip_conf_json=True, check_is_unskipped=False)
    validate_manager.git_util = "Not None"

    mocker.patch.object(UpdateRN, 'is_bump_required', return_value=True)
    mocker.patch.object(ValidateManager, 'get_unfiltered_changed_files_from_git',
                        return_value=({pack.pack_metadata.path}, set(), set()))
    mocker.patch.object(UpdateReleaseNotesManager, 'setup_validate_manager', return_value=validate_manager)
    mocker.patch.object(ValidateManager, 'setup_git_params', return_value='')

    mocker.patch.object(GitUtil, 'get_current_working_branch', return_value="branch_name")
    mocker.patch.object(UpdateRN, 'get_pack_metadata', return_value={'currentVersion': '1.0.0'})
    mocker.patch('demisto_sdk.commands.common.tools.get_pack_name', return_value='FeedAzureValid')
    mocker.patch('demisto_sdk.commands.common.tools.get_pack_names_from_files', return_value={'FeedAzureValid'})

    with ChangeCWD(repo.path):
        runner = CliRunner(mix_stderr=False)
        result = runner.invoke(main, [UPDATE_RN_COMMAND, "-g"])
    assert result.exit_code == 0
    assert 'No changes that require release notes were detected. If such changes were made, ' \
           'please commit the changes and rerun the command' in result.stdout
Beispiel #4
0
def run_validate(file_path: str, json_output_file: str) -> None:
    os.environ['DEMISTO_SDK_SKIP_VERSION_CHECK'] = '1'
    tests_dir = 'Tests'
    if not os.path.exists(tests_dir):
        os.makedirs(tests_dir)
    with open(f'{tests_dir}/id_set.json', 'w') as f:
        json.dump({}, f)
    v_manager = ValidateManager(is_backward_check=False,
                                prev_ver=None,
                                use_git=False,
                                only_committed_files=False,
                                print_ignored_files=False,
                                skip_conf_json=True,
                                validate_id_set=False,
                                file_path=file_path,
                                validate_all=False,
                                is_external_repo=False,
                                skip_pack_rn_validation=False,
                                print_ignored_errors=False,
                                silence_init_prints=False,
                                no_docker_checks=False,
                                skip_dependencies=False,
                                id_set_path=None,
                                staged=False,
                                json_file_path=json_output_file,
                                skip_schema_check=True,
                                create_id_set=False)
    v_manager.run_validation()
Beispiel #5
0
 def test_validate_no_old_format__without_toversion(self, mocker):
     """
         Given:
             - an old format_file without toversion
         When:
             - running validate_no_old_format on the file
         Then:
             - return a False as the file is invalid
     """
     mocker.patch.object(BaseValidator, "handle_error", return_value="not-a-non-string")
     validate_manager = ValidateManager()
     old_format_files = {"demisto_sdk/tests/test_files/script-valid.yml"}
     assert not validate_manager.validate_no_old_format(old_format_files)
Beispiel #6
0
 def test_validate_no_old_format__with_toversion(self):
     """
         Given:
             - an old format_file with toversion
         When:
             - running validate_no_old_format on the file
         Then:
             - return a True as the file is valid
     """
     validate_manager = ValidateManager()
     old_format_files = {"demisto_sdk/tests/test_files/Unifier/SampleScriptPackage/"
                         "script-SampleScriptPackageSanityDocker45_45.yml"}
     assert validate_manager.validate_no_old_format(old_format_files)
Beispiel #7
0
 def test_verify_no_dup_rn__validate_manager(self, added_files: set, expected: bool):
     """
         Given:
             - A list of added files
         When:
             - verifying there are no other new release notes.
         Then:
             - return a validation response
         Case 1: Release notes in different packs.
         Case 2: Release notes where one is in the same pack
     """
     validate_manager = ValidateManager(skip_conf_json=True)
     result = validate_manager.validate_no_duplicated_release_notes(added_files)
     assert result is expected
 def test_validate_invalid_pack_dependencies__validate_manager(self, ):
     """
         Given:
             - A file path with invalid pack dependencies
         When:
             - checking validity of pack dependencies for added or modified files
         Then:
             - return a False validation response
     """
     validate_manager = ValidateManager(skip_conf_json=True)
     id_set_path = os.path.normpath(
         os.path.join(__file__, git_path(), 'demisto_sdk', 'tests', 'test_files', 'id_set', 'id_set.json'))
     result = validate_manager.validate_pack_unique_files('QRadar', pack_error_ignore_list={},
                                                          id_set_path=id_set_path)
     assert not result
Beispiel #9
0
 def test_is_py_or_yml_invalid__validate_manager(self):
     """
         Given:
             - A file path which contains a python script in a legacy yml schema
         When:
             - verifying the yml is valid using validate manager
         Then:
             - return a False validation response
     """
     files_path = os.path.normpath(
         os.path.join(__file__, f'{git_path()}/demisto_sdk/tests', 'test_files'))
     test_file = os.path.join(files_path,
                              'UnifiedIntegrations/Integrations/integration-Symantec_Messaging_Gateway.yml')
     validate_manager = ValidateManager()
     res = validate_manager._is_py_script_or_integration(test_file)
     assert res is False
Beispiel #10
0
 def test_is_py_or_yml__validate_manager(self):
     """
         Given:
             - A file path which contains a python script
         When:
             - validating the associated yml file
         Then:
             - return a False validation response
     """
     files_path = os.path.normpath(
         os.path.join(__file__, f'{git_path()}/demisto_sdk/tests', 'test_files'))
     test_file = os.path.join(files_path, 'CortexXDR',
                              'Integrations/PaloAltoNetworks_XDR/PaloAltoNetworks_XDR.yml')
     validate_manager = ValidateManager()
     res = validate_manager._is_py_script_or_integration(test_file)
     assert res is False
Beispiel #11
0
    def __init__(self,
                 input: str = '',
                 output: str = '',
                 path: str = '',
                 from_version: str = '',
                 no_validate: bool = False,
                 verbose: bool = False,
                 assume_yes: bool = False,
                 interactive: bool = True,
                 clear_cache: bool = False,
                 **kwargs):
        self.source_file = input
        self.output_file = self.set_output_file_path(output)
        self.verbose = verbose
        _, self.relative_content_path = is_file_from_content_repo(
            self.output_file)
        self.old_file = self.is_old_file(
            self.relative_content_path
            if self.relative_content_path else self.output_file, self.verbose)
        self.schema_path = path
        self.from_version = from_version
        self.no_validate = no_validate
        self.assume_yes = assume_yes
        self.interactive = interactive
        self.updated_ids: Dict = {}
        if not self.no_validate:
            self.validate_manager = ValidateManager(
                silence_init_prints=True,
                skip_conf_json=True,
                skip_dependencies=True,
                skip_pack_rn_validation=True,
                check_is_unskipped=False,
                validate_id_set=False)

        if not self.source_file:
            raise Exception(
                'Please provide <source path>, <optional - destination path>.')
        try:
            self.data, self.file_type = get_dict_from_file(
                self.source_file, clear_cache=clear_cache)
        except Exception:
            raise Exception(
                F'Provided file {self.source_file} is not a valid file.')
        self.from_version_key = self.set_from_version_key_name()
        self.id_set_file, _ = get_dict_from_file(
            path=kwargs.get('id_set_path'))  # type: ignore[arg-type]
Beispiel #12
0
    def test_get_error_ignore_list__validate_manager(self, mocker):
        """
            Given:
                - A file path to pack ignore
            When:
                - running get_error_ignore_list from validate manager
            Then:
                - verify that the created ignored_errors list is correct
        """
        files_path = os.path.normpath(
            os.path.join(__file__, f'{git_path()}/demisto_sdk/tests', 'test_files'))
        test_file = os.path.join(files_path, 'fake_pack/.pack-ignore')

        mocker.patch.object(ValidateManager, 'get_pack_ignore_file_path', return_value=test_file)

        validate_manager = ValidateManager()
        ignore_errors_list = validate_manager.get_error_ignore_list("fake")
        assert ignore_errors_list['file_name'] == ['BA101', 'IF107']
        assert 'SC100' not in ignore_errors_list['file_name']
Beispiel #13
0
 def test_validate_no_missing_release_notes__missing_rn(self, repo):
     """
         Given:
             - 2 packs with modified files and release notes for only one
         When:
             - running validate_no_missing_release_notes on the files
         Then:
             - return a False as there are release notes missing
     """
     pack1 = repo.create_pack('PackName1')
     incident_field1 = pack1.create_incident_field('incident-field', content=INCIDENT_FIELD)
     pack2 = repo.create_pack('PackName2')
     incident_field2 = pack2.create_incident_field('incident-field', content=INCIDENT_FIELD)
     validate_manager = ValidateManager()
     modified_files = {incident_field1.get_path_from_pack(),
                       incident_field2.get_path_from_pack()}
     added_files = {'Packs/PackName1/ReleaseNotes/1_0_0.md'}
     with ChangeCWD(repo.path):
         assert validate_manager.validate_no_missing_release_notes(modified_files, added_files) is False
    def test_setup_git_params(self, mocker):
        mocker.patch.object(ValidateManager, 'get_content_release_identifier', return_value='')

        mocker.patch.object(ValidateManager, 'get_current_working_branch', return_value='20.0.7')
        validate_manager = ValidateManager()
        validate_manager.setup_git_params()

        assert validate_manager.always_valid
        assert validate_manager.compare_type == '..'

        mocker.patch.object(ValidateManager, 'get_current_working_branch', return_value='master')
        # resetting always_valid flag
        validate_manager.always_valid = False
        validate_manager.setup_git_params()
        assert not validate_manager.always_valid
        assert validate_manager.compare_type == '..'

        mocker.patch.object(ValidateManager, 'get_current_working_branch', return_value='not-master-branch')
        validate_manager.setup_git_params()
        assert not validate_manager.always_valid
        assert validate_manager.compare_type == '...'
Beispiel #15
0
 def setup_validate_manager(self):
     return ValidateManager(skip_pack_rn_validation=True, prev_ver=self.prev_ver,
                            silence_init_prints=True, skip_conf_json=True, check_is_unskipped=False,
                            file_path=self.given_pack)
Beispiel #16
0
 def test_files_validator_validate_pack_unique_files__validate_manager(self, ):
     validate_manager = ValidateManager(skip_conf_json=True)
     result = validate_manager.validate_pack_unique_files(VALID_PACK, pack_error_ignore_list={})
     assert result
Beispiel #17
0
    def test_filter_changed_files(self, mocker):
        """
            Given:
                - A string of git diff results
            When:
                - running filter_changed_files on the string
            Then:
                - Ensure the modified files are recognized correctly.
                - Ensure the added files are recognized correctly.
                - Ensure the renamed file is in a tup;e in the modified files.
                - Ensure modified metadata files are in the changed_meta_files and that the added one is not.
                - Ensure the added code and meta files are not in added files.
                - Ensure old format file is recognized correctly.
                - Ensure deleted file is recognized correctly.
                - Ensure ignored files are set correctly.
        """
        mocker.patch.object(os.path, 'isfile', return_value=True)
        mocker.patch.object(ValidateManager, '_is_py_script_or_integration', return_value=True)
        diff_string = "M	Packs/CommonTypes/IncidentFields/incidentfield-Detection_URL.json\n" \
                      "M	Packs/EWS/Classifiers/classifier-EWS_v2.json\n" \
                      "M	Packs/Elasticsearch/Integrations/Elasticsearch_v2/Elasticsearch_v2.py\n" \
                      "M	Packs/Elasticsearch/Integrations/integration-Elasticsearch.yml\n" \
                      "M	Packs/F5/pack_metadata.json\n"\
                      "R100	Packs/EclecticIQ/Integrations/EclecticIQ/EclecticIQ.yml	" \
                      "Packs/EclecticIQ/Integrations/EclecticIQ_new/EclecticIQ_new.yml\n" \
                      "A	Packs/MyNewPack/.pack-ignore\n" \
                      "A	Packs/MyNewPack/.secrets-ignore\n" \
                      "A	Packs/MyNewPack/Integrations/MyNewIntegration/MyNewIntegration.py\n" \
                      "A	Packs/MyNewPack/Integrations/MyNewIntegration/MyNewIntegration.yml\n" \
                      "A	Packs/MyNewPack/Integrations/MyNewIntegration/MyNewIntegration_description.md\n" \
                      "A	Packs/MyNewPack/Integrations/MyNewIntegration/MyNewIntegration_image.png\n" \
                      "A	Packs/MyNewPack/Integrations/MyNewIntegration/MyNewIntegration_test.py\n" \
                      "A	Packs/MyNewPack/Integrations/MyNewIntegration/Pipfile\n" \
                      "A	Packs/MyNewPack/Integrations/MyNewIntegration/Pipfile.lock\n" \
                      "A	Packs/MyNewPack/Integrations/MyNewIntegration/README.md\n" \
                      "A	Packs/MyNewPack/README.md\n" \
                      "A	Packs/MyNewPack/pack_metadata.json\n" \
                      "D	Packs/DeprecatedContent/Scripts/script-ExtractURL.yml"

        validate_manager = ValidateManager()
        modified_files, added_files, deleted_files, old_format_files, changed_meta_files = validate_manager.\
            filter_changed_files(files_string=diff_string, print_ignored_files=True)

        # checking that modified files are recognized correctly
        assert 'Packs/CommonTypes/IncidentFields/incidentfield-Detection_URL.json' in modified_files
        assert 'Packs/EWS/Classifiers/classifier-EWS_v2.json' in modified_files
        assert ('Packs/EclecticIQ/Integrations/EclecticIQ/EclecticIQ.yml',
                'Packs/EclecticIQ/Integrations/EclecticIQ_new/EclecticIQ_new.yml') in modified_files

        # check that the modified code file is not there but the yml file is
        assert 'Packs/Elasticsearch/Integrations/Elasticsearch_v2/Elasticsearch_v2.yml' in modified_files
        assert 'Packs/Elasticsearch/Integrations/Elasticsearch_v2/Elasticsearch_v2.py' not in modified_files

        # check that the modified metadata file is in the changed_meta_files but the added one is not
        assert 'Packs/F5/pack_metadata.json' in changed_meta_files
        assert 'Packs/MyNewPack/pack_metadata.json' not in changed_meta_files

        # check that the added files are recognized correctly
        assert 'Packs/MyNewPack/Integrations/MyNewIntegration/README.md' in added_files
        assert 'Packs/MyNewPack/Integrations/MyNewIntegration/MyNewIntegration.yml' in added_files

        # check that the added code files and meta file are not in the added_files
        assert 'Packs/MyNewPack/Integrations/MyNewIntegration/MyNewIntegration.py' not in added_files
        assert 'Packs/MyNewPack/Integrations/MyNewIntegration/MyNewIntegration_test.py' not in added_files
        assert 'Packs/MyNewPack/pack_metadata.json' not in added_files

        # check that non-image, pipfile, description or schema are in the ignored files and the rest are
        assert 'Packs/MyNewPack/Integrations/MyNewIntegration/Pipfile' not in validate_manager.ignored_files
        assert 'Packs/MyNewPack/Integrations/MyNewIntegration/Pipfile.lock' not in validate_manager.ignored_files
        assert 'Packs/MyNewPack/Integrations/MyNewIntegration/MyNewIntegration_description.md' not \
               in validate_manager.ignored_files
        assert 'Packs/MyNewPack/Integrations/MyNewIntegration/MyNewIntegration_image.png' not \
               in validate_manager.ignored_files
        assert 'Packs/MyNewPack/.secrets-ignore' in validate_manager.ignored_files
        assert 'Packs/MyNewPack/Integrations/MyNewIntegration/MyNewIntegration_test.py' in \
               validate_manager.ignored_files
        assert 'Packs/MyNewPack/.pack-ignore' in validate_manager.ignored_files

        # check recognized old-format file
        assert 'Packs/Elasticsearch/Integrations/integration-Elasticsearch.yml' in old_format_files

        # check recognized deleted file
        assert 'Packs/DeprecatedContent/Scripts/script-ExtractURL.yml' in deleted_files
Beispiel #18
0
 def test_create_ignored_errors_list__validate_manager(self):
     validate_manager = ValidateManager()
     errors_to_check = ["IN", "SC", "CJ", "DA", "DB", "DO", "ID", "DS", "IM", "IF", "IT", "RN", "RM", "PA", "PB",
                        "WD", "RP", "BA100", "BC100", "ST", "CL", "MP"]
     ignored_list = validate_manager.create_ignored_errors_list(errors_to_check)
     assert ignored_list == ["BA101", "BA102", "BA103", "BA104", "BC101", "BC102", "BC103", "BC104"]