Search in sources :

Example 6 with BlueOceanDomainRequirement

use of io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement 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

BlueOceanDomainRequirement (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement)6 User (hudson.model.User)5 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)4 ServiceException (io.jenkins.blueocean.commons.ServiceException)4 Domain (com.cloudbees.plugins.credentials.domains.Domain)3 ErrorMessage (io.jenkins.blueocean.commons.ErrorMessage)3 IOException (java.io.IOException)3 UsernamePasswordCredentialsImpl (com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl)2 Cause (hudson.model.Cause)2 TopLevelItem (hudson.model.TopLevelItem)2 BlueOceanCredentialsProvider (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanCredentialsProvider)2 BlueOceanDomainSpecification (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainSpecification)2 OrganizationFolder (jenkins.branch.OrganizationFolder)2 GHUser (org.kohsuke.github.GHUser)2 AbstractFolderProperty (com.cloudbees.hudson.plugins.folder.AbstractFolderProperty)1 CredentialsStore (com.cloudbees.plugins.credentials.CredentialsStore)1 StandardCredentials (com.cloudbees.plugins.credentials.common.StandardCredentials)1 DomainSpecification (com.cloudbees.plugins.credentials.domains.DomainSpecification)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Mailer (hudson.tasks.Mailer)1