def update_policy_merge_strategy()

in azure-devops/azext_devops/dev/repos/policy.py [0:0]


def update_policy_merge_strategy(policy_id,
                                 repository_id=None, branch=None, branch_match_type=None,
                                 blocking=None, enabled=None,
                                 use_squash_merge=None,
                                 allow_squash=None,
                                 allow_rebase=None,
                                 allow_rebase_merge=None,
                                 allow_no_fast_forward=None,
                                 organization=None, project=None, detect=None):
    """Update merge strategy policy
    """
    organization, project = resolve_instance_and_project(
        detect=detect, organization=organization, project=project)
    policy_client = get_policy_client(organization)
    current_policy = policy_client.get_policy_configuration(project=project, configuration_id=policy_id)
    current_setting = current_policy.settings
    current_scope = current_policy.settings['scope'][0]
    current_setting_new_merge_values_array = [
        current_setting.get('allowSquash', None),
        current_setting.get('allowRebase', None),
        current_setting.get('allowRebaseMerge', None),
        current_setting.get('allowNoFastForward', None)]
    is_new_merge_type_update = (
        allow_squash is not None or
        allow_no_fast_forward is not None or
        allow_rebase is not None or
        allow_rebase_merge is not None)

    if is_new_merge_type_update:
        if use_squash_merge is not None:
            raise CLIError(_MERGE_POLICY_DEPRECATED_OPTION_ERROR)
        # Update is trying to set some new merge type (this should consider useSquashMerge as deprecated)
        param_name_array = ['allowSquash', 'allowRebase', 'allowRebaseMerge', 'allowNoFastForward']
        param_value_array = [
            allow_squash if allow_squash is not None else current_setting.get(
                'allowSquash', current_setting.get('useSquashMerge', None)),
            allow_rebase if allow_rebase is not None else current_setting.get('allowRebase', None),
            allow_rebase_merge if allow_rebase_merge is not None else current_setting.get('allowRebaseMerge', None),
            allow_no_fast_forward if allow_no_fast_forward is not None else current_setting.get(
                'allowNoFastForward', None)
        ]
        # We cannot send setting as None in the API
        for i, value in enumerate(param_value_array):
            if value is None:
                param_value_array[i] = False
        # API does not fail but the update is rejected if the last setting is being set to false.
        # So this check prevents it from client side.
        if not [i for i, value in enumerate(param_value_array) if value]:
            raise CLIError("Atleast one merge type must be enabled.")
    # Update is trying to set legacy option
    elif use_squash_merge is not None:
        # Moving from non legacy to legacy - NOT ALLOWED
        if [i for i, value in enumerate(current_setting_new_merge_values_array) if value is not None]:
            raise CLIError(_MERGE_POLICY_DEPRECATED_OPTION_ERROR)
        # Changing from legacy to legacy
        param_name_array = ['useSquashMerge']
        param_value_array = [use_squash_merge]
    # No update to merge types only other options are getting updated
    else:
        # Current setting is new values type
        if [i for i, value in enumerate(current_setting_new_merge_values_array) if value is not None]:
            param_name_array = ['allowSquash', 'allowRebase', 'allowRebaseMerge', 'allowNoFastForward']
            param_value_array = current_setting_new_merge_values_array
            # We cannot send setting as None in the API
            for i, value in enumerate(param_value_array):
                if value is None:
                    param_value_array[i] = False
        # Current setting is legacy option
        else:
            param_name_array = ['useSquashMerge']
            param_value_array = [current_setting.get('useSquashMerge', False)]

    updated_configuration = create_configuration_object(
        repository_id or current_scope['repositoryId'],
        branch or current_scope['refName'],
        blocking if blocking is not None else current_policy.is_blocking,
        enabled if enabled is not None else current_policy.is_enabled,
        'fa4e907d-c16b-4a4c-9dfa-4916e5d171ab',
        param_name_array,
        param_value_array,
        branch_match_type or current_scope['matchKind']
    )

    return policy_client.update_policy_configuration(
        configuration=updated_configuration,
        project=project,
        configuration_id=policy_id
    )