Search in sources :

Example 6 with TopLevelItem

use of hudson.model.TopLevelItem in project blueocean-plugin by jenkinsci.

the class GithubOrgFolderPermissionsTest method createGithubPipeline.

private void createGithubPipeline(boolean shouldSucceed) throws Exception {
    String credentialId = createGithubCredential(user);
    String pipelineName = "cloudbeers";
    Map resp = new RequestBuilder(baseUrl).status(shouldSucceed ? 201 : 403).jwtToken(getJwtToken(j.jenkins, user.getId(), user.getId())).crumb(this.crumb).post("/organizations/" + getOrgName() + "/pipelines/").data(GithubTestUtils.buildRequestBody(GithubScm.ID, null, githubApiUrl, pipelineName, "PR-demo")).build(Map.class);
    TopLevelItem item = getOrgRoot().getItem(pipelineName);
    if (shouldSucceed) {
        assertEquals(pipelineName, resp.get("name"));
        assertEquals("io.jenkins.blueocean.rest.impl.pipeline.MultiBranchPipelineImpl", resp.get("_class"));
        Assert.assertTrue(item instanceof WorkflowMultiBranchProject);
        Map r = get("/organizations/" + getOrgName() + "/pipelines/" + pipelineName + "/");
        assertEquals(pipelineName, r.get("name"));
    } else {
        assertEquals(403, resp.get("code"));
        assertEquals("User vivek doesn't have Job create permission", resp.get("message"));
        Assert.assertNull(item);
        String r = get("/organizations/" + getOrgName() + "/pipelines/" + pipelineName + "/", 404, String.class);
    }
}
Also used : WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) TopLevelItem(hudson.model.TopLevelItem) Map(java.util.Map)

Example 7 with TopLevelItem

use of hudson.model.TopLevelItem in project blueocean-plugin by jenkinsci.

the class GithubPipelineCreateRequest method create.

@SuppressWarnings("unchecked")
@Override
public BluePipeline create(Reachable parent) throws IOException {
    String apiUrl = null;
    //default
    String orgName = getName();
    String credentialId = null;
    StringBuilder sb = new StringBuilder();
    List<String> repos = new ArrayList<>();
    if (scmConfig != null) {
        apiUrl = StringUtils.defaultIfBlank(scmConfig.getUri(), GithubScm.DEFAULT_API_URI);
        if (scmConfig.getConfig().get("orgName") instanceof String) {
            orgName = (String) scmConfig.getConfig().get("orgName");
        }
        credentialId = scmConfig.getCredentialId();
        if (scmConfig != null && scmConfig.getConfig().get("repos") instanceof List) {
            for (String r : (List<String>) scmConfig.getConfig().get("repos")) {
                sb.append(String.format("(%s\\b)?", r));
                repos.add(r);
            }
        }
    }
    User authenticatedUser = User.current();
    if (authenticatedUser == null) {
        throw new ServiceException.UnauthorizedException("Must login to create a pipeline");
    }
    TopLevelItem item = null;
    try {
        if (credentialId != null) {
            validateCredentialId(credentialId, apiUrl);
        }
        item = create(Jenkins.getInstance(), getName(), DESCRIPTOR, CustomOrganizationFolderDescriptor.class);
        if (item instanceof OrganizationFolder) {
            if (credentialId != null) {
                //Find domain attached to this credentialId, if present check if it's BlueOcean specific domain then
                //add the properties otherwise simply use it
                Domain domain = CredentialsUtils.findDomain(credentialId, authenticatedUser);
                if (domain == null) {
                    //this should not happen since validateCredentialId found the credential
                    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())) {
                    ((OrganizationFolder) item).addProperty(new BlueOceanCredentialsProvider.FolderPropertyImpl(authenticatedUser.getId(), credentialId, BlueOceanCredentialsProvider.createDomain(apiUrl)));
                }
            }
            GitHubSCMNavigator gitHubSCMNavigator = new GitHubSCMNavigator(apiUrl, orgName, credentialId, credentialId);
            if (sb.length() > 0) {
                gitHubSCMNavigator.setPattern(sb.toString());
            }
            // cick of github scan build
            OrganizationFolder organizationFolder = (OrganizationFolder) item;
            organizationFolder.getNavigators().replace(gitHubSCMNavigator);
            if (repos.size() == 1) {
                SCMSourceEvent.fireNow(new SCMSourceEventImpl(repos.get(0), item, apiUrl, gitHubSCMNavigator));
            } else {
                organizationFolder.scheduleBuild(new Cause.UserIdCause());
            }
            return new GithubOrganizationFolder(organizationFolder, parent.getLink());
        }
    } catch (Exception e) {
        String msg = String.format("Error creating pipeline %s: %s", getName(), e.getMessage());
        logger.error(msg, e);
        if (item != null) {
            try {
                item.delete();
            } catch (InterruptedException e1) {
                logger.error(String.format("Error creating pipeline %s: %s", getName(), e1.getMessage()), e1);
                throw new ServiceException.UnexpectedErrorException("Error cleaning up pipeline " + getName() + " due to error: " + e.getMessage(), e);
            }
        }
        if (e instanceof ServiceException) {
            throw e;
        }
        throw new ServiceException.UnexpectedErrorException(msg, e);
    }
    return null;
}
Also used : User(hudson.model.User) ArrayList(java.util.ArrayList) TopLevelItem(hudson.model.TopLevelItem) Cause(hudson.model.Cause) ArrayList(java.util.ArrayList) List(java.util.List) CustomOrganizationFolderDescriptor(jenkins.branch.CustomOrganizationFolderDescriptor) OrganizationFolder(jenkins.branch.OrganizationFolder) BlueOceanCredentialsProvider(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanCredentialsProvider) GitHubSCMNavigator(org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator) IOException(java.io.IOException) ServiceException(io.jenkins.blueocean.commons.ServiceException) ServiceException(io.jenkins.blueocean.commons.ServiceException) BlueOceanDomainRequirement(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement) Domain(com.cloudbees.plugins.credentials.domains.Domain) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage)

Example 8 with TopLevelItem

use of hudson.model.TopLevelItem in project blueocean-plugin by jenkinsci.

the class GithubOrgFolderTest method simpleOrgTest.

@Test
public void simpleOrgTest() throws IOException, UnirestException {
    login();
    Map resp = new RequestBuilder(baseUrl).status(201).jwtToken(getJwtToken(j.jenkins, "bob", "bob")).post("/organizations/jenkins/pipelines/").data(ImmutableMap.of("name", "jenkinsci", "$class", "io.jenkins.blueocean.blueocean_github_pipeline.GithubPipelineCreateRequest", "scmConfig", ImmutableMap.of("config", ImmutableMap.of("repos", ImmutableList.of("stapler"))))).build(Map.class);
    assertEquals("jenkinsci", resp.get("name"));
    assertEquals("io.jenkins.blueocean.blueocean_github_pipeline.GithubOrganizationFolder", resp.get("_class"));
    TopLevelItem item = j.getInstance().getItem("jenkinsci");
    Assert.assertNotNull(item);
    Assert.assertTrue(item instanceof OrganizationFolder);
    Map r = get("/organizations/jenkins/pipelines/jenkinsci/");
    assertEquals("jenkinsci", r.get("name"));
    assertFalse((Boolean) r.get("scanAllRepos"));
}
Also used : OrganizationFolder(jenkins.branch.OrganizationFolder) TopLevelItem(hudson.model.TopLevelItem) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) PipelineBaseTest(io.jenkins.blueocean.rest.impl.pipeline.PipelineBaseTest) Test(org.junit.Test)

Example 9 with TopLevelItem

use of hudson.model.TopLevelItem 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)

Example 10 with TopLevelItem

use of hudson.model.TopLevelItem in project configuration-as-code-plugin by jenkinsci.

the class SeedJobTest method configure_seed_job.

@Test
@ConfiguredWithCode("SeedJobTest.yml")
public void configure_seed_job() throws Exception {
    final Jenkins jenkins = Jenkins.getInstance();
    final TopLevelItem test = jenkins.getItem("configuration-as-code");
    assertNotNull(test);
    assertTrue(test instanceof WorkflowMultiBranchProject);
}
Also used : Jenkins(jenkins.model.Jenkins) WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) TopLevelItem(hudson.model.TopLevelItem) Test(org.junit.Test) ConfiguredWithCode(org.jenkinsci.plugins.casc.misc.ConfiguredWithCode)

Aggregations

TopLevelItem (hudson.model.TopLevelItem)16 Test (org.junit.Test)5 File (java.io.File)3 IOException (java.io.IOException)3 Map (java.util.Map)3 OrganizationFolder (jenkins.branch.OrganizationFolder)3 Jenkins (jenkins.model.Jenkins)3 WorkflowMultiBranchProject (org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject)3 Domain (com.cloudbees.plugins.credentials.domains.Domain)2 Cause (hudson.model.Cause)2 FreeStyleBuild (hudson.model.FreeStyleBuild)2 FreeStyleProject (hudson.model.FreeStyleProject)2 Hudson (hudson.model.Hudson)2 Item (hudson.model.Item)2 User (hudson.model.User)2 ErrorMessage (io.jenkins.blueocean.commons.ErrorMessage)2 ServiceException (io.jenkins.blueocean.commons.ServiceException)2 BlueOceanCredentialsProvider (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanCredentialsProvider)2 BlueOceanDomainRequirement (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement)2 ConfiguredWithCode (org.jenkinsci.plugins.casc.misc.ConfiguredWithCode)2