示例#1
0
def pipeline_update(
        name=None,
        id=None,
        description=None,
        new_name=None,  # pylint: disable=redefined-builtin
        branch=None,
        yml_path=None,
        queue_id=None,
        organization=None,
        project=None,
        detect=None,
        new_folder_path=None):
    """ Update a pipeline
    :param name: Name of the pipeline to update.
    :type name: str
    :param id: Id of the pipeline to update.
    :type id: str
    :param new_name: New updated name of the pipeline.
    :type new_name: str
    :param description: New description for the pipeline.
    :type description: str
    :param branch: Branch name for which the pipeline will be configured.
    :type branch: str
    :param yml_path: Path of the pipelines yaml file in the repo.
    :type yml_path: str
    :param queue_id: Queue id of the agent pool where the pipeline needs to run.
    :type queue_id: int
    :param new_folder_path: New full path of the folder to move the pipeline to.
    e.g. "user1/production_pipelines"
    :type new_folder_path: str
    """
    # pylint: disable=too-many-branches
    organization, project = resolve_instance_and_project(
        detect=detect, organization=organization, project=project)
    pipeline_client = get_new_pipeline_client(organization=organization)
    if id is None:
        if name is not None:
            id = get_definition_id_from_name(name, pipeline_client, project)
        else:
            raise CLIError(
                "Either --id or --name argument must be supplied for this command."
            )
    definition = pipeline_client.get_definition(definition_id=id,
                                                project=project)
    if new_name:
        definition.name = new_name
    if description:
        definition.description = description
    if branch:
        definition.repository.default_branch = branch
    if queue_id:
        definition.queue = AgentPoolQueue()
        definition.queue.id = queue_id
    if yml_path:
        definition.process = _create_process_object(yml_path)
    if new_folder_path:
        definition.path = new_folder_path
    return pipeline_client.update_definition(project=project,
                                             definition_id=id,
                                             definition=definition)
示例#2
0
def validate_name_is_available(name, path, organization, project):
    client = get_new_pipeline_client(organization=organization)
    path = fix_path_for_api(path)
    definition_references = client.get_definitions(project=project,
                                                   name=name,
                                                   path=path)
    if len(definition_references.value) == 0:
        return True
    return False
def pipeline_create(name,
                    description=None,
                    repository=None,
                    branch=None,
                    yml_path=None,
                    repository_type=None,
                    service_connection=None,
                    organization=None,
                    project=None,
                    detect=None,
                    queue_id=None,
                    skip_first_run=None):
    """Create a new Azure Pipeline (YAML based)
    :param name: Name of the new pipeline
    :type name: str
    :param description: Description for the new pipeline
    :type description: str
    :param repository: Repository for which the pipeline needs to be configured.
    Can be clone url of the git repository or name of the repository for a Azure Repos
    or Owner/RepoName in case of GitHub repository.
    If omitted it will be auto-detected from the remote url of local git repository.
    If name is mentioned instead of url, --repository-type argument is also required.
    :type repository: str
    :param branch: Branch name for which the pipeline will be configured. If omitted, it will be auto-detected
    from local repository
    :type branch: str
    :param yml_path: Path of the pipelines yaml file in the repo (if yaml is already present in the repo).
    :type yml_path: str
    :param repository_type: Type of repository. If omitted, it will be auto-detected from remote url
    of local repository. 'tfsgit' for Azure Repos, 'github' for GitHub repository.
    :type repository_type: str
    :param service_connection: Id of the Service connection created for the repository for GitHub repository.
    Use command az devops service-endpoint -h for creating/listing service_connections. Not required for Azure Repos.
    :type service_connection: str
    :param queue_id: Id of the queue in the available agent pools. Will be auto detected if not specified.
    :type queue_id: str
    :param skip_first_run: Specify this flag to prevent the first run being triggered by the command.
    Command will return a pipeline if run is skipped else it will output a pipeline run.
    :type skip_first_run: bool
    """
    repository_name = None
    if repository:
        organization, project = resolve_instance_and_project(
            detect=detect, organization=organization, project=project)
    else:
        organization, project, repository_name = resolve_instance_project_and_repo(
            detect=detect, organization=organization, project=project)
    # resolve repository if local repo for azure repo
    if repository_name:
        repository = repository_name
        repository_type = _AZURE_GIT_REPO_TYPE
    # resolve repository from local repo for github repo
    if not repository:
        repository = _get_repository_url_from_local_repo(detect=detect)
    if not repository:
        raise CLIError('The following arguments are required: --repository.')
    if not repository_type:
        repository_type = try_get_repository_type(repository)
    if not repository_type:
        raise CLIError(
            'The following arguments are required: --repository-type. '
            'Check command help for valid values.')
    if not branch and should_detect(detect):
        branch = get_current_branch_name()
    if not branch:
        raise CLIError('The following arguments are required: --branch.')
    # repository, repository-type, branch should be set by now
    if not repository_name and is_valid_url(repository):
        repository_name = _get_repo_name_from_repo_url(repository)
    else:
        repository_name = repository

    # Validate name availability so user does not face name conflicts after going through the whole process
    if not validate_name_is_available(name, organization, project):
        raise CLIError(
            'Pipeline with name {name} already exists.'.format(name=name))

    # Parse repository information according to repository type
    repo_id = None
    api_url = None
    repository_url = None
    if repository_type.lower() == _GITHUB_REPO_TYPE:
        repo_id = repository_name
        repository_url = 'https://github.com/' + repository_name
        api_url = get_github_repos_api_url(repository_name)
    if repository_type.lower() == _AZURE_GIT_REPO_TYPE:
        repo_id = _get_repository_id_from_name(organization, project,
                                               repository_name)

    if not service_connection and repository_type != _AZURE_GIT_REPO_TYPE:
        service_connection = get_github_service_endpoint(organization, project)

    new_cix_client = get_new_cix_client(organization=organization)
    # No yml path => find or recommend yml scenario
    queue_branch = branch
    if not yml_path:
        yml_path, queue_branch = _create_and_get_yml_path(
            new_cix_client, repository_type, repo_id, repository_name, branch,
            service_connection, project, organization)
    if not queue_id:
        queue_id = _get_agent_queue_by_heuristic(organization=organization,
                                                 project=project)
        if queue_id is None:
            logger.warning(
                'Cannot find a hosted pool queue in the project. Provide a --queue-id in command params.'
            )

    # Create build definition
    definition = _create_pipeline_build_object(
        name, description, repo_id, repository_name, repository_url, api_url,
        branch, service_connection, repository_type, yml_path, queue_id)
    client = get_new_pipeline_client(organization)
    created_definition = client.create_definition(definition=definition,
                                                  project=project)
    logger.warning('Successfully created a pipeline with Name: %s, Id: %s.',
                   created_definition.name, created_definition.id)
    if skip_first_run:
        return created_definition
    return client.queue_build(build=Build(definition=created_definition,
                                          source_branch=queue_branch),
                              project=project)
def validate_name_is_available(name, organization, project):
    client = get_new_pipeline_client(organization=organization)
    definition_references = client.get_definitions(project=project, name=name)
    if not definition_references:
        return True
    return False