Пример #1
0
def list_policy(organization=None, project=None, repository_id=None, branch=None, detect=None):
    """List all policies in a project.
    :param repository_id: Id (UUID) of the repository.
    :type repository_id: string
    :param branch: Branch. (--repository-id is required)
    :type branch: string
    :rtype: [PolicyConfiguration]
    """
    organization, project = resolve_instance_and_project(
        detect=detect, organization=organization, project=project)

    if branch is not None and repository_id is None:
        raise CLIError('--repository-id is required with --branch')

    scope = None

    if repository_id is not None:
        repository_id = repository_id.replace('-', '')
        scope = repository_id
        if branch is not None:
            branch = resolve_git_ref_heads(branch)
            scope = scope + ':' + branch

    policy_client = get_policy_client(organization)
    return policy_client.get_policy_configurations(project=project, scope=scope)
Пример #2
0
def update_policy_work_item_linking(policy_id,
                                    repository_id=None, branch=None, is_blocking=None, is_enabled=None,
                                    organization=None, project=None, detect=None):
    """Update work item linking 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_scope = current_policy.settings['scope'][0]

    updated_configuration = create_configuration_object(
        repository_id or current_scope['repositoryId'],
        branch or current_scope['refName'],
        is_blocking or str(current_policy.is_blocking),
        is_enabled or str(current_policy.is_enabled),
        '40e92b44-2fe1-4dd6-b3d8-74a9c21d0c6e',
        [],
        []
    )

    return policy_client.update_policy_configuration(
        configuration=updated_configuration,
        project=project,
        configuration_id=policy_id
    )
def update_policy_case_enforcement(policy_id,
                                   repository_id=None,
                                   blocking=None,
                                   enabled=None,
                                   organization=None,
                                   project=None,
                                   detect=None):
    """Update case enforcement 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_scope = current_policy.settings['scope'][0]

    updated_configuration = create_configuration_object(
        repository_id or current_scope['repositoryId'], None,
        blocking if blocking is not None else current_policy.is_blocking,
        enabled if enabled is not None else current_policy.is_enabled,
        '7ed39669-655c-494e-b4a0-a08b4da0fcce', ['enforceConsistentCase'],
        ['true'])

    return policy_client.update_policy_configuration(
        configuration=updated_configuration,
        project=project,
        configuration_id=policy_id)
Пример #4
0
def get_policy(policy_id, organization=None, project=None, detect=None):
    """Show policy details.
    """
    organization, project = resolve_instance_and_project(
        detect=detect, organization=organization, project=project)
    policy_client = get_policy_client(organization)
    return policy_client.get_policy_configuration(project=project, configuration_id=policy_id)
Пример #5
0
def update_policy_comment_required(policy_id,
                                   repository_id=None, branch=None, is_blocking=None, is_enabled=None,
                                   organization=None, project=None, detect=None):
    """Update comment resolution required 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_scope = current_policy.settings['scope'][0]

    updated_configuration = create_configuration_object(
        repository_id or current_scope['repositoryId'],
        branch or current_scope['refName'],
        is_blocking or str(current_policy.is_blocking),
        is_enabled or str(current_policy.is_enabled),
        'c6a1889d-b943-4856-b76f-9e46bb6b0df2',
        [],
        []
    )

    return policy_client.update_policy_configuration(
        configuration=updated_configuration,
        project=project,
        configuration_id=policy_id
    )
Пример #6
0
def update_policy_case_enforcement(policy_id,
                                   repository_id=None, is_blocking=None, is_enabled=None,
                                   organization=None, project=None, detect=None):
    """Update case enforcement 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_scope = current_policy.settings['scope'][0]

    updated_configuration = create_configuration_object(
        repository_id or current_scope['repositoryId'],
        None,
        is_blocking or str(current_policy.is_blocking),
        is_enabled or str(current_policy.is_enabled),
        '40e92b44-2fe1-4dd6-b3d8-74a9c21d0c6e',
        ['enforceConsistentCase'],
        ['true']
    )

    return policy_client.update_policy_configuration(
        configuration=updated_configuration,
        project=project,
        configuration_id=policy_id
    )
Пример #7
0
def update_policy_merge_strategy(policy_id,
                                 repository_id=None, branch=None, is_blocking=None, is_enabled=None,
                                 use_squash_merge=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)
    param_name_array = ['useSquashMerge']

    current_setting = current_policy.settings
    current_scope = current_policy.settings['scope'][0]

    param_value_array = [
        use_squash_merge or current_setting.get('useSquashMerge', None)
    ]

    updated_configuration = create_configuration_object(
        repository_id or current_scope['repositoryId'],
        branch or current_scope['refName'],
        is_blocking or str(current_policy.is_blocking),
        is_enabled or str(current_policy.is_enabled),
        'fa4e907d-c16b-4a4c-9dfa-4916e5d171ab',
        param_name_array,
        param_value_array
    )

    return policy_client.update_policy_configuration(
        configuration=updated_configuration,
        project=project,
        configuration_id=policy_id
    )
Пример #8
0
def create_policy_build(repository_id, branch, branch_match_type, is_blocking, is_enabled,
                        build_definition_id, queue_on_source_update_only, manual_queue_only, display_name, valid_duration,
                        path_filter=None,
                        organization=None, project=None, detect=None):
    """Create build policy
    """
    organization, project = resolve_instance_and_project(
        detect=detect, organization=organization, project=project)
    policy_client = get_policy_client(organization)
    param_name_array = [
        'buildDefinitionId',
        'queueOnSourceUpdateOnly',
        'manualQueueOnly',
        'displayName',
        'validDuration',
        'filenamePatterns']
    param_value_array = [
        build_definition_id,
        queue_on_source_update_only,
        manual_queue_only,
        display_name,
        valid_duration,
        createFileNamePatterns(path_filter)]
    configuration = create_configuration_object(repository_id, branch, is_blocking, is_enabled,
                                                '0609b952-1397-4640-95ec-e00a01b2c241',
                                                param_name_array, param_value_array,
                                                branch_match_type)

    return policy_client.create_policy_configuration(configuration=configuration, project=project)
def create_policy_required_reviewer(repository_id,
                                    branch,
                                    blocking,
                                    enabled,
                                    message,
                                    required_reviewer_ids,
                                    branch_match_type='exact',
                                    path_filter=None,
                                    organization=None,
                                    project=None,
                                    detect=None):
    """Create required reviewer policy
    """
    organization, project = resolve_instance_and_project(
        detect=detect, organization=organization, project=project)
    requiredReviewerIds = resolveIdentityMailsToIds(required_reviewer_ids,
                                                    organization)
    policy_client = get_policy_client(organization)
    param_name_array = ['requiredReviewerIds', 'message', 'filenamePatterns']
    param_value_array = [
        requiredReviewerIds, message,
        createFileNamePatterns(path_filter)
    ]
    configuration = create_configuration_object(
        repository_id, branch, blocking, enabled,
        'fd2167ab-b0be-447a-8ec8-39368250530e', param_name_array,
        param_value_array, branch_match_type)

    return policy_client.create_policy_configuration(
        configuration=configuration, project=project)
def create_policy_approver_count(repository_id,
                                 branch,
                                 blocking,
                                 enabled,
                                 minimum_approver_count,
                                 creator_vote_counts,
                                 allow_downvotes,
                                 reset_on_source_push,
                                 branch_match_type='exact',
                                 organization=None,
                                 project=None,
                                 detect=None):
    """Create approver count policy
    """
    organization, project = resolve_instance_and_project(
        detect=detect, organization=organization, project=project)
    policy_client = get_policy_client(organization)
    param_name_array = [
        'minimumApproverCount', 'creatorVoteCounts', 'allowDownvotes',
        'resetOnSourcePush'
    ]
    param_value_array = [
        minimum_approver_count, creator_vote_counts, allow_downvotes,
        reset_on_source_push
    ]
    configuration = create_configuration_object(
        repository_id, branch, blocking, enabled,
        'fa4e907d-c16b-4a4c-9dfa-4906e5d171dd', param_name_array,
        param_value_array, branch_match_type)

    return policy_client.create_policy_configuration(
        configuration=configuration, project=project)
Пример #11
0
def delete_policy(policy_id, organization=None, project=None, detect=None):
    """Delete a policy.
    """
    organization, project = resolve_instance_and_project(
        detect=detect, organization=organization, project=project)
    policy_client = get_policy_client(organization)
    return policy_client.delete_policy_configuration(project=project, configuration_id=policy_id)
Пример #12
0
def update_policy_file_size(policy_id,
                            repository_id=None, is_blocking=None, is_enabled=None,
                            maximum_git_blob_size=None, use_uncompressed_size=None,
                            organization=None, project=None, detect=None):
    """Update file size 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)
    param_name_array = ['maximumGitBlobSizeInBytes', 'useUncompressedSize']

    current_setting = current_policy.settings
    current_scope = current_policy.settings['scope'][0]

    param_value_array = [
        maximum_git_blob_size or current_setting.get('maximumGitBlobSizeInBytes', None),
        use_uncompressed_size or current_setting.get('useUncompressedSize', None)
    ]

    updated_configuration = create_configuration_object(
        repository_id or current_scope['repositoryId'],
        None,
        is_blocking or str(current_policy.is_blocking),
        is_enabled or str(current_policy.is_enabled),
        '2e26e725-8201-4edd-8bf5-978563c34a80',
        param_name_array,
        param_value_array
    )

    return policy_client.update_policy_configuration(
        configuration=updated_configuration,
        project=project,
        configuration_id=policy_id
    )
Пример #13
0
def create_policy_work_item_linking(repository_id, branch, is_blocking, is_enabled,
                                    organization=None, project=None, detect=None):
    """Create work item linking policy.
    """
    organization, project = resolve_instance_and_project(
        detect=detect, organization=organization, project=project)
    policy_client = get_policy_client(organization)
    configuration = create_configuration_object(repository_id, branch, is_blocking, is_enabled,
                                                '40e92b44-2fe1-4dd6-b3d8-74a9c21d0c6e', [], [])

    return policy_client.create_policy_configuration(configuration=configuration, project=project)
def update_policy_build(policy_id,
                        repository_id=None,
                        branch=None,
                        branch_match_type=None,
                        blocking=None,
                        enabled=None,
                        build_definition_id=None,
                        queue_on_source_update_only=None,
                        manual_queue_only=None,
                        display_name=None,
                        valid_duration=None,
                        path_filter=None,
                        organization=None,
                        project=None,
                        detect=None):
    """Update build 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)
    param_name_array = [
        'buildDefinitionId', 'queueOnSourceUpdateOnly', 'manualQueueOnly',
        'displayName', 'validDuration', 'filenamePatterns'
    ]

    current_setting = current_policy.settings
    current_scope = current_policy.settings['scope'][0]

    param_value_array = [
        build_definition_id or current_setting.get('buildDefinitionId', None),
        queue_on_source_update_only if queue_on_source_update_only is not None
        else current_setting.get('queueOnSourceUpdateOnly', None),
        manual_queue_only if manual_queue_only is not None else
        current_setting.get('manualQueueOnly', None), display_name
        or current_setting.get('displayName', None), valid_duration
        or current_setting.get('validDuration', None),
        createFileNamePatterns(path_filter)
        or current_setting.get('filenamePatterns', None)
    ]

    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,
        '0609b952-1397-4640-95ec-e00a01b2c241', 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)
Пример #15
0
def create_policy_comment_required(repository_id, branch, is_blocking, is_enabled,
                                   organization=None, project=None, detect=None):
    """Create comment resolution required policy.
    """
    organization, project = resolve_instance_and_project(
        detect=detect, organization=organization, project=project)
    policy_client = get_policy_client(organization)
    configuration = create_configuration_object(repository_id, branch, is_blocking, is_enabled,
                                                'c6a1889d-b943-4856-b76f-9e46bb6b0df2', [], [])

    return policy_client.create_policy_configuration(configuration=configuration, project=project)
Пример #16
0
def create_policy_configuration_file(policy_configuration, organization=None, project=None, detect=None):
    '''Create a policy using a configuration file.
    Recommended when creating policies using multiple scopes for a policy.
    See https://aka.ms/azure-devops-cli-docs-policy-file for more information.
    '''
    organization, project = resolve_instance_and_project(
        detect=detect, organization=organization, project=project)
    policy_client = get_policy_client(organization)
    with open(policy_configuration) as f:
        import json
        configuration = json.load(f)
        return policy_client.create_policy_configuration(configuration=configuration, project=project)
Пример #17
0
def create_policy_case_enforcement(repository_id, is_blocking, is_enabled,
                                   organization=None, project=None, detect=None):
    """Create case enforcement policy.
    """
    organization, project = resolve_instance_and_project(
        detect=detect, organization=organization, project=project)
    policy_client = get_policy_client(organization)
    configuration = create_configuration_object(repository_id, None, is_blocking, is_enabled,
                                                '40e92b44-2fe1-4dd6-b3d8-74a9c21d0c6e',
                                                ['enforceConsistentCase'],
                                                ['true'])

    return policy_client.create_policy_configuration(configuration=configuration, project=project)
def update_policy_approver_count(policy_id,
                                 repository_id=None,
                                 branch=None,
                                 blocking=None,
                                 enabled=None,
                                 branch_match_type=None,
                                 minimum_approver_count=None,
                                 creator_vote_counts=None,
                                 allow_downvotes=None,
                                 reset_on_source_push=None,
                                 organization=None,
                                 project=None,
                                 detect=None):
    """Update approver count 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)
    param_name_array = [
        'minimumApproverCount', 'creatorVoteCounts', 'allowDownvotes',
        'resetOnSourcePush'
    ]

    current_setting = current_policy.settings
    current_scope = current_policy.settings['scope'][0]

    param_value_array = [
        minimum_approver_count if minimum_approver_count is not None else
        current_setting.get('minimumApproverCount', None),
        creator_vote_counts if creator_vote_counts is not None else
        current_setting.get('creatorVoteCounts', None),
        allow_downvotes if allow_downvotes is not None else
        current_setting.get('allowDownvotes', None),
        reset_on_source_push if reset_on_source_push is not None else
        current_setting.get('resetOnSourcePush', None)
    ]

    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-4906e5d171dd', 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)
Пример #19
0
def queue_pr_policy(id, evaluation_id, organization=None, detect=None):  # pylint: disable=redefined-builtin
    """Queue an evaluation of a policy for a pull request.
    :param id: ID of the pull request.
    :type id: int
    :param evaluation_id: ID of the policy evaluation to queue.
    :type evaluation_id: str
    :rtype: :class:`PolicyEvaluationRecord <policy.v4_0.models.PolicyEvaluationRecord>`
    """
    organization = resolve_instance(detect=detect, organization=organization)
    git_client = get_git_client(organization)
    pr = git_client.get_pull_request_by_id(id)
    policy_client = get_policy_client(organization)
    return policy_client.requeue_policy_evaluation(
        project=pr.repository.project.id, evaluation_id=evaluation_id)
Пример #20
0
def create_policy_file_size(repository_id, is_blocking, is_enabled,
                            maximum_git_blob_size, use_uncompressed_size,
                            organization=None, project=None, detect=None):
    """Create file size policy
    """
    organization, project = resolve_instance_and_project(
        detect=detect, organization=organization, project=project)
    policy_client = get_policy_client(organization)
    param_name_array = ['maximumGitBlobSizeInBytes', 'useUncompressedSize']
    param_value_array = [maximum_git_blob_size, use_uncompressed_size]
    configuration = create_configuration_object(repository_id, None, is_blocking, is_enabled,
                                                '2e26e725-8201-4edd-8bf5-978563c34a80',
                                                param_name_array, param_value_array)

    return policy_client.create_policy_configuration(configuration=configuration, project=project)
Пример #21
0
def create_policy_merge_strategy(repository_id, branch, is_blocking, is_enabled,
                                 use_squash_merge,
                                 organization=None, project=None, detect=None):
    """Create merge strategy policy
    """
    organization, project = resolve_instance_and_project(
        detect=detect, organization=organization, project=project)
    policy_client = get_policy_client(organization)
    param_name_array = ['useSquashMerge']
    param_value_array = [use_squash_merge]
    configuration = create_configuration_object(repository_id, branch, is_blocking, is_enabled,
                                                'fa4e907d-c16b-4a4c-9dfa-4916e5d171ab',
                                                param_name_array, param_value_array)

    return policy_client.create_policy_configuration(configuration=configuration, project=project)
def list_policy(organization=None, project=None, detect=None):
    """List of policies.
    :param organization: Azure Devops organization URL. Example: https://dev.azure.com/MyOrganizationName/
    :type organization: str
    :param project: Name or ID of the project.
    :type project: str
    :param detect: Automatically detect organization. Default is "on".
    :type detect: str
    :rtype: [PolicyConfiguration]
    """
    try:
        organization, project = resolve_instance_and_project(
            detect=detect, organization=organization, project=project)
        policy_client = get_policy_client(organization)
        return policy_client.get_policy_configurations(project=project)
    except VstsServiceError as ex:
        raise CLIError(ex)
def update_policy_required_reviewer(policy_id,
                                    repository_id=None,
                                    branch=None,
                                    branch_match_type=None,
                                    blocking=None,
                                    enabled=None,
                                    message=None,
                                    required_reviewer_ids=None,
                                    path_filter=None,
                                    organization=None,
                                    project=None,
                                    detect=None):
    """Update required reviewer 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)
    param_name_array = ['requiredReviewerIds', 'message', 'filenamePatterns']
    requiredReviewerIds = resolveIdentityMailsToIds(required_reviewer_ids,
                                                    organization)

    current_setting = current_policy.settings
    current_scope = current_policy.settings['scope'][0]

    param_value_array = [
        requiredReviewerIds
        or current_setting.get('requiredReviewerIds', None), message
        or current_setting.get('message', None),
        createFileNamePatterns(path_filter)
        or current_setting.get('filenamePatterns', None)
    ]

    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,
        'fd2167ab-b0be-447a-8ec8-39368250530e', 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)
def create_policy_merge_strategy(repository_id,
                                 branch,
                                 blocking,
                                 enabled,
                                 use_squash_merge=None,
                                 allow_squash=None,
                                 allow_rebase=None,
                                 allow_rebase_merge=None,
                                 allow_no_fast_forward=None,
                                 branch_match_type='exact',
                                 organization=None,
                                 project=None,
                                 detect=None):
    """Create merge strategy policy
    """
    if (use_squash_merge is None and not allow_squash
            and not allow_rebase_merge and not allow_rebase
            and not allow_no_fast_forward):
        raise CLIError("Atleast one merge type must be enabled.")
    organization, project = resolve_instance_and_project(
        detect=detect, organization=organization, project=project)
    policy_client = get_policy_client(organization)
    if use_squash_merge is None:
        param_name_array = [
            'allowSquash', 'allowRebase', 'allowRebaseMerge',
            'allowNoFastForward'
        ]
        param_value_array = [
            allow_squash, allow_rebase, allow_rebase_merge,
            allow_no_fast_forward
        ]
        for i, value in enumerate(param_value_array):
            if value is None:
                param_value_array[i] = False
    else:
        if allow_rebase or allow_no_fast_forward or allow_rebase_merge or allow_squash:
            raise CLIError(_MERGE_POLICY_DEPRECATED_OPTION_ERROR)
        param_name_array = ['useSquashMerge']
        param_value_array = [use_squash_merge]

    configuration = create_configuration_object(
        repository_id, branch, blocking, enabled,
        'fa4e907d-c16b-4a4c-9dfa-4916e5d171ab', param_name_array,
        param_value_array, branch_match_type)
    return policy_client.create_policy_configuration(
        configuration=configuration, project=project)
def create_policy_case_enforcement(repository_id,
                                   blocking,
                                   enabled,
                                   organization=None,
                                   project=None,
                                   detect=None):
    """Create case enforcement policy.
    """
    organization, project = resolve_instance_and_project(
        detect=detect, organization=organization, project=project)
    policy_client = get_policy_client(organization)
    configuration = create_configuration_object(
        repository_id, None, blocking, enabled,
        '7ed39669-655c-494e-b4a0-a08b4da0fcce', ['enforceConsistentCase'],
        ['true'])

    return policy_client.create_policy_configuration(
        configuration=configuration, project=project)
def delete_policy(id, organization=None, project=None, detect=None):  # pylint: disable=redefined-builtin
    """Delete a policy.
    :param id: ID of the policy.
    :type id: int
    :param organization: Azure Devops organization URL. Example: https://dev.azure.com/MyOrganizationName/
    :type organization: str
    :param project: Name or ID of the project.
    :type project: str
    :param detect: Automatically detect organization. Default is "on".
    :type detect: str
    """
    try:
        organization, project = resolve_instance_and_project(
            detect=detect, organization=organization, project=project)
        policy_client = get_policy_client(organization)
        return policy_client.delete_policy_configuration(project=project,
                                                         configuration_id=id)
    except VstsServiceError as ex:
        raise CLIError(ex)
Пример #27
0
def list_pr_policies(id, organization=None, detect=None, top=None, skip=None):  # pylint: disable=redefined-builtin
    """List policies of a pull request.
    :param id: ID of the pull request.
    :type id: int
    :param top: Maximum number of policies to list.
    :type top: int
    :param skip: Number of policies to skip.
    :type skip: int
    :rtype: list of :class:`PolicyEvaluationRecord <policy.v4_0.models.PolicyEvaluationRecord>`
    """
    organization = resolve_instance(detect=detect, organization=organization)
    git_client = get_git_client(organization)
    pr = git_client.get_pull_request_by_id(id)
    policy_client = get_policy_client(organization)
    artifact_id = "vstfs:///CodeReview/CodeReviewId/{project_id}/{pull_request_id}".format(
        project_id=pr.repository.project.id, pull_request_id=id)
    return policy_client.get_policy_evaluations(project=pr.repository.project.id,
                                                artifact_id=artifact_id,
                                                top=top,
                                                skip=skip)
def create_policy(policy_configuration=None,
                  repository_id=None,
                  branch=None,
                  isBlocking=False,
                  isEnabled=False,
                  policy_type=None,
                  minimumApproverCount=None,
                  creatorVoteCounts=None,
                  allowDownvotes=None,
                  resetOnSourcePush=None,
                  useSquashMerge=None,
                  buildDefinitionId=None,
                  queueOnSourceUpdateOnly=None,
                  manualQueueOnly=None,
                  displayName=None,
                  validDuration=None,
                  maximumGitBlobSizeInBytes=None,
                  useUncompressedSize=None,
                  optionalReviewerIds=None,
                  requiredReviewerIds=None,
                  message=None,
                  organization=None,
                  project=None,
                  detect=None):
    """Create a policy.
    :param policy_configuration: File path of file containing policy configuration to create in a serialized form.
                                 please use / backslash when typing in directory path.
                                 Only --project and --organization param are needed when passing this.
    :type policy_configuration: string

    :param repository_id: Id (UUID) of the repository on which to apply the policy
    :type repository_id: string
    :param branch: Branch on which this policy should be applied
    :type branch: string
    :param isBlocking: Whether the policy should be blocking or not. Default value is false. Accepted values are true and false.
    :type isBlocking: bool
    :param isEnabled: Whether the policy is enabled or not. By default the value is true. Accepted values and true and false.
    :type isEnabled: bool
    :param policy_type: Type of policy you want to create
    :type policy_type: string

    :param optionalReviewerIds: Optional Reviewers (List of email ids seperated with ';').
    :type optionalReviewerIds: string
    :param requiredReviewerIds: Required Reviewers (List of email ids seperated with ';').
    :type requiredReviewerIds: string
    :param message: Message.
    :type message: string

    :param minimumApproverCount: Minimum approver count.
    :type minimumApproverCount: int
    :param creatorVoteCounts: Whether the creator's vote count counts or not.
    :type creatorVoteCounts: bool
    :param allowDownvotes: Whether to allow downvotes or not.
    :type allowDownvotes: bool
    :param resetOnSourcePush: Whether to reset source on push.
    :type resetOnSourcePush: bool

    :param buildDefinitionId: Build Definition Id
    :type buildDefinitionId: int
    :param queueOnSourceUpdateOnly: Queue Only on source update.
    :type queueOnSourceUpdateOnly: bool
    :param manualQueueOnly : Whether to allow only manual queue of builds.
    :type manualQueueOnly : bool
    :param displayName : Display Name.
    :type displayName : string
    :param validDuration :  Policy validity duration (in hours).
    :type validDuration : double

    :param useSquashMerge: Whether to squash merge always.
    :type useSquashMerge: bool

    :param maximumGitBlobSizeInBytes: Maximum Git Blob Size In Bytes.
    :type maximumGitBlobSizeInBytes: long
    :param useUncompressedSize: Whether to use uncompressed size.
    :type useUncompressedSize: bool

    :param organization: Azure Devops organization URL. Example: https://dev.azure.com/MyOrganizationName/
    :type organization: str
    :param project: Name or ID of the project.
    :type project: str
    :param detect: Automatically detect organization. Default is "on".
    :type detect: str
    :rtype: :class:`<PolicyConfiguration> <policy.v4_0.models.PolicyConfiguration>`
    """
    try:
        organization, project = resolve_instance_and_project(
            detect=detect, organization=organization, project=project)
        policy_client = get_policy_client(organization)

        policyConfigurationToCreate = generateConfigurationObject(
            policy_configuration, repository_id, branch, policy_type,
            isBlocking, isEnabled, minimumApproverCount, creatorVoteCounts,
            allowDownvotes, resetOnSourcePush, useSquashMerge,
            buildDefinitionId, queueOnSourceUpdateOnly, manualQueueOnly,
            displayName, validDuration, maximumGitBlobSizeInBytes,
            useUncompressedSize, optionalReviewerIds, requiredReviewerIds,
            message, organization)

        return policy_client.create_policy_configuration(
            configuration=policyConfigurationToCreate, project=project)
    except VstsServiceError as ex:
        raise CLIError(ex)
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)