コード例 #1
0
ファイル: pull_request.py プロジェクト: rbnswartz/vsts-cli
def add_pull_request_work_items(pull_request_id,
                                work_items,
                                team_instance=None,
                                detect=None):
    """Link one or more work items to a pull request.
    :param pull_request_id: ID of the pull request.
    :type pull_request_id: int
    :param work_items: IDs of the work items to link. Space separated.
    :type work_items: list of int
    :param team_instance: VSTS account or TFS collection URL. Example: https://myaccount.visualstudio.com
    :type team_instance: str
    :param detect: Automatically detect instance. Default is "on".
    :type detect: str
    :rtype: list of :class:`AssociatedWorkItem <git.v4_0.models.AssociatedWorkItem>`
    """
    try:
        team_instance = resolve_instance(detect=detect,
                                         team_instance=team_instance)
        client = get_git_client(team_instance)
        existing_pr = client.get_pull_request_by_id(pull_request_id)
        if work_items is not None and work_items:
            work_items = list(set(work_items))  # make distinct
            wit_client = get_work_item_tracking_client(team_instance)
            pr_url = 'vstfs:///Git/PullRequestId/{project}%2F{repo}%2F{id}'.format(
                project=existing_pr.repository.project.id,
                repo=existing_pr.repository.id,
                id=pull_request_id)
            for work_item_id in work_items:
                patch_document = []
                patch_operation = JsonPatchOperation()
                patch_operation.op = 0
                patch_operation.path = '/relations/-'
                patch_operation.value = WorkItemRelation()
                patch_operation.value.attributes = {'name': 'Pull Request'}
                patch_operation.value.rel = 'ArtifactLink'
                patch_operation.value.url = pr_url
                patch_document.append(patch_operation)
                try:
                    wit_client.update_work_item(document=patch_document,
                                                id=work_item_id)
                except VstsClientRequestError as ex:
                    logging.exception(ex)
                    message = ex.args[0]
                    if message != 'Relation already exists.':
                        raise CLIError(ex)
            refs = client.get_pull_request_work_items(
                project=existing_pr.repository.project.id,
                repository_id=existing_pr.repository.id,
                pull_request_id=pull_request_id)
        ids = []
        for ref in refs:
            ids.append(ref.id)
        return wit_client.get_work_items(ids=ids)
    except Exception as ex:
        handle_command_exception(ex)
コード例 #2
0
def _create_patch_operation(op, path, value):
    patch_operation = JsonPatchOperation()
    patch_operation.op = op
    patch_operation.path = path
    patch_operation.value = value
    return patch_operation
コード例 #3
0
ファイル: pull_request.py プロジェクト: luisdgomx/vsts-cli
def remove_pull_request_work_items(pull_request_id,
                                   work_items,
                                   team_instance=None,
                                   detect=None):
    """Unlink one or more work items from a pull request.
    :param pull_request_id: ID of the pull request.
    :type pull_request_id: int
    :param work_items: IDs of the work items to unlink. Space separated.
    :type work_items: list of int
    :param team_instance: VSTS account or TFS collection URL. Example: https://myaccount.visualstudio.com
    :type team_instance: str
    :param detect: Automatically detect instance. Default is "on".
    :type detect: str
    :rtype: list of :class:`AssociatedWorkItem <git.v4_0.models.AssociatedWorkItem>`
    """
    team_instance = resolve_instance(detect=detect,
                                     team_instance=team_instance)
    client = get_git_client(team_instance)
    existing_pr = client.get_pull_request_by_id(pull_request_id)
    if work_items is not None and work_items:
        work_items = list(set(work_items))  # make distinct
        wit_client = get_work_item_tracking_client(team_instance)
        work_items_full = wit_client.get_work_items(ids=work_items, expand=1)
        if work_items_full:
            url = 'vstfs:///Git/PullRequestId/{project}%2F{repo}%2F{id}'.format(
                project=existing_pr.repository.project.id,
                repo=existing_pr.repository.id,
                id=pull_request_id)
            for work_item in work_items_full:
                if work_item.relations is not None:
                    index = 0
                    for relation in work_item.relations:
                        if relation.url == url:
                            patch_document = []

                            patch_test_operation = JsonPatchOperation()
                            patch_test_operation.op = 'test'
                            patch_test_operation.path = '/rev'
                            patch_test_operation.value = work_item.rev
                            patch_document.append(patch_test_operation)

                            patch_operation = JsonPatchOperation()
                            patch_operation.op = 1
                            patch_operation.path = '/relations/{index}'.format(
                                index=index)
                            patch_document.append(patch_operation)

                            wit_client.update_work_item(
                                document=patch_document, id=work_item.id)
                        else:
                            index += 1
            refs = client.get_pull_request_work_items(
                project=existing_pr.repository.project.id,
                repository_id=existing_pr.repository.id,
                pull_request_id=pull_request_id)
            if refs:
                ids = []
                for ref in refs:
                    ids.append(ref.id)
                if ids:
                    return wit_client.get_work_items(ids=ids)