Esempio n. 1
0
def test_has_remote(mocker, git_value, response):
    """
    While: Testing if the remote upstream contains demisto/content
    Given:
      1. Origin string not containing demisto/content
      2. Upstream string containing demisto/content
    Expects:
      1. Test condition fails
      2. Test condition passes
    :param git_value: Git string from `git remotes -v`
    """
    mocker.patch('demisto_sdk.commands.common.tools.run_command', return_value=git_value)
    test_remote = has_remote_configured()
    assert response == test_remote
Esempio n. 2
0
def get_modified_and_added_files(compare_type,
                                 prev_ver,
                                 ignored_errors=dict(),
                                 no_configuration_prints=False,
                                 staged=False,
                                 print_ignored_files=False,
                                 is_circle=False,
                                 branch_name=None):
    """Get the modified and added files from a specific branch

    Args:
        is_circle (bool): Whether the code runs on circle build.
        print_ignored_files (bool): Whether to print ignored files.
        staged (bool): Whether to return only staged files
        no_configuration_prints (bool): Whether to print additional config prints
        ignored_errors (dict): A dict of ignored errors per file.
        branch_name (str): the branch name
        compare_type (str): whether to run diff with two dots (..) or three (...)
        prev_ver (str): Against which branch to run the comparision - master/last release

    Returns:
        tuple. 3 sets representing modified files, added files and files of old format who have changed.
    """
    if not branch_name:
        branch_name = get_current_working_branch()
    base_validator = BaseValidator(ignored_errors=ignored_errors)
    if not no_configuration_prints:
        if staged:
            click.echo("Collecting staged files only")
        else:
            click.echo("Collecting all committed files")

    prev_ver = add_origin(branch_name, prev_ver)
    # all committed changes of the current branch vs the prev_ver
    all_committed_files_string = run_command(
        f'git diff --name-status {prev_ver}{compare_type}refs/heads/{branch_name}'
    )

    modified_files, added_files, _, old_format_files, changed_meta_files, ignored_files, new_packs = \
        filter_changed_files(all_committed_files_string, prev_ver, print_ignored_files=print_ignored_files)

    if not is_circle:
        remote_configured = has_remote_configured()
        is_origin_demisto = is_origin_content_repo()
        if remote_configured and not is_origin_demisto:
            if not no_configuration_prints:
                click.echo(
                    "Collecting all local changed files from fork against the content master"
                )

            # only changes against prev_ver (without local changes)

            all_changed_files_string = run_command(
                'git diff --name-status upstream/master...HEAD')
            modified_files_from_tag, added_files_from_tag, _, _, changed_meta_files_from_tag, \
                ignored_files_from_tag, new_packs_from_tag = \
                filter_changed_files(all_changed_files_string, print_ignored_files=print_ignored_files)

            # all local non-committed changes and changes against prev_ver
            outer_changes_files_string = run_command(
                'git diff --name-status --no-merges upstream/master...HEAD')
            nc_modified_files, nc_added_files, nc_deleted_files, nc_old_format_files, nc_changed_meta_files, \
                nc_ignored_files, nc_new_packs = \
                filter_changed_files(outer_changes_files_string, print_ignored_files=print_ignored_files)

        else:
            if (not is_origin_demisto
                    and not remote_configured) and not no_configuration_prints:
                error_message, error_code = Errors.changes_may_fail_validation(
                )
                base_validator.handle_error(error_message,
                                            error_code,
                                            file_path="General-Error",
                                            warning=True,
                                            drop_line=True)

            if not no_configuration_prints and not staged:
                click.echo(
                    "Collecting all local changed files against the content master"
                )

            # only changes against prev_ver (without local changes)
            all_changed_files_string = run_command(
                'git diff --name-status {}'.format(prev_ver))
            modified_files_from_tag, added_files_from_tag, _, _, changed_meta_files_from_tag, \
                ignored_files_from_tag, new_packs_from_tag = \
                filter_changed_files(all_changed_files_string, print_ignored_files=print_ignored_files)

            # all local non-committed changes and changes against prev_ver
            outer_changes_files_string = run_command(
                'git diff --name-status --no-merges HEAD')
            nc_modified_files, nc_added_files, nc_deleted_files, nc_old_format_files, nc_changed_meta_files, \
                nc_ignored_files, nc_new_packs = \
                filter_changed_files(outer_changes_files_string, print_ignored_files=print_ignored_files)

        old_format_files = old_format_files.union(nc_old_format_files)
        modified_files = modified_files.union(
            modified_files_from_tag.intersection(nc_modified_files))

        added_files = added_files.union(
            added_files_from_tag.intersection(nc_added_files))

        changed_meta_files = changed_meta_files.union(
            changed_meta_files_from_tag.intersection(nc_changed_meta_files))

        ignored_files = ignored_files.union(
            ignored_files_from_tag.intersection(nc_ignored_files))

        new_packs = new_packs.union(
            new_packs_from_tag.intersection(nc_new_packs))

        modified_files = modified_files - set(nc_deleted_files)
        added_files = added_files - set(nc_deleted_files)
        changed_meta_files = changed_meta_files - set(nc_deleted_files)

    if staged:
        modified_files, added_files, old_format_files, changed_meta_files = \
            filter_staged_only(modified_files, added_files, old_format_files, changed_meta_files)

    modified_packs = get_packs(modified_files).union(
        get_packs(old_format_files)).union(get_packs(added_files))
    return modified_files, added_files, old_format_files, changed_meta_files, \
        modified_packs, ignored_files, new_packs
Esempio n. 3
0
    def get_modified_and_added_files(self, compare_type, prev_ver):
        """Get the modified and added files from a specific branch

        Args:
            compare_type (str): whether to run diff with two dots (..) or three (...)
            prev_ver (str): Against which branch to run the comparision - master/last releaese

        Returns:
            tuple. 3 sets representing modified files, added files and files of old format who have changed.
        """
        if not self.no_configuration_prints:
            click.echo("Collecting all committed files")

        # all committed changes of the current branch vs the prev_ver
        all_committed_files_string = run_command(
            f'git diff --name-status {prev_ver}{compare_type}refs/heads/{self.branch_name}')

        modified_files, added_files, _, old_format_files, changed_meta_files = \
            self.filter_changed_files(all_committed_files_string, prev_ver)

        if not self.is_circle:
            remote_configured = has_remote_configured()
            is_origin_demisto = is_origin_content_repo()
            if remote_configured and not is_origin_demisto:
                if not self.no_configuration_prints:
                    click.echo("Collecting all local changed files from fork against the content master")

                # all local non-committed changes and changes against prev_ver
                all_changed_files_string = run_command(
                    'git diff --name-status upstream/master...HEAD')
                modified_files_from_tag, added_files_from_tag, _, _, changed_meta_files_from_tag = \
                    self.filter_changed_files(all_changed_files_string, print_ignored_files=self.print_ignored_files)

                # only changes against prev_ver (without local changes)
                outer_changes_files_string = run_command('git diff --name-status --no-merges upstream/master...HEAD')
                nc_modified_files, nc_added_files, nc_deleted_files, nc_old_format_files, nc_changed_meta_files = \
                    self.filter_changed_files(outer_changes_files_string, print_ignored_files=self.print_ignored_files)

            else:
                if (not is_origin_demisto and not remote_configured) and not self.no_configuration_prints:
                    error_message, error_code = Errors.changes_may_fail_validation()
                    self.handle_error(error_message, error_code, file_path="General-Error", warning=True,
                                      drop_line=True)

                if not self.no_configuration_prints:
                    click.echo("Collecting all local changed files against the content master")

                # all local non-committed changes and changes against prev_ver
                all_changed_files_string = run_command('git diff --name-status {}'.format(prev_ver))
                modified_files_from_tag, added_files_from_tag, _, _, changed_meta_files_from_tag = \
                    self.filter_changed_files(all_changed_files_string, print_ignored_files=self.print_ignored_files)

                # only changes against prev_ver (without local changes)
                outer_changes_files_string = run_command('git diff --name-status --no-merges HEAD')
                nc_modified_files, nc_added_files, nc_deleted_files, nc_old_format_files, nc_changed_meta_files = \
                    self.filter_changed_files(outer_changes_files_string, print_ignored_files=self.print_ignored_files)

            old_format_files = old_format_files.union(nc_old_format_files)
            modified_files = modified_files.union(
                modified_files_from_tag.intersection(nc_modified_files))

            added_files = added_files.union(
                added_files_from_tag.intersection(nc_added_files))

            changed_meta_files = changed_meta_files.union(
                changed_meta_files_from_tag.intersection(nc_changed_meta_files))

            modified_files = modified_files - set(nc_deleted_files)
            added_files = added_files - set(nc_modified_files) - set(nc_deleted_files)
            changed_meta_files = changed_meta_files - set(nc_deleted_files)

        return modified_files, added_files, old_format_files, changed_meta_files