예제 #1
0
def build_queue(definition_id=None,
                definition_name=None,
                branch=None,
                variables=None,
                open_browser=False,
                team_instance=None,
                project=None,
                detect=None,
                source_branch=None):
    """Request (queue) a build.
    :param definition_id: ID of the definition to queue. Required if --name is not supplied.
    :type definition_id: int
    :param definition_name: Name of the definition to queue. Ignored if --id is supplied.
    :type definition_name: str
    :param branch: Branch to build. Required if there is not a default branch set up on the definition. Example: "dev".
    :type branch: str
    :param variables: Space separated "name=value" pairs for the variables you would like to set.
    :type variables: [str]
    :param open_browser: Open the build results page in your web browser.
    :type open_browser: bool
    :param team_instance: VSTS account or TFS collection URL. Example: https://myaccount.visualstudio.com
    :type team_instance: str
    :param project: Name or ID of the team project.
    :type project: str
    :param detect: Automatically detect instance and project. Default is "on".
    :type detect: str
    :param source_branch: Obsolete. Use --branch instead.
    :type source_branch: str
    :rtype: :class:`<Build> <build.v4_0.models.Build>`
    """
    if branch is None:
        branch = source_branch
    team_instance, project = resolve_instance_and_project(
        detect=detect, team_instance=team_instance, project=project)
    if definition_id is None and definition_name is None:
        raise ValueError(
            'Either the --definition-id argument or the --definition-name argument '
            + 'must be supplied for this command.')
    client = get_build_client(team_instance)
    if definition_id is None:
        definition_id = get_definition_id_from_name(definition_name, client,
                                                    project)
    definition_reference = DefinitionReference(id=definition_id)
    build = Build(definition=definition_reference)
    build.source_branch = resolve_git_ref_heads(branch)
    if variables is not None and variables:
        build.parameters = {}
        for variable in variables:
            separator_pos = variable.find('=')
            if separator_pos >= 0:
                build.parameters[
                    variable[:separator_pos]] = variable[separator_pos + 1:]
            else:
                raise ValueError(
                    'The --variables argument should consist of space separated "name=value" pairs.'
                )
    queued_build = client.queue_build(build=build, project=project)
    if open_browser:
        _open_build(queued_build, team_instance)
    return queued_build
예제 #2
0
파일: build.py 프로젝트: zarenner/vsts-cli
def build_list(definition_ids=None,
               branch=None,
               team_instance=None,
               project=None,
               detect=None,
               top=None,
               result=None,
               status=None,
               reason=None,
               tags=None,
               requested_for=None):
    """List build results.
    :param definition_ids: IDs (space separated) of definitions to list builds for.
    :type definition_ids: list of int
    :param branch: Filter by builds for this branch.
    :type branch: str
    :param team_instance: VSTS account or TFS collection URL. Example: https://myaccount.visualstudio.com
    :type team_instance: str
    :param project: Name or ID of the team project.
    :type project: str
    :param detect: Automatically detect instance and project. Default is "on".
    :type detect: str
    :param top: Maximum number of builds to list.
    :type top: int
    :param result: Limit to builds with this result.
    :type result: str
    :param status: Limit to builds with this status.
    :type status: str
    :param reason: Limit to builds with this reason.
    :type reason: str
    :param tags: Limit to builds with each of the specified tags. Space separated.
    :type tags: list of str
    :param requested_for: Limit to builds requested for this user or group.
    :type requested_for: str
    :rtype: :class:`<Build> <build.v4_0.models.Build>`
    """
    try:
        team_instance, project = resolve_instance_and_project(
            detect=detect, team_instance=team_instance, project=project)
        client = get_build_client(team_instance)
        if definition_ids is not None and definition_ids:
            definition_ids = list(set(definition_ids))  # make distinct
        if tags is not None and tags:
            tags = list(set(tags))  # make distinct
        builds = client.get_builds(definitions=definition_ids,
                                   project=project,
                                   branch_name=resolve_git_ref_heads(branch),
                                   top=top,
                                   result_filter=result,
                                   status_filter=status,
                                   reason_filter=reason,
                                   tag_filters=tags,
                                   requested_for=resolve_identity_as_id(
                                       requested_for, team_instance))
        return builds
    except Exception as ex:
        handle_command_exception(ex)
예제 #3
0
def build_queue(definition_id=None,
                definition_name=None,
                source_branch=None,
                open_browser=False,
                team_instance=None,
                project=None,
                detect=None):
    """Request (queue) a build.
    :param definition_id: ID of the definition to queue. Required if --name is not supplied.
    :type definition_id: int
    :param definition_name: Name of the definition to queue. Ignored if --id is supplied.
    :type definition_name: str
    :param source_branch: Branch to build. Example: "dev".
    :type source_branch: str
    :param open_browser: Open the build results page in your web browser.
    :type open_browser: bool
    :param team_instance: VSTS account or TFS collection URL. Example: https://myaccount.visualstudio.com
    :type team_instance: str
    :param project: Name or ID of the team project.
    :type project: str
    :param detect: Automatically detect instance and project. Default is "on".
    :type detect: str
    :rtype: :class:`<Build> <build.v4_0.models.Build>`
    """
    try:
        team_instance, project = resolve_instance_and_project(
            detect=detect, team_instance=team_instance, project=project)
        if definition_id is None and definition_name is None:
            raise ValueError(
                'Either the --definition-id argument or the --definition-name argument '
                + 'must be supplied for this command.')
        client = get_build_client(team_instance)
        if definition_id is None:
            definition_id = get_definition_id_from_name(
                definition_name, client, project)
        definition_reference = DefinitionReference(id=definition_id)
        build = Build(definition=definition_reference)
        if source_branch is not None:
            build.source_branch = resolve_git_ref_heads(source_branch)
        queued_build = client.queue_build(build=build, project=project)
        if open_browser:
            _open_build(queued_build, team_instance)
        return queued_build
    except Exception as ex:
        handle_command_exception(ex)
예제 #4
0
def list_pull_requests(repository=None,
                       creator=None,
                       include_links=False,
                       reviewer=None,
                       source_branch=None,
                       status=None,
                       target_branch=None,
                       project=None,
                       skip=None,
                       top=None,
                       team_instance=None,
                       detect=None):
    """List pull requests.
    :param repository: Name or ID of the repository.
    :type repository: str
    :param creator: Limit results to pull requests created by this user.
    :type creator: str
    :param include_links: Include _links for each pull request.
    :type include_links: bool
    :param reviewer: Limit results to pull requests where this user is a reviewer.
    :type reviewer: str
    :param source_branch: Limit results to pull requests that originate from this source branch.
    :type source_branch: str
    :param status: Limit results to pull requests with this status.
    :type status: str
    :param target_branch: Limit results to pull requests that target this branch.
    :type target_branch: str
    :param project: Name or ID of the project.
    :type project: str
    :param skip: Number of pull requests to skip.
    :type skip: int
    :param top: Maximum number of pull requests to list.
    :type top: int
    :param team_instance: VSTS account or TFS collection URL. Example: https://myaccount.visualstudio.com
    :type team_instance: str
    :param detect: Automatically detect instance and project. Default is "on".
    :type detect: str
    :rtype: list of :class:`VssJsonCollectionWrapper <git.v4_0.models.VssJsonCollectionWrapper>`
    """
    team_instance, project, repository = resolve_instance_project_and_repo(
        detect=detect,
        team_instance=team_instance,
        project=project,
        repo=repository)
    search_criteria = GitPullRequestSearchCriteria(
        creator_id=resolve_identity_as_id(creator, team_instance),
        include_links=include_links,
        reviewer_id=resolve_identity_as_id(reviewer, team_instance),
        source_ref_name=resolve_git_ref_heads(source_branch),
        status=status,
        target_ref_name=resolve_git_ref_heads(target_branch))
    client = get_git_client(team_instance)
    if repository is None:
        pr_list = client.get_pull_requests_by_project(
            project=project,
            search_criteria=search_criteria,
            skip=skip,
            top=top)
    else:
        pr_list = client.get_pull_requests(project=project,
                                           repository_id=repository,
                                           search_criteria=search_criteria,
                                           skip=skip,
                                           top=top)
    return pr_list
예제 #5
0
def create_pull_request(project=None,
                        repository=None,
                        source_branch=None,
                        target_branch=None,
                        title=None,
                        description=None,
                        auto_complete=False,
                        squash=False,
                        delete_source_branch=False,
                        bypass_policy=False,
                        bypass_policy_reason=None,
                        merge_commit_message=None,
                        reviewers=None,
                        work_items=None,
                        open_browser=False,
                        team_instance=None,
                        detect=None,
                        transition_work_items=False):
    """Create a pull request.
    :param project: Name or ID of the team project.
    :type project: str
    :param repository: Name or ID of the repository to create the pull request in.
    :type repository: str
    :param source_branch: Name of the source branch. Example: "dev".
    :type source_branch: str
    :param target_branch: Name of the target branch. If not specified, defaults to the
                          default branch of the target repository.
    :type target_branch: str
    :param title: Title for the new pull request.
    :type title: str
    :param description: Description for the new pull request. Can include markdown.
    :type description: str
    :param auto_complete: Set the pull request to complete automatically when all policies have passed and
                          the source branch can be merged into the target branch.
    :type auto_complete: bool
    :param squash: Squash the commits in the source branch when merging into the target branch.
    :type squash: bool
    :param delete_source_branch: Delete the source branch after the pull request has been completed
                                 and merged into the target branch.
    :type delete_source_branch: bool
    :param bypass_policy: Bypass required policies (if any) and completes the pull request once it
                          can be merged.
    :type bypass_policy: bool
    :param bypass_policy_reason: Reason for bypassing the required policies.
    :type bypass_policy_reason: str
    :param merge_commit_message: Message displayed when commits are merged.
    :type merge_commit_message: str
    :param reviewers: Additional users or groups to include as reviewers on the new pull request.
                      Space separated.
    :type reviewers: list of str
    :param work_items: IDs of the work items to link to the new pull request. Space separated.
    :type work_items: list of str
    :param open_browser: Open the pull request in your web browser.
    :type open_browser: bool
    :param team_instance: VSTS account or TFS collection URL. Example: https://myaccount.visualstudio.com
    :type team_instance: str
    :param detect: Automatically detect instance, project, repository, source and target branches
                   if these values are not specified. Default is "on".
    :type detect: str
    :param transition_work_items: Transition any work items linked to the pull request into the next logical state.
                   (e.g. Active -> Resolved)
    :type transition_work_items: bool
    :rtype: :class:`GitPullRequest <git.v4_0.models.GitPullRequest>`
    """
    team_instance, project, repository = resolve_instance_project_and_repo(
        detect=detect,
        team_instance=team_instance,
        project=project,
        repo=repository)
    if should_detect(detect):
        if source_branch is None:
            source_branch = get_current_branch_name()
            if source_branch is None:
                raise ValueError(
                    'The source branch could not be detected, please ' +
                    'provide the --source-branch argument.')
    else:
        if source_branch is None:
            raise ValueError('--source-branch is a required argument.')
    if target_branch is None:
        if project is not None and repository is not None:
            target_branch = _get_default_branch(team_instance, project,
                                                repository)
        if target_branch is None:
            raise ValueError(
                'The target branch could not be detected, please ' +
                'provide the --target-branch argument.')
    client = get_git_client(team_instance)
    pr = GitPullRequest(description=description,
                        source_ref_name=source_branch,
                        target_ref_name=target_branch)
    if title is not None:
        pr.title = title
    else:
        pr.title = 'Merge ' + source_branch + ' to ' + target_branch
    pr.source_ref_name = resolve_git_ref_heads(source_branch)
    pr.target_ref_name = resolve_git_ref_heads(target_branch)
    if pr.source_ref_name == pr.target_ref_name:
        raise CLIError(
            'The source branch, "{}", can not be the same as the target branch.'
            .format(pr.source_ref_name))
    pr.reviewers = _resolve_reviewers_as_refs(reviewers, team_instance)
    if work_items is not None and len(work_items) > 0:
        resolved_work_items = []
        for work_item in work_items:
            resolved_work_items.append(ResourceRef(id=work_item))
        pr.work_item_refs = resolved_work_items
    pr = client.create_pull_request(git_pull_request_to_create=pr,
                                    project=project,
                                    repository_id=repository)
    title_from_commit = None
    if title is None:
        # if title wasn't specified and only one commit, we will set the PR title to the comment of that commit
        commits = client.get_pull_request_commits(
            repository_id=repository,
            pull_request_id=pr.pull_request_id,
            project=project)
        if len(commits) == 1:
            title_from_commit = commits[0].comment
    set_completion_options = (bypass_policy or bypass_policy_reason is not None
                              or squash or merge_commit_message is not None
                              or delete_source_branch or transition_work_items)
    if auto_complete or set_completion_options or title_from_commit is not None:
        pr_for_update = GitPullRequest()
        if auto_complete:
            # auto-complete will not get set on create, so a subsequent update is required.
            pr_for_update.auto_complete_set_by = IdentityRef(
                id=resolve_identity_as_id(ME, team_instance))
        if set_completion_options:
            completion_options = GitPullRequestCompletionOptions()
            completion_options.bypass_policy = bypass_policy
            completion_options.bypass_reason = bypass_policy_reason
            completion_options.delete_source_branch = delete_source_branch
            completion_options.squash_merge = squash
            completion_options.merge_commit_message = merge_commit_message
            completion_options.transition_work_items = transition_work_items
            pr_for_update.completion_options = completion_options
        if title_from_commit is not None:
            pr_for_update.title = title_from_commit
        pr = client.update_pull_request(
            git_pull_request_to_update=pr_for_update,
            project=pr.repository.project.id,
            repository_id=pr.repository.id,
            pull_request_id=pr.pull_request_id)
    if open_browser:
        _open_pull_request(pr, team_instance)
    return pr