org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject Java Examples

The following examples show how to use org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject. 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 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 #2
Source File: GitLabSCMSourceDeserializationTest.java    From gitlab-branch-source-plugin with MIT License 6 votes vote down vote up
@Test
public void afterRestartingJenkinsTransientFieldsAreNotNull() throws Exception {
    plan.then(j -> {
        GitLabSCMSourceBuilder sb = new GitLabSCMSourceBuilder(SOURCE_ID, "server", "creds", "po", "group/project", "project");
        WorkflowMultiBranchProject project = j.createProject(WorkflowMultiBranchProject.class, PROJECT_NAME);
        project.getSourcesList().add(new BranchSource(sb.build()));
    });

    plan.then(j -> {
        SCMSource source = j.getInstance()
                .getAllItems(WorkflowMultiBranchProject.class)
                .stream().filter(p -> PROJECT_NAME.equals(p.getName()))
                .map(p -> p.getSCMSource(SOURCE_ID))
                .findFirst()
                .get();

        Class<? extends SCMSource> clazz = source.getClass();
        Field mergeRequestContributorCache = clazz.getDeclaredField("mergeRequestContributorCache");
        mergeRequestContributorCache.setAccessible(true);
        Field mergeRequestMetadataCache = clazz.getDeclaredField("mergeRequestMetadataCache");
        mergeRequestMetadataCache.setAccessible(true);
        assertNotNull(mergeRequestMetadataCache.get(source));
        assertNotNull(mergeRequestContributorCache.get(source));
    });
}
 
Example #3
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 #4
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 #5
Source File: DefaultsBinderTest.java    From pipeline-multibranch-defaults-plugin with MIT License 6 votes vote down vote up
@Test
public void testDefaultJenkinsFileLoadFromWorkspace() throws Exception {
    GlobalConfigFiles globalConfigFiles = r.jenkins.getExtensionList(GlobalConfigFiles.class).get(GlobalConfigFiles.class);
    ConfigFileStore store = globalConfigFiles.get();
    Config config = new GroovyScript("Jenkinsfile", "Jenkinsfile", "",
        "semaphore 'wait'; node {checkout scm; load 'Jenkinsfile'}");
    store.save(config);


    sampleGitRepo.init();
    sampleGitRepo.write("Jenkinsfile", "echo readFile('file')");
    sampleGitRepo.git("add", "Jenkinsfile");
    sampleGitRepo.write("file", "initial content");
    sampleGitRepo.git("commit", "--all", "--message=flow");
    WorkflowMultiBranchProject mp = r.jenkins.createProject(PipelineMultiBranchDefaultsProject.class, "p");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleGitRepo.toString(), "", "*", "", false),
        new DefaultBranchPropertyStrategy(new BranchProperty[0])));
    WorkflowJob p = PipelineMultiBranchDefaultsProjectTest.scheduleAndFindBranchProject(mp, "master");
    SemaphoreStep.waitForStart("wait/1", null);
    WorkflowRun b1 = p.getLastBuild();
    assertNotNull(b1);
    assertEquals(1, b1.getNumber());
    SemaphoreStep.success("wait/1", null);
    r.assertLogContains("initial content", r.waitForCompletion(b1));
}
 
Example #6
Source File: MultiBranchTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Test
public void getMultiBranchPipelineInsideFolder() throws IOException, ExecutionException, InterruptedException {
    MockFolder folder1 = j.createFolder("folder1");
    WorkflowMultiBranchProject mp = folder1.createProject(WorkflowMultiBranchProject.class, "p");
    mp.setDisplayName("My MBP");

    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/");

    validateMultiBranchPipeline(mp, r, 3);
    Assert.assertEquals("/blue/rest/organizations/jenkins/pipelines/folder1/pipelines/p/",
        ((Map)((Map)r.get("_links")).get("self")).get("href"));
    Assert.assertEquals("folder1/My%20MBP", r.get("fullDisplayName"));
    r = get("/organizations/jenkins/pipelines/folder1/pipelines/p/master/");
    Assert.assertEquals("folder1/My%20MBP/master", r.get("fullDisplayName"));
}
 
Example #7
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 #8
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 #9
Source File: GitHubSCMSourceTest.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
@Test
@Issue("JENKINS-48035")
public void testGitHubRepositoryNameContributor() throws IOException {
    WorkflowMultiBranchProject job = r.createProject(WorkflowMultiBranchProject.class);
    job.setSourcesList(Arrays.asList(new BranchSource(source)));
    Collection<GitHubRepositoryName> names = GitHubRepositoryNameContributor.parseAssociatedNames(job);
    assertThat(names, contains(allOf(
            hasProperty("userName", equalTo("cloudbeers")),
            hasProperty("repositoryName", equalTo("yolo"))
    )));
    //And specifically...
    names = new ArrayList<>();
    ExtensionList.lookup(GitHubRepositoryNameContributor.class).get(GitHubSCMSourceRepositoryNameContributor.class).parseAssociatedNames(job, names);
    assertThat(names, contains(allOf(
            hasProperty("userName", equalTo("cloudbeers")),
            hasProperty("repositoryName", equalTo("yolo"))
    )));
}
 
Example #10
Source File: DefaultsBinderTest.java    From pipeline-multibranch-defaults-plugin with MIT License 6 votes vote down vote up
@Test
public void testDefaultJenkinsFile() throws Exception {
    GlobalConfigFiles globalConfigFiles = r.jenkins.getExtensionList(GlobalConfigFiles.class).get(GlobalConfigFiles.class);
    ConfigFileStore store = globalConfigFiles.get();

    Config config = new GroovyScript("Jenkinsfile", "Jenkinsfile", "",
        "semaphore 'wait'; node {checkout scm; echo readFile('file')}");
    store.save(config);

    sampleGitRepo.init();
    sampleGitRepo.write("file", "initial content");
    sampleGitRepo.git("commit", "--all", "--message=flow");
    WorkflowMultiBranchProject mp = r.jenkins.createProject(PipelineMultiBranchDefaultsProject.class, "p");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleGitRepo.toString(), "", "*", "", false),
        new DefaultBranchPropertyStrategy(new BranchProperty[0])));
    WorkflowJob p = PipelineMultiBranchDefaultsProjectTest.scheduleAndFindBranchProject(mp, "master");
    SemaphoreStep.waitForStart("wait/1", null);
    WorkflowRun b1 = p.getLastBuild();
    assertNotNull(b1);
    assertEquals(1, b1.getNumber());
    SemaphoreStep.success("wait/1", null);
    r.assertLogContains("initial content", r.waitForCompletion(b1));
}
 
Example #11
Source File: MultiBranchPipelineImpl.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Override
public MultiBranchPipelineImpl getPipeline(Item item, Reachable parent, BlueOrganization organization) {
    if (item instanceof MultiBranchProject) {
        MultiBranchPipelineImpl mbpi = new MultiBranchPipelineImpl(organization, (MultiBranchProject) item);
        if (item instanceof WorkflowMultiBranchProject) {
            WorkflowMultiBranchProject wfmbp = (WorkflowMultiBranchProject)item;
            BranchProjectFactory<WorkflowJob, WorkflowRun> bpf = wfmbp.getProjectFactory();
            if (bpf instanceof WorkflowBranchProjectFactory) {
                String sp = ((WorkflowBranchProjectFactory) bpf).getScriptPath();
                mbpi.setScriptPath(sp);
            }
        }
        return mbpi;
    }
    return null;
}
 
Example #12
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 #13
Source File: MultiBranchTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Test
public void getMultiBranchPipelineRunStages() 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();
    WorkflowRun b1 = p.getLastBuild();
    assertEquals(1, b1.getNumber());
    assertEquals(3, mp.getItems().size());

    j.waitForCompletion(b1);

    List<Map> nodes = get("/organizations/jenkins/pipelines/p/branches/master/runs/1/nodes", List.class);

    Assert.assertEquals(3, nodes.size());
}
 
Example #14
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 #15
Source File: ForkPullRequestDiscoveryTrait2Test.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
private void assertRoundTrip(WorkflowMultiBranchProject p, SCMHeadAuthority<? super GitHubSCMSourceRequest, ? extends ChangeRequestSCMHead2, ? extends SCMRevision> trust, boolean configuredByUrl) throws Exception {

        GitHubSCMSource s;
        if (configuredByUrl)
            s = new GitHubSCMSource("", "", "https://github.com/nobody/nowhere", true);
        else
            s = new GitHubSCMSource("nobody", "nowhere", null, false);

        p.setSourcesList(Collections.singletonList(new BranchSource(s)));
        s.setTraits(Collections.singletonList(new ForkPullRequestDiscoveryTrait(0, trust)));
        r.configRoundtrip(p);
        List<SCMSourceTrait> traits = ((GitHubSCMSource) p.getSourcesList().get(0).getSource()).getTraits();
        assertEquals(1, traits.size());
        assertEquals(trust.getClass(), ((ForkPullRequestDiscoveryTrait) traits.get(0)).getTrust().getClass());
    }
 
Example #16
Source File: MultiBranchTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Test
public void testMultiBranchPipelineBranchUnsecurePermissions() throws IOException, ExecutionException, InterruptedException {
    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.assertTrue(permissions.get("create"));
    Assert.assertTrue(permissions.get("read"));
    Assert.assertTrue(permissions.get("start"));
    Assert.assertTrue(permissions.get("stop"));



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

    permissions = (Map<String, Boolean>) r.get("permissions");
    Assert.assertTrue(permissions.get("create"));
    Assert.assertTrue(permissions.get("start"));
    Assert.assertTrue(permissions.get("stop"));
    Assert.assertTrue(permissions.get("read"));
}
 
Example #17
Source File: AbstractMultiBranchCreateRequest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
private MultiBranchProject createMultiBranchProject(BlueOrganization organization) throws IOException {
    TopLevelItem item = createProject(getName(), DESCRIPTOR_NAME, MultiBranchProjectDescriptor.class, organization);
    if (!(item instanceof WorkflowMultiBranchProject)) {
        try {
            item.delete(); // we don't know about this project type
        } catch (InterruptedException e) {
            throw new ServiceException.UnexpectedErrorException("Failed to delete pipeline: " + getName());
        }
    }
    return (MultiBranchProject) item;
}
 
Example #18
Source File: GerritWebHookTriggerTest.java    From gerrit-code-review-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void gerritWebHookShouldTriggerMultiBranchPipelineProjectWithFolder() throws Exception {
  Folder f = j.jenkins.createProject(Folder.class, "folder");
  WorkflowMultiBranchProject mp = f.createProject(WorkflowMultiBranchProject.class, projectName);
  mp.getSourcesList().add(new BranchSource(getGerritSCMSource()));

  assertEquals(httpStatusOfPostGerritEventBodyToWebhookURI(), HttpServletResponse.SC_OK);
  j.waitUntilNoActivity();

  assertEquals(Result.SUCCESS, mp.getIndexing().getResult());
}
 
Example #19
Source File: GithubNotificationConfigTest.java    From github-autostatus-plugin with MIT License 5 votes vote down vote up
@Test
public void testConfigBranchSource() throws Exception {
    Run build = Mockito.mock(Run.class);
    SCMRevisionAction mockSCMRevisionAction = mock(SCMRevisionAction.class);
    when(build.getAction(SCMRevisionAction.class)).thenReturn(mockSCMRevisionAction);

    GitHubSCMSource source = mock(GitHubSCMSource.class);
    when(source.getCredentialsId()).thenReturn("git-user");
    when(source.getRepoOwner()).thenReturn("repo-owner");
    when(source.getRepository()).thenReturn("repo");
    BranchSCMHead head = new BranchSCMHead("test-branch");
    SCMRevisionImpl revision = new SCMRevisionImpl(head, "what-the-hash");
    when(mockSCMRevisionAction.getRevision()).thenReturn(revision);

    WorkflowMultiBranchProject mockProject = mock(WorkflowMultiBranchProject.class);
    WorkflowJob mockJob = new WorkflowJob(mockProject, "job-name");
    when(build.getParent()).thenReturn(mockJob);
    when(mockProject.getSCMSources()).thenReturn(Collections.singletonList(source));

    PowerMockito.mockStatic(CredentialsMatchers.class);
    when(CredentialsMatchers.firstOrNull(any(), any())).thenReturn(new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "user-pass", null, "git-user", "git-password"));

    GitHub github = mock(GitHub.class);
    GHUser mockUser = mock(GHUser.class);
    GHRepository mockRepo = mock(GHRepository.class);
    when(github.getUser(any())).thenReturn(mockUser);
    when(mockUser.getRepository(any())).thenReturn(mockRepo);
    GitHubBuilder builder = PowerMockito.mock(GitHubBuilder.class);

    PowerMockito.when(builder.withProxy(Matchers.<Proxy>anyObject())).thenReturn(builder);
    PowerMockito.when(builder.withOAuthToken(anyString(), anyString())).thenReturn(builder);
    PowerMockito.when(builder.build()).thenReturn(github);
    PowerMockito.when(builder.withEndpoint(any())).thenReturn(builder);

    GithubNotificationConfig instance = GithubNotificationConfig.fromRun(build, builder);
    assertEquals("what-the-hash", instance.getShaString());
    assertEquals("test-branch", instance.getBranchName());
}
 
Example #20
Source File: BlueOceanWebURLBuilderTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
private WorkflowJob findBranchProject(WorkflowMultiBranchProject mp, String name) throws Exception {
    WorkflowJob p = mp.getItem(Util.rawEncode(name));
    if (p == null) {
        mp.getIndexing().writeWholeLogTo(System.out);
        fail(name + " project not found");
    }
    return p;
}
 
Example #21
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 #22
Source File: PipelineBranchDefaultsProjectFactoryTest.java    From pipeline-multibranch-defaults-plugin with MIT License 5 votes vote down vote up
@Test
public void allBranches() throws Exception {
    sampleRepo.init();
    sampleRepo.git("checkout", "-b", "dev/main");
    sampleRepo.write("file", "initial content");
    sampleRepo.git("add", "file");
    sampleRepo.git("commit", "--all", "--message=flow");
    WorkflowMultiBranchProject mp = r.jenkins.createProject(PipelineMultiBranchDefaultsProject.class, "p");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false)));
    WorkflowJob p = PipelineMultiBranchDefaultsProjectTest.scheduleAndFindBranchProject(mp, "dev%2Fmain");
    assertEquals(2, mp.getItems().size());
}
 
Example #23
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 #24
Source File: PipelineMultiBranchDefaultsProjectTest.java    From pipeline-multibranch-defaults-plugin with MIT License 5 votes vote down vote up
public static
@Nonnull
WorkflowJob findBranchProject(@Nonnull WorkflowMultiBranchProject mp, @Nonnull String name) throws Exception {
    WorkflowJob p = mp.getItem(name);
    showIndexing(mp);
    if (p == null) {
        fail(name + " project not found");
    }
    return p;
}
 
Example #25
Source File: MultiBranchTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Test
public void getPipelineJobRunsNoBranches() throws Exception {
    WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo1.toString(), "", "*", "", false),
        new DefaultBranchPropertyStrategy(new BranchProperty[0])));

    List l = request().get("/organizations/jenkins/pipelines/p/runs").build(List.class);

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

    List branches = request().get("/organizations/jenkins/pipelines/p/runs").build(List.class);
    Assert.assertEquals(0, branches.size());

}
 
Example #26
Source File: PipelineScmTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Test
public void unauthenticatedUserContentAccessShouldFail() throws IOException, ExecutionException, InterruptedException {
    WorkflowMultiBranchProject mp = j.createProject(WorkflowMultiBranchProject.class, "mbp");
    mp.setDisplayName("My MBP");
    new RequestBuilder(baseUrl)
            .status(401)
            .get("/organizations/jenkins/pipelines/mbp/scm/content").build(Map.class);

}
 
Example #27
Source File: PipelineScmTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Test
public void AuthorizedUserContentAccessShouldSucceed() throws IOException, ExecutionException, InterruptedException, UnirestException {
    WorkflowMultiBranchProject mp = j.createProject(WorkflowMultiBranchProject.class, "mbp");
    mp.setDisplayName("My MBP");

    Map content = new RequestBuilder(baseUrl)
            .status(200)
            .jwtToken(getJwtToken(j.jenkins, bob.getId(), bob.getId()))
            .get("/organizations/jenkins/pipelines/mbp/scm/content").build(Map.class);
    assertNotNull(content);
    assertEquals("Hello World!", ((Map)content.get("content")).get("data"));
}
 
Example #28
Source File: PipelineScmTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Test
public void unauthenticatedUserContentUpdateShouldFail() throws IOException, ExecutionException, InterruptedException {
    WorkflowMultiBranchProject mp = j.createProject(WorkflowMultiBranchProject.class, "mbp");
    mp.setDisplayName("My MBP");
    new RequestBuilder(baseUrl)
            .status(401)
            .data(ImmutableMap.of("content",ImmutableMap.of("data","Hello World Again!")))
            .put("/organizations/jenkins/pipelines/mbp/scm/content").build(Map.class)
    ;
}
 
Example #29
Source File: PipelineScmTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Test
public void authorizedUserContentUpdateShouldSucceed() throws IOException, ExecutionException, InterruptedException, UnirestException {
    WorkflowMultiBranchProject mp = j.createProject(WorkflowMultiBranchProject.class, "mbp");
    mp.setDisplayName("My MBP");
    Map content = new RequestBuilder(baseUrl)
            .status(200)
            .jwtToken(getJwtToken(j.jenkins, bob.getId(), bob.getId()))
            .data(ImmutableMap.of("content",ImmutableMap.of("data","Hello World Again!")))
            .put("/organizations/jenkins/pipelines/mbp/scm/content").build(Map.class);

    assertNotNull(content);
    assertEquals("Hello World Again!", ((Map)content.get("content")).get("data"));
}
 
Example #30
Source File: BlueOceanWebURLBuilderTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Test
public void getMultiBranchPipelineInsideFolder() throws Exception {
    MockFolder folder1 = jenkinsRule.createFolder("folder1");
    MockFolder folder2 = folder1.createProject(MockFolder.class, "folder two with spaces");
    WorkflowMultiBranchProject mp = folder2.createProject(WorkflowMultiBranchProject.class, "p");

    String blueOceanURL;

    blueOceanURL = urlMapper.getUrl(mp);
    assertURL("blue/organizations/jenkins/folder1%2Ffolder%20two%20with%20spaces%2Fp/branches/", blueOceanURL);

    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();
    jenkinsRule.waitUntilNoActivity();

    // All branch jobs should just resolve back to the same top level branches
    // page for the multibranch job in Blue Ocean.
    WorkflowJob masterJob = findBranchProject(mp, "master");
    blueOceanURL = urlMapper.getUrl(masterJob);
    assertURL("blue/organizations/jenkins/folder1%2Ffolder%20two%20with%20spaces%2Fp/branches/", blueOceanURL);
    WorkflowJob featureUx1Job = findBranchProject(mp, "feature/ux-1");
    blueOceanURL = urlMapper.getUrl(featureUx1Job);
    assertURL("blue/organizations/jenkins/folder1%2Ffolder%20two%20with%20spaces%2Fp/branches/", blueOceanURL);
    WorkflowJob feature2Job = findBranchProject(mp, "feature2");
    blueOceanURL = urlMapper.getUrl(feature2Job);
    assertURL("blue/organizations/jenkins/folder1%2Ffolder%20two%20with%20spaces%2Fp/branches/", blueOceanURL);

    // Runs on the jobs
    blueOceanURL = urlMapper.getUrl(masterJob.getFirstBuild());
    assertURL("blue/organizations/jenkins/folder1%2Ffolder%20two%20with%20spaces%2Fp/detail/master/1/", blueOceanURL);
    blueOceanURL = urlMapper.getUrl(featureUx1Job.getFirstBuild());
    assertURL("blue/organizations/jenkins/folder1%2Ffolder%20two%20with%20spaces%2Fp/detail/feature%2Fux-1/1/", blueOceanURL);
}