Search in sources :

Example 26 with BranchSource

use of jenkins.branch.BranchSource in project blueocean-plugin by jenkinsci.

the class MultiBranchTest method getPipelineJobActivities.

//Disabled test for now as I can't get it to work. Tested manually.
//@Test
public void getPipelineJobActivities() throws Exception {
    WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
    sampleRepo1.init();
    sampleRepo1.write("Jenkinsfile", "stage 'build'\n " + "node {echo 'Building'}\n" + "stage 'test'\nnode { echo 'Testing'}\n" + "sleep 10000 \n" + "stage 'deploy'\nnode { echo 'Deploying'}\n");
    sampleRepo1.write("file", "initial content");
    sampleRepo1.git("add", "Jenkinsfile");
    sampleRepo1.git("commit", "--all", "--message=flow");
    //create feature branch
    sampleRepo1.git("checkout", "-b", "abc");
    sampleRepo1.write("Jenkinsfile", "echo \"branch=${env.BRANCH_NAME}\"; " + "node {" + "   stage ('Build'); " + "   echo ('Building'); " + "   stage ('Test'); sleep 10000; " + "   echo ('Testing'); " + "   stage ('Deploy'); " + "   echo ('Deploying'); " + "}");
    ScriptApproval.get().approveSignature("method java.lang.String toUpperCase");
    sampleRepo1.write("file", "subsequent content1");
    sampleRepo1.git("commit", "--all", "--message=tweaked1");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo1.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
    for (SCMSource source : mp.getSCMSources()) {
        assertEquals(mp, source.getOwner());
    }
    scheduleAndFindBranchProject(mp);
    for (WorkflowJob job : mp.getItems()) {
        Queue.Item item = job.getQueueItem();
        if (item != null) {
            item.getFuture().waitForStart();
        }
        job.setConcurrentBuild(false);
        job.scheduleBuild2(0);
        job.scheduleBuild2(0);
    }
    List l = request().get("/organizations/jenkins/pipelines/p/activities").build(List.class);
    Assert.assertEquals(4, l.size());
    Assert.assertEquals("io.jenkins.blueocean.service.embedded.rest.QueueItemImpl", ((Map) l.get(0)).get("_class"));
    Assert.assertEquals("io.jenkins.blueocean.rest.impl.pipeline.PipelineRunImpl", ((Map) l.get(2)).get("_class"));
}
Also used : WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) GitSCMSource(jenkins.plugins.git.GitSCMSource) SCMSource(jenkins.scm.api.SCMSource) GitSCMSource(jenkins.plugins.git.GitSCMSource) BranchSource(jenkins.branch.BranchSource) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) DefaultBranchPropertyStrategy(jenkins.branch.DefaultBranchPropertyStrategy) Queue(hudson.model.Queue)

Example 27 with BranchSource

use of jenkins.branch.BranchSource 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 28 with BranchSource

use of jenkins.branch.BranchSource in project blueocean-plugin by jenkinsci.

the class GitPipelineUpdateRequest method update.

@CheckForNull
@Override
@SuppressWarnings("unchecked")
public BluePipeline update(BluePipeline pipeline) throws IOException {
    Item item = Jenkins.getInstance().getItemByFullName(pipeline.getFullName());
    if (item instanceof MultiBranchProject) {
        ACL acl = Jenkins.getInstance().getACL();
        Authentication a = Jenkins.getAuthentication();
        if (!acl.hasPermission(a, Item.CONFIGURE)) {
            throw new ServiceException.ForbiddenException(String.format("Failed to update Git pipeline: %s. User %s doesn't have Job configure permission", pipeline.getName(), a.getName()));
        }
        MultiBranchProject mbp = (MultiBranchProject) item;
        BranchSource branchSource = getGitScmSource(mbp);
        if (branchSource != null) {
            mbp.getSourcesList().replaceBy(Collections.singleton(branchSource));
            mbp.scheduleBuild2(0, new CauseAction(new Cause.UserIdCause()));
        }
    }
    return pipeline;
}
Also used : Item(hudson.model.Item) Authentication(org.acegisecurity.Authentication) CauseAction(hudson.model.CauseAction) MultiBranchProject(jenkins.branch.MultiBranchProject) ACL(hudson.security.ACL) BranchSource(jenkins.branch.BranchSource) CheckForNull(javax.annotation.CheckForNull)

Example 29 with BranchSource

use of jenkins.branch.BranchSource in project blueocean-plugin by jenkinsci.

the class AbstractRunImplTest method replayRunTestMB.

// Disabled, see JENKINS-40084
@Test
@Ignore
public void replayRunTestMB() throws Exception {
    j.createOnlineSlave(Label.get("remote"));
    sampleRepo.write("Jenkinsfile", "node('remote') {\n" + "    ws {\n" + "       checkout scm\n" + "       stage 'build'\n " + "node {echo 'Building'}\n" + "       stage 'test'\nnode { echo 'Testing'}\n" + "       stage 'deploy'\nnode { echo 'Deploying'}\n" + "       }\n" + "   }");
    sampleRepo.git("add", "Jenkinsfile");
    sampleRepo.git("commit", "--message=init");
    WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
    for (SCMSource source : mp.getSCMSources()) {
        assertEquals(mp, source.getOwner());
    }
    mp.scheduleBuild2(0).getFuture().get();
    WorkflowJob job1 = mp.getItem("master");
    WorkflowRun b1 = job1.scheduleBuild2(0).waitForStart();
    j.waitForCompletion(b1);
    j.assertBuildStatusSuccess(b1);
    sampleRepo.write("file1", "");
    sampleRepo.git("add", "file1");
    sampleRepo.git("commit", "--message=init");
    WorkflowRun b2 = job1.scheduleBuild2(0).get();
    j.assertBuildStatusSuccess(b2);
    Assert.assertNotEquals(new PipelineRunImpl(b1, null, null).getCommitId(), new PipelineRunImpl(b2, null, null).getCommitId());
    Map replayBuild = request().post("/organizations/jenkins/pipelines/p/branches/master/runs/" + b1.getNumber() + "/replay").build(Map.class);
    Queue.Item item = j.getInstance().getQueue().getItem(Long.parseLong((String) replayBuild.get("id")));
    WorkflowRun replayedRun = (WorkflowRun) item.getFuture().get();
    Map r = request().get("/organizations/jenkins/pipelines/p/branches/master/runs/" + replayedRun.getNumber() + "/").build(Map.class);
    assertEquals(new PipelineRunImpl(b1, null, null).getCommitId(), r.get("commitId"));
}
Also used : WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) GitSCMSource(jenkins.plugins.git.GitSCMSource) SCMSource(jenkins.scm.api.SCMSource) GitSCMSource(jenkins.plugins.git.GitSCMSource) BranchSource(jenkins.branch.BranchSource) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) DefaultBranchPropertyStrategy(jenkins.branch.DefaultBranchPropertyStrategy) Map(java.util.Map) Queue(hudson.model.Queue) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 30 with BranchSource

use of jenkins.branch.BranchSource in project blueocean-plugin by jenkinsci.

the class SseEventTest method multiBranchJobEvents.

@Test
public void multiBranchJobEvents() throws Exception {
    setupScm();
    final OneShotEvent success = new OneShotEvent();
    // index: 0 QUEUED, index 1: ALLOCATED, index: 2 INDEXING, index: 3 COMPLETED
    final boolean[] mbpStatus = { false, false, false, false };
    // index: 0 created, index: 1 QUEUED, index 2: ALLOCATED, index: 3 RUNNING, index: 4 SUCCESS
    final boolean[] masterBranchStatus = { false, false, false, false, false };
    final boolean[] feature1BranchStatus = { false, false, false, false, false };
    final boolean[] masterBranchPipelineEvent = { false };
    final boolean[] feature1BranchPipelineEvent = { false };
    final boolean[] masterBranchNodeBlockEvent = { false };
    final boolean[] feature1BranchNodeBlockEvent = { false };
    final AssertionHelper assertionHelper = new AssertionHelper();
    SSEConnection con = new SSEConnection(j.getURL(), "me", message -> {
        System.out.println(message);
        if ("job".equals(message.get(jenkins_channel))) {
            if ("org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject".equals(message.get(jenkins_object_type))) {
                assertionHelper.isEquals("/blue/rest/organizations/jenkins/pipelines/pipeline1/", message.get(blueocean_job_rest_url));
                // assertEquals("/blue/rest/organizations/jenkins/pipelines/pipeline1/", message.get(blueocean_job_rest_url));
                assertionHelper.isEquals("pipeline1", message.get(blueocean_job_pipeline_name));
                assertionHelper.isEquals("job", message.get(jenkins_channel));
                assertionHelper.isEquals("jenkins", message.get(jenkins_org));
                assertionHelper.isEquals("true", message.get(job_ismultibranch));
                assertionHelper.isEquals("pipeline1", message.get(job_name));
                assertionHelper.isNull(message.get(job_orgfolder_indexing_status));
                assertionHelper.isNull(message.get(job_orgfolder_indexing_result));
                if ("QUEUED".equals(message.get(job_run_status))) {
                    // queued
                    mbpStatus[0] = true;
                }
                if ("ALLOCATED".equals(message.get(job_run_status))) {
                    // allocated or left queue
                    mbpStatus[1] = true;
                }
                if ("INDEXING".equals(message.get(job_multibranch_indexing_status))) {
                    // indexing started
                    mbpStatus[2] = true;
                    assertionHelper.isNotNull(message.get(job_run_queueId));
                }
                if ("SUCCESS".equals(message.get(job_multibranch_indexing_result)) || ("COMPLETED".equals(message.get(job_multibranch_indexing_status)) && "job_run_queue_task_complete".equals(message.get(jenkins_event)))) {
                    // indexing completed
                    mbpStatus[3] = true;
                }
            } else if ("org.jenkinsci.plugins.workflow.job.WorkflowJob".equals(message.get(jenkins_object_type))) {
                assertionHelper.isEquals("pipeline1", message.get(blueocean_job_pipeline_name));
                if ("pipeline1/master".equals(message.get(job_name))) {
                    System.out.println("job_run_status::::: " + message.get(job_run_status));
                    assertionHelper.isEquals("/blue/rest/organizations/jenkins/pipelines/pipeline1/branches/master/", message.get(blueocean_job_rest_url));
                    assertionHelper.isEquals("master", message.get(blueocean_job_branch_name));
                    if ("job_crud_created".equals(message.get(jenkins_event))) {
                        masterBranchStatus[0] = true;
                    }
                    if ("QUEUED".equals(message.get(job_run_status))) {
                        masterBranchStatus[1] = true;
                    } else if ("ALLOCATED".equals(message.get(job_run_status))) {
                        masterBranchStatus[2] = true;
                    } else if ("RUNNING".equals(message.get(job_run_status))) {
                        System.out.println("in master running.....");
                        assertionHelper.isEquals("1", message.get(blueocean_queue_item_expected_build_number));
                        masterBranchStatus[3] = true;
                    }
                    if ("SUCCESS".equals(message.get(job_run_status)) || "job_run_queue_task_complete".equals(message.get(jenkins_event))) {
                        masterBranchStatus[4] = true;
                    }
                } else if ("pipeline1/feature%2Fux-1".equals(message.get(job_name))) {
                    assertionHelper.isEquals("/blue/rest/organizations/jenkins/pipelines/pipeline1/branches/feature%252Fux-1/", message.get(blueocean_job_rest_url));
                    assertionHelper.isEquals("feature%2Fux-1", message.get(blueocean_job_branch_name));
                    if ("job_crud_created".equals(message.get(jenkins_event))) {
                        feature1BranchStatus[0] = true;
                    }
                    System.out.println("job_run_status::::: " + message.get(job_run_status));
                    if ("QUEUED".equals(message.get(job_run_status))) {
                        feature1BranchStatus[1] = true;
                    } else if ("ALLOCATED".equals(message.get(job_run_status))) {
                        feature1BranchStatus[2] = true;
                    }
                    if ("SUCCESS".equals(message.get(job_run_status)) || "job_run_queue_task_complete".equals(message.get(jenkins_event))) {
                        feature1BranchStatus[4] = true;
                    }
                }
            } else if ("org.jenkinsci.plugins.workflow.job.WorkflowRun".equals(message.get(jenkins_object_type))) {
                if ("pipeline1/master".equals(message.get(job_name))) {
                    if ("RUNNING".equals(message.get(job_run_status))) {
                        assertionHelper.isEquals("1", message.get(blueocean_queue_item_expected_build_number));
                        masterBranchStatus[3] = true;
                    }
                } else if ("pipeline1/feature%2Fux-1".equals(message.get(job_name))) {
                    if ("RUNNING".equals(message.get(job_run_status))) {
                        assertionHelper.isEquals("1", message.get(blueocean_queue_item_expected_build_number));
                        feature1BranchStatus[3] = true;
                    }
                }
            }
            // all completed
            if (mbpStatus[3] && masterBranchStatus[4] && feature1BranchStatus[4]) {
                success.signal();
            }
        } else if ("pipeline".equals(message.get(jenkins_channel))) {
            if ("pipeline1/master".equals(message.get(pipeline_job_name))) {
                assertionHelper.isEquals("1", message.get(pipeline_run_id));
                masterBranchPipelineEvent[0] = true;
                if ("pipeline_step".equals(message.get(jenkins_event)) && "node".equals(message.get(pipeline_step_name))) {
                    masterBranchNodeBlockEvent[0] = true;
                }
            } else if ("pipeline1/feature%2Fux-1".equals(message.get(pipeline_job_name))) {
                feature1BranchPipelineEvent[0] = true;
                assertionHelper.isEquals("1", message.get(pipeline_run_id));
                if ("pipeline_step".equals(message.get(jenkins_event)) && "node".equals(message.get(pipeline_step_name))) {
                    feature1BranchNodeBlockEvent[0] = true;
                }
            }
            if ("pipeline_stage".equals(message.get(jenkins_event))) {
                assertionHelper.isNotNull(message.get(pipeline_step_stage_name));
                assertionHelper.isEquals("build", message.get(pipeline_step_stage_name));
                assertionHelper.isNotNull(message.get(pipeline_step_flownode_id));
            } else if ("pipeline_step".equals(message.get(jenkins_event))) {
                assertionHelper.isNotNull(message.get(pipeline_step_flownode_id));
                assertionHelper.isEquals("false", message.get(pipeline_step_is_paused));
            }
        }
    });
    con.subscribe("pipeline");
    con.subscribe("job");
    final WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "pipeline1");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
    for (SCMSource source : mp.getSCMSources()) {
        assertEquals(mp, source.getOwner());
    }
    mp.scheduleBuild2(0).getFuture().get();
    WorkflowJob p = mp.getItem("master");
    if (p == null) {
        mp.getIndexing().writeWholeLogTo(System.out);
        fail("master project not found");
    }
    j.waitUntilNoActivity();
    WorkflowRun b1 = p.getLastBuild();
    assertEquals(1, b1.getNumber());
    assertEquals(2, mp.getItems().size());
    success.block(5000);
    con.close();
    if (success.isSignaled()) {
        assertTrue(masterBranchPipelineEvent[0]);
        assertTrue(feature1BranchPipelineEvent[0]);
        assertTrue(masterBranchNodeBlockEvent[0]);
        assertTrue(feature1BranchNodeBlockEvent[0]);
        assertArrayEquals(mbpStatus, new boolean[] { true, true, true, true });
        assertArrayEquals(masterBranchStatus, new boolean[] { true, true, true, true, true });
        assertArrayEquals(feature1BranchStatus, new boolean[] { true, true, true, true, true });
    }
    if (assertionHelper.totalErrors() > 0) {
        fail("There were errors: " + assertionHelper.totalErrors());
    }
}
Also used : WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) GitSCMSource(jenkins.plugins.git.GitSCMSource) SCMSource(jenkins.scm.api.SCMSource) GitSCMSource(jenkins.plugins.git.GitSCMSource) BranchSource(jenkins.branch.BranchSource) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) SSEConnection(io.jenkins.blueocean.events.sse.SSEConnection) DefaultBranchPropertyStrategy(jenkins.branch.DefaultBranchPropertyStrategy) OneShotEvent(hudson.util.OneShotEvent) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Aggregations

BranchSource (jenkins.branch.BranchSource)42 GitSCMSource (jenkins.plugins.git.GitSCMSource)39 WorkflowMultiBranchProject (org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject)39 SCMSource (jenkins.scm.api.SCMSource)35 Test (org.junit.Test)34 DefaultBranchPropertyStrategy (jenkins.branch.DefaultBranchPropertyStrategy)32 Map (java.util.Map)26 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)25 List (java.util.List)16 ArrayList (java.util.ArrayList)15 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)13 User (hudson.model.User)6 Queue (hudson.model.Queue)5 MockFolder (org.jvnet.hudson.test.MockFolder)5 ExtensionList (hudson.ExtensionList)3 FreeStyleProject (hudson.model.FreeStyleProject)3 RunList (hudson.util.RunList)3 ImmutableList (com.google.common.collect.ImmutableList)2 Cause (hudson.model.Cause)2 OneShotEvent (hudson.util.OneShotEvent)2