Search in sources :

Example 1 with MultiBranchPipelineImpl

use of io.jenkins.blueocean.rest.impl.pipeline.MultiBranchPipelineImpl in project blueocean-plugin by jenkinsci.

the class GitPipelineCreateRequest method create.

@Override
public BluePipeline create(Reachable parent) throws IOException {
    User authenticatedUser = User.current();
    if (authenticatedUser == null) {
        throw new ServiceException.UnauthorizedException("Must login to create a pipeline");
    }
    String sourceUri = scmConfig.getUri();
    if (sourceUri == null) {
        throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create Git pipeline:" + getName()).add(new ErrorMessage.Error("scmConfig.uri", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "uri is required")));
    }
    TopLevelItem item = create(Jenkins.getInstance(), getName(), MODE, MultiBranchProjectDescriptor.class);
    if (item instanceof WorkflowMultiBranchProject) {
        WorkflowMultiBranchProject project = (WorkflowMultiBranchProject) item;
        if (StringUtils.isNotBlank(scmConfig.getCredentialId())) {
            Domain domain = CredentialsUtils.findDomain(scmConfig.getCredentialId(), authenticatedUser);
            if (domain == null) {
                throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create pipeline").add(new ErrorMessage.Error("scm.credentialId", ErrorMessage.Error.ErrorCodes.INVALID.toString(), "No domain in user credentials found for credentialId: " + scmConfig.getCredentialId())));
            }
            if (domain.test(new BlueOceanDomainRequirement())) {
                //this is blueocean specific domain
                project.addProperty(new BlueOceanCredentialsProvider.FolderPropertyImpl(authenticatedUser.getId(), scmConfig.getCredentialId(), BlueOceanCredentialsProvider.createDomain(sourceUri)));
            }
        }
        String credentialId = StringUtils.defaultString(scmConfig.getCredentialId());
        project.getSourcesList().add(new BranchSource(new GitSCMSource(null, sourceUri, credentialId, "*", "", false)));
        project.scheduleBuild(new Cause.UserIdCause());
        return new MultiBranchPipelineImpl(project);
    } else {
        try {
            // we don't know about this project type
            item.delete();
        } catch (InterruptedException e) {
            throw new ServiceException.UnexpectedErrorException("Failed to delete pipeline: " + getName());
        }
    }
    return null;
}
Also used : MultiBranchPipelineImpl(io.jenkins.blueocean.rest.impl.pipeline.MultiBranchPipelineImpl) User(hudson.model.User) BlueOceanCredentialsProvider(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanCredentialsProvider) TopLevelItem(hudson.model.TopLevelItem) GitSCMSource(jenkins.plugins.git.GitSCMSource) BranchSource(jenkins.branch.BranchSource) WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) ServiceException(io.jenkins.blueocean.commons.ServiceException) BlueOceanDomainRequirement(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement) Cause(hudson.model.Cause) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage) Domain(com.cloudbees.plugins.credentials.domains.Domain)

Aggregations

Domain (com.cloudbees.plugins.credentials.domains.Domain)1 Cause (hudson.model.Cause)1 TopLevelItem (hudson.model.TopLevelItem)1 User (hudson.model.User)1 ErrorMessage (io.jenkins.blueocean.commons.ErrorMessage)1 ServiceException (io.jenkins.blueocean.commons.ServiceException)1 MultiBranchPipelineImpl (io.jenkins.blueocean.rest.impl.pipeline.MultiBranchPipelineImpl)1 BlueOceanCredentialsProvider (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanCredentialsProvider)1 BlueOceanDomainRequirement (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement)1 BranchSource (jenkins.branch.BranchSource)1 GitSCMSource (jenkins.plugins.git.GitSCMSource)1 WorkflowMultiBranchProject (org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject)1