Java Code Examples for org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject#getSCMSources()

The following examples show how to use org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject#getSCMSources() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MultiBranchTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Test
public void resolveMbpLink() throws Exception {
    WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
    FreeStyleProject f = j.jenkins.createProject(FreeStyleProject.class, "f");
    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();

    j.waitUntilNoActivity();

    Assert.assertEquals("/blue/rest/organizations/jenkins/pipelines/p/",LinkResolver.resolveLink(mp).getHref());
    Assert.assertEquals("/blue/rest/organizations/jenkins/pipelines/p/branches/master/",LinkResolver.resolveLink(mp.getBranch("master")).getHref());
    Assert.assertEquals("/blue/rest/organizations/jenkins/pipelines/p/branches/feature%252Fux-1/",LinkResolver.resolveLink(mp.getBranch("feature%2Fux-1")).getHref());
    Assert.assertEquals("/blue/rest/organizations/jenkins/pipelines/p/branches/feature2/",LinkResolver.resolveLink(mp.getBranch("feature2")).getHref());
    Assert.assertEquals("/blue/rest/organizations/jenkins/pipelines/f/",LinkResolver.resolveLink(f).getHref());
}
 
Example 2
Source File: MultiBranchTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Test
public void getMultiBranchPipelines() throws IOException, ExecutionException, InterruptedException {
    Assume.assumeTrue(runAllTests());
    WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
    FreeStyleProject f = j.jenkins.createProject(FreeStyleProject.class, "f");
    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();

    List<Map> resp = get("/organizations/jenkins/pipelines/", List.class);
    Assert.assertEquals(2, resp.size());
    validatePipeline(f, resp.get(0));
    validateMultiBranchPipeline(mp, resp.get(1), 3);
    Assert.assertEquals(mp.getBranch("master").getBuildHealth().getScore(), resp.get(0).get("weatherScore"));
}
 
Example 3
Source File: MultiBranchTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Test
public void getPipelinesTest() throws Exception {

    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());
    }

    WorkflowJob p = scheduleAndFindBranchProject(mp, "master");


    j.waitUntilNoActivity();

    List<Map> responses = get("/search/?q=type:pipeline;excludedFromFlattening:jenkins.branch.MultiBranchProject", List.class);
    Assert.assertEquals(1, responses.size());
}
 
Example 4
Source File: MultiBranchTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Test
public void getMultiBranchPipelinesWithNonMasterBranch() throws Exception {
    sampleRepo.git("checkout", "feature2");
    sampleRepo.git("branch","-D", "master");
    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();

    List<Map> resp = get("/organizations/jenkins/pipelines/", List.class);
    Assert.assertEquals(1, resp.size());
    validateMultiBranchPipeline(mp, resp.get(0), 2);
    assertNull(mp.getBranch("master"));
}
 
Example 5
Source File: MultiBranchTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Test
public void multiBranchPipelineIndex() throws Exception {
    User user = login();
    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());
    }

    Map map = new RequestBuilder(baseUrl)
            .post("/organizations/jenkins/pipelines/p/runs/")
            .jwtToken(getJwtToken(j.jenkins, user.getId(), user.getId()))
            .crumb( getCrumb( j.jenkins ) )
            .data(ImmutableMap.of())
            .status(200)
            .build(Map.class);

    assertNotNull(map);
}
 
Example 6
Source File: MultiBranchTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Test
public void startMultiBranchPipelineRuns() throws Exception {
    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());
    }
    WorkflowJob p = scheduleAndFindBranchProject(mp, "feature%2Fux-1");
    j.waitUntilNoActivity();

    Map resp = post("/organizations/jenkins/pipelines/p/branches/"+ Util.rawEncode("feature%2Fux-1")+"/runs/",
        Collections.EMPTY_MAP);
    String id = (String) resp.get("id");
    String link = getHrefFromLinks(resp, "self");
    Assert.assertEquals("/blue/rest/organizations/jenkins/pipelines/p/branches/feature%252Fux-1/runs/"+id+"/", link);
}
 
Example 7
Source File: MultiBranchTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Test
public void parameterizedBranchTest() throws Exception{
    setupParameterizedScm();

    WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo2.toString(), "", "*", "", false)));
    for (SCMSource source : mp.getSCMSources()) {
        assertEquals(mp, source.getOwner());
    }

    WorkflowJob p = scheduleAndFindBranchProject(mp, branches[1]);
    j.waitUntilNoActivity();

    Map resp = get("/organizations/jenkins/pipelines/p/branches/"+Util.rawEncode(branches[1])+"/", Map.class);
    List<Map<String,Object>> parameters = (List<Map<String, Object>>) resp.get("parameters");
    Assert.assertEquals(1, parameters.size());
    Assert.assertEquals("param1", parameters.get(0).get("name"));
    Assert.assertEquals("StringParameterDefinition", parameters.get(0).get("type"));
    Assert.assertEquals("string param", parameters.get(0).get("description"));
    Assert.assertEquals("xyz", ((Map)parameters.get(0).get("defaultParameterValue")).get("value"));

    resp = post("/organizations/jenkins/pipelines/p/branches/"+Util.rawEncode(branches[1])+"/runs/",
            ImmutableMap.of("parameters",
                    ImmutableList.of(ImmutableMap.of("name", "param1", "value", "abc"))
            ), 200);
    Assert.assertEquals(branches[1], resp.get("pipeline"));
    Thread.sleep(5000);
    resp = get("/organizations/jenkins/pipelines/p/branches/"+Util.rawEncode(branches[1])+"/runs/2/");
    Assert.assertEquals("Response should be SUCCESS: " + resp.toString(), "SUCCESS", resp.get("result"));
    Assert.assertEquals("Response should be FINISHED: " + resp.toString(), "FINISHED", resp.get("state"));
}
 
Example 8
Source File: MultiBranchTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Test
public void testMultiBranchPipelineBranchSecurePermissions() throws IOException, ExecutionException, InterruptedException {
    j.jenkins.setSecurityRealm(new HudsonPrivateSecurityRealm(false));
    j.jenkins.setAuthorizationStrategy(new LegacyAuthorizationStrategy());

    MockFolder folder1 = j.createFolder("folder1");
    WorkflowMultiBranchProject mp = folder1.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();


    Map r = get("/organizations/jenkins/pipelines/folder1/pipelines/p/");


    Map<String,Boolean> permissions = (Map<String, Boolean>) r.get("permissions");
    Assert.assertFalse(permissions.get("create"));
    Assert.assertTrue(permissions.get("read"));
    Assert.assertFalse(permissions.get("start"));
    Assert.assertFalse(permissions.get("stop"));



    r = get("/organizations/jenkins/pipelines/folder1/pipelines/p/branches/master/");

    permissions = (Map<String, Boolean>) r.get("permissions");
    Assert.assertFalse(permissions.get("create"));
    Assert.assertFalse(permissions.get("start"));
    Assert.assertFalse(permissions.get("stop"));
    Assert.assertTrue(permissions.get("read"));
}
 
Example 9
Source File: MultiBranchTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Test
public void getMultiBranchPipeline() throws IOException, ExecutionException, InterruptedException {
    Assume.assumeTrue(runAllTests());
    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();


    Map resp = get("/organizations/jenkins/pipelines/p/");
    validateMultiBranchPipeline(mp, resp, 3);

    List<String> names = (List<String>) resp.get("branchNames");

    IsArrayContainingInAnyOrder.arrayContainingInAnyOrder(names, branches);

    List<Map> br = get("/organizations/jenkins/pipelines/p/branches", List.class);

    List<String> branchNames = new ArrayList<>();
    List<Integer> weather = new ArrayList<>();
    for (Map b : br) {
        branchNames.add((String) b.get("name"));
        weather.add((int) b.get("weatherScore"));
    }

    for (String n : branches) {
        assertTrue(branchNames.contains(n));
    }

    for (int s : weather) {
        assertEquals(100, s);
    }
}
 
Example 10
Source File: MultiBranchTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void testMbpPagination() throws Exception {
    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();
    this.j.waitUntilNoActivity();

    //create 6 runs
    List<WorkflowRun> launchedItems = new ArrayList<>();
    for(String branch:branches) {
        WorkflowJob job = findBranchProject(mp, branch);
        launchedItems.addAll(job.getBuilds());
        for (int j = 0; j < 2; j++) {
            launchedItems.add(job.scheduleBuild2(0).waitForStart());
            this.j.waitUntilNoActivity();
        }
    }

    //total 9. 3 triggered from indexing and 6 we launched
    assertEquals(9, launchedItems.size());

    //sort runs
    Collections.sort(launchedItems, new Comparator<WorkflowRun>() {
        @Override
        public int compare(WorkflowRun o1, WorkflowRun o2) {
            return new Date(o2.getStartTimeInMillis()).compareTo(new Date(o1.getStartTimeInMillis()));

        }
    });

    //request 10 runs, but should not return 9 runs.
    List<Map> resp = get("/organizations/jenkins/pipelines/p/runs?start=0&limit=10", List.class);

    assertEquals(9, resp.size()); //max number of runs are 9

    for(int i=0; i< 9; i++){
        Assert.assertEquals(launchedItems.get(i).getId(), (resp.get(i).get("id")));
    }

    // now call 3 out of 9 runs and see pagination returns only 3 and in right sort order
    resp = get("/organizations/jenkins/pipelines/p/runs?start=0&limit=3", List.class);

    assertEquals(3, resp.size());

    for(int i=0; i< 3; i++){
        Assert.assertEquals(launchedItems.get(i).getId(), (resp.get(i).get("id")));
    }
}
 
Example 11
Source File: PipelineNodeTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void declarativeSyntheticSteps() throws Exception {
    setupScm("pipeline {\n" +
                 "    agent any\n" +
                 "    stages {\n" +
                 "        stage(\"build\") {\n" +
                 "            steps{\n" +
                 "              sh 'echo \"Start Build\"'\n" +
                 "              echo 'End Build'\n" +
                 "            }\n" +
                 "        }\n" +
                 "        stage(\"deploy\") {\n" +
                 "            steps{\n" +
                 "              sh 'echo \"Start Deploy\"'\n" +
                 "              sh 'echo \"Deploying...\"'\n" +
                 "              sh 'echo \"End Deploy\"'\n" +
                 "            }           \n" +
                 "        }\n" +
                 "    }\n" +
                 "    post {\n" +
                 "        failure {\n" +
                 "            echo \"failed\"\n" +
                 "        }\n" +
                 "        success {\n" +
                 "            echo \"success\"\n" +
                 "        }\n" +
                 "    }\n" +
                 "}");
    WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false)));
    for (SCMSource source : mp.getSCMSources()) {
        assertEquals(mp, source.getOwner());
    }

    mp.scheduleBuild2(0).getFuture().get();

    j.waitUntilNoActivity();

    WorkflowJob p = scheduleAndFindBranchProject(mp, "master");
    j.waitUntilNoActivity();
    WorkflowRun b1 = p.getLastBuild();
    Assert.assertEquals(Result.SUCCESS, b1.getResult());

    List<FlowNode> stages = getStages(NodeGraphBuilder.NodeGraphBuilderFactory.getInstance(b1));

    Assert.assertEquals(2, stages.size());

    Assert.assertEquals("build", stages.get(0).getDisplayName());
    Assert.assertEquals("deploy", stages.get(1).getDisplayName());

    List<Map> resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/nodes/", List.class);
    Assert.assertEquals(2, resp.size());
    Assert.assertEquals("build", resp.get(0).get("displayName"));
    Assert.assertEquals("deploy", resp.get(1).get("displayName"));

    resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/steps/", List.class);
    Assert.assertEquals(7, resp.size());

    resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/nodes/" + stages.get(0).getId() + "/steps/", List.class);
    Assert.assertEquals(3, resp.size());

    resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/nodes/" + stages.get(1).getId() + "/steps/", List.class);
    Assert.assertEquals(4, resp.size());

}
 
Example 12
Source File: MultiBranchTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void branchCapabilityTest() throws Exception {

    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());
    }

    WorkflowJob p = scheduleAndFindBranchProject(mp, "master");


    j.waitUntilNoActivity();

    //check MBP capabilities
    Map<String,Object> response = get("/organizations/jenkins/pipelines/p/");
    String clazz = (String) response.get("_class");

    response = get("/classes/"+clazz+"/");
    Assert.assertNotNull(response);

    List<String> classes = (List<String>) response.get("classes");
    Assert.assertTrue(classes.contains(BLUE_SCM)
            && classes.contains(JENKINS_MULTI_BRANCH_PROJECT)
            && classes.contains(BLUE_MULTI_BRANCH_PIPELINE)
            && classes.contains(BLUE_PIPELINE_FOLDER)
            && classes.contains(JENKINS_ABSTRACT_FOLDER)
            && classes.contains(BLUE_PIPELINE));

    response = get("/organizations/jenkins/pipelines/p/branches/master/", Map.class);
    clazz = (String) response.get("_class");

    response = get("/classes/"+clazz+"/");
    Assert.assertNotNull(response);

    classes = (List<String>) response.get("classes");
    Assert.assertTrue(classes.contains(JENKINS_JOB)
        && classes.contains(JENKINS_WORKFLOW_JOB)
        && classes.contains(BLUE_BRANCH)
        && classes.contains(BLUE_PIPELINE)
        && classes.contains(PULL_REQUEST));
}
 
Example 13
Source File: MultiBranchTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test @Ignore
public void getPipelineJobrRuns() 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"));
}
 
Example 14
Source File: MultiBranchTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void createUserFavouriteMultibranchBranchTest() throws Exception {
    j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
    hudson.model.User user = User.get("alice");
    user.setFullName("Alice Cooper");
    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());
    }

    WorkflowJob p = scheduleAndFindBranchProject(mp, "master");
    j.waitUntilNoActivity();

    WorkflowJob p1 = scheduleAndFindBranchProject(mp, "feature2");

    String token = getJwtToken(j.jenkins,"alice","alice");
    Map map = new RequestBuilder(baseUrl)
        .put("/organizations/jenkins/pipelines/p/branches/feature2/favorite")
        .jwtToken(token)
        .data(ImmutableMap.of("favorite", true))
        .build(Map.class);


    validatePipeline(p1, (Map) map.get("item"));

    Assert.assertEquals("/blue/rest/organizations/jenkins/pipelines/p/branches/feature2/favorite/", getHrefFromLinks(map, "self"));

    List l = new RequestBuilder(baseUrl)
        .get("/users/"+user.getId()+"/favorites/")
        .jwtToken(token)
        .build(List.class);

    Assert.assertEquals(1, l.size());

    Map branch = (Map)((Map)l.get(0)).get("item");

    Assert.assertEquals("/blue/rest/organizations/jenkins/pipelines/p/branches/feature2/favorite/", getHrefFromLinks((Map)l.get(0), "self"));

    validatePipeline(p1, branch);

    String c = (String) branch.get("_class");
    Assert.assertEquals(BranchImpl.class.getName(), c);


    map = new RequestBuilder(baseUrl)
        .put(getUrlFromHref(getHrefFromLinks((Map)l.get(0), "self")))
        .jwtToken(token)
        .data(ImmutableMap.of("favorite", false))
        .build(Map.class);


    validatePipeline(p1, (Map) map.get("item"));

    Assert.assertEquals("/blue/rest/organizations/jenkins/pipelines/p/branches/feature2/favorite/", getHrefFromLinks(map, "self"));

    l = new RequestBuilder(baseUrl)
        .get("/users/"+user.getId()+"/favorites/")
        .jwtToken(token)
        .build(List.class);

    Assert.assertEquals(0, l.size());


    new RequestBuilder(baseUrl)
        .get("/users/"+user.getId()+"/favorites/")
        .jwtToken(getJwtToken(j.jenkins,"bob","bob"))
        .status(403)
        .build(String.class);

}
 
Example 15
Source File: MultiBranchTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void getMultiBranchPipelineActivityRuns() throws Exception {
    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());
    }
    scheduleAndFindBranchProject(mp);
    j.waitUntilNoActivity();

    WorkflowJob p = findBranchProject(mp, "master");

    WorkflowRun b1 = p.getLastBuild();
    assertEquals(1, b1.getNumber());
    assertEquals(3, mp.getItems().size());

    //execute feature/ux-1 branch build
    p = findBranchProject(mp, "feature%2Fux-1");
    WorkflowRun b2 = p.getLastBuild();
    assertEquals(1, b2.getNumber());


    //execute feature 2 branch build
    p = findBranchProject(mp, "feature2");
    WorkflowRun b3 = p.getLastBuild();
    assertEquals(1, b3.getNumber());


    List<Map> resp = get("/organizations/jenkins/pipelines/p/runs", List.class);
    Assert.assertEquals(3, resp.size());
    Date d1 = new SimpleDateFormat(DATE_FORMAT_STRING).parse((String)resp.get(0).get("startTime"));
    Date d2 = new SimpleDateFormat(DATE_FORMAT_STRING).parse((String)resp.get(1).get("startTime"));
    Date d3 = new SimpleDateFormat(DATE_FORMAT_STRING).parse((String)resp.get(2).get("startTime"));

    Assert.assertTrue(d1.compareTo(d2) >= 0);
    Assert.assertTrue(d2.compareTo(d3) >= 0);

    for(Map m: resp){
        BuildData d;
        WorkflowRun r;
        if(m.get("pipeline").equals("master")){
            r = b1;
            d = b1.getAction(BuildData.class);
        } else if(m.get("pipeline").equals("feature2")){
            r = b3;
            d = b3.getAction(BuildData.class);
        } else{
            r = b2;
            d = b2.getAction(BuildData.class);
        }
        validateRun(r,m);
        String commitId = "";
        if(d != null) {
            commitId = d.getLastBuiltRevision().getSha1String();
            Assert.assertEquals(commitId, m.get("commitId"));
        }
    }
}
 
Example 16
Source File: MultiBranchTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void testMultiBranchPipelineQueueContainer() 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);
    Resource r = BluePipelineFactory.resolve(mp);
    assertTrue(r instanceof MultiBranchPipelineImpl);

    for (WorkflowJob job : mp.getItems()) {
        Queue.Item item = job.getQueueItem();
        job.setConcurrentBuild(false);
        job.scheduleBuild2(0);
        job.scheduleBuild2(0);
    }
    Queue.Item[] queueItems = Jenkins.getInstance().getQueue().getItems();
    MultiBranchPipelineQueueContainer mbpQueueContainer =
            new MultiBranchPipelineQueueContainer((MultiBranchPipelineImpl) r);
    Iterator<BlueQueueItem> blueQueueItems = mbpQueueContainer.iterator(0,100);
    if (queueItems.length > 0){
        assertTrue(mbpQueueContainer.iterator().hasNext());
        assertEquals("/blue/rest/organizations/jenkins/pipelines/p/queue/", mbpQueueContainer.getLink().getHref());
        BlueQueueItem blueQueueItem = mbpQueueContainer.get(String.valueOf(queueItems[0].getId()));
        assertNotNull(blueQueueItem);
        assertTrue(blueQueueItems.hasNext());
    }
}
 
Example 17
Source File: SseEventTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void multiBranchJobEventsWithCustomOrg() throws Exception {
    MockFolder folder = j.createFolder("TestOrgFolderName");
    assertNotNull(folder);

    setupScm();

    final OneShotEvent success = new OneShotEvent();

    final Boolean[] pipelineNameMatched = {null};
    final Boolean[] mbpPipelineNameMatched = {null};

    SSEConnection con = new SSEConnection(j.getURL(), "me", new ChannelSubscriber() {
        @Override
        public void onMessage(@Nonnull Message 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))) {
                    if("pipeline1".equals(message.get(blueocean_job_pipeline_name))) {
                        mbpPipelineNameMatched[0] = true;
                    }else{
                        mbpPipelineNameMatched[0] = false;
                    }
                } else if ("org.jenkinsci.plugins.workflow.job.WorkflowJob".equals(message.get(jenkins_object_type))) {
                    if("pipeline1".equals(message.get(blueocean_job_pipeline_name))) {
                        pipelineNameMatched[0] = true;
                    }else {
                        pipelineNameMatched[0] = false;
                    }
                }
            }
            if(pipelineNameMatched[0] != null && mbpPipelineNameMatched[0] != null){
                success.signal();
            }
        }
    });
    con.subscribe("pipeline");
    con.subscribe("job");

    final WorkflowMultiBranchProject mp = folder.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(pipelineNameMatched[0]);
        assertTrue(mbpPipelineNameMatched[0]);
    }
}
 
Example 18
Source File: RunImplTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@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"));
}
 
Example 19
Source File: PipelineNodeTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void encodedStepDescription() throws Exception {
    setupScm("pipeline {\n" +
                 "  agent any\n" +
                 "  stages {\n" +
                 "    stage('Build') {\n" +
                 "      steps {\n" +
                 "          sh 'echo \"\\033[32m some text \\033[0m\"'    \n" +
                 "      }\n" +
                 "    }\n" +
                 "  }\n" +
                 "}");
    WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false)));
    for (SCMSource source : mp.getSCMSources()) {
        assertEquals(mp, source.getOwner());
    }

    mp.scheduleBuild2(0).getFuture().get();

    j.waitUntilNoActivity();

    WorkflowJob p = scheduleAndFindBranchProject(mp, "master");
    j.waitUntilNoActivity();
    WorkflowRun b1 = p.getLastBuild();
    Assert.assertEquals(Result.SUCCESS, b1.getResult());

    List<FlowNode> stages = getStages(NodeGraphBuilder.NodeGraphBuilderFactory.getInstance(b1));

    Assert.assertEquals(1, stages.size());

    Assert.assertEquals("Build", stages.get(0).getDisplayName());

    List<Map> resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/nodes/", List.class);
    Assert.assertEquals(1, resp.size());
    Assert.assertEquals("Build", resp.get(0).get("displayName"));

    resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/steps/", List.class);
    Assert.assertEquals(2, resp.size());

    assertNotNull(resp.get(0).get("displayName"));

    assertEquals("Shell Script", resp.get(1).get("displayName"));
    assertEquals("echo \"\u001B[32m some text \u001B[0m\"", resp.get(1).get("displayDescription"));
}
 
Example 20
Source File: PipelineNodeTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void declarativeSyntheticSkippedStage() throws Exception {

    setupScm("pipeline {\n" +
                 "    agent any\n" +
                 "    stages {\n" +
                 "        stage(\"build\") {\n" +
                 "            steps{\n" +
                 "              sh 'echo \"Start Build\"'\n" +
                 "              echo 'End Build'\n" +
                 "            }\n" +
                 "        }\n" +
                 "        stage(\"SkippedStage\") {\n" +
                 "            when {\n" +
                 "        expression {\n" +
                 "                return false\n" +
                 "        }\n" +
                 "            }\n" +
                 "            steps {\n" +
                 "                script {\n" +
                 "                    echo \"World\"\n" +
                 "                    echo \"Heal it\"\n" +
                 "                }\n" +
                 "\n" +
                 "            }\n" +
                 "        }\n" +
                 "        stage(\"deploy\") {\n" +
                 "            steps{\n" +
                 "              sh 'echo \"Start Deploy\"'\n" +
                 "              sh 'echo \"Deploying...\"'\n" +
                 "              sh 'echo \"End Deploy\"'\n" +
                 "            }           \n" +
                 "        }\n" +
                 "    }\n" +
                 "    post {\n" +
                 "        failure {\n" +
                 "            echo \"failed\"\n" +
                 "        }\n" +
                 "        success {\n" +
                 "            echo \"success\"\n" +
                 "        }\n" +
                 "    }\n" +
                 "}");
    WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false)));
    for (SCMSource source : mp.getSCMSources()) {
        assertEquals(mp, source.getOwner());
    }

    mp.scheduleBuild2(0).getFuture().get();

    j.waitUntilNoActivity();

    WorkflowJob p = scheduleAndFindBranchProject(mp, "master");
    j.waitUntilNoActivity();
    WorkflowRun b1 = p.getLastBuild();
    Assert.assertEquals(Result.SUCCESS, b1.getResult());

    List<FlowNode> stages = getStages(NodeGraphBuilder.NodeGraphBuilderFactory.getInstance(b1));

    Assert.assertEquals(3, stages.size());

    Assert.assertEquals("build", stages.get(0).getDisplayName());
    Assert.assertEquals("SkippedStage", stages.get(1).getDisplayName());
    Assert.assertEquals("deploy", stages.get(2).getDisplayName());

    List<Map> resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/nodes/", List.class);
    Assert.assertEquals(3, resp.size());
    Assert.assertEquals("build", resp.get(0).get("displayName"));
    Assert.assertEquals("SkippedStage", resp.get(1).get("displayName"));
    Assert.assertEquals("deploy", resp.get(2).get("displayName"));
    //check status
    Assert.assertEquals("SUCCESS", resp.get(0).get("result"));
    Assert.assertEquals("FINISHED", resp.get(0).get("state"));
    Assert.assertEquals("NOT_BUILT", resp.get(1).get("result"));
    Assert.assertEquals("SKIPPED", resp.get(1).get("state"));
    Assert.assertEquals("SUCCESS", resp.get(2).get("result"));
    Assert.assertEquals("FINISHED", resp.get(2).get("state"));

    resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/steps/", List.class);
    Assert.assertEquals(7, resp.size());

    resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/nodes/" + stages.get(0).getId() + "/steps/", List.class);
    Assert.assertEquals(3, resp.size());

    resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/nodes/" + stages.get(1).getId() + "/steps/", List.class);
    Assert.assertEquals(0, resp.size());

    resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/nodes/" + stages.get(2).getId() + "/steps/", List.class);
    Assert.assertEquals(4, resp.size());
}