Search in sources :

Example 16 with MultiBranchProject

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

the class GithubScmContentProviderTest method testScmSourceProperties.

private void testScmSourceProperties(String mockedApiUrl) throws Exception {
    // ensure the cloud provider works with cloud org folder
    String credentialId = createGithubCredential(user);
    MultiBranchProject mbp = mockMbp(credentialId, user, GithubScm.DOMAIN_NAME);
    // unfortunately overriding the GitHub apiUrl for WireMock returns a "localhost" URL here, so we mock the call
    when(((GitHubSCMSource) mbp.getSCMSources().get(0)).getApiUri()).thenReturn(mockedApiUrl);
    ScmContentProvider provider = new GithubScmContentProvider();
    assertTrue("github provider should support github multi-branch folder", provider.support(mbp));
    assertEquals(GithubScm.ID, provider.getScmId());
    assertEquals(mockedApiUrl, provider.getApiUrl(mbp));
    // ensure the cloud provider doesn't support enterprise org folder
    mbp = mockMbp(createGithubEnterpriseCredential(), user, GithubEnterpriseScm.DOMAIN_NAME);
    assertFalse("github provider should not support github enterprise org folder", provider.support(mbp));
}
Also used : ScmContentProvider(io.jenkins.blueocean.rest.impl.pipeline.ScmContentProvider) MultiBranchProject(jenkins.branch.MultiBranchProject) GitHubSCMSource(org.jenkinsci.plugins.github_branch_source.GitHubSCMSource)

Example 17 with MultiBranchProject

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

the class GithubScmContentProviderTest method unauthorizedSaveContentToMbpShouldFail.

@Test
public void unauthorizedSaveContentToMbpShouldFail() throws UnirestException, IOException {
    User alice = User.get("alice");
    alice.setFullName("Alice Cooper");
    alice.addProperty(new Mailer.UserProperty("alice@jenkins-ci.org"));
    String aliceCredentialId = createGithubCredential(alice);
    StaplerRequest staplerRequest = mockStapler();
    GitContent content = new GitContent.Builder().autoCreateBranch(true).base64Data("c2xlZXAgMTUKbm9kZSB7CiAgY2hlY2tvdXQgc2NtCiAgc2ggJ2xzIC1sJwp9\\nCnNsZWVwIDE1Cg==\\n").branch("test1").message("another commit").owner("cloudbeers").path("Jankinsfile").repo("PR-demo").sha("e23b8ef5c2c4244889bf94db6c05cc08ea138aef").build();
    when(staplerRequest.bindJSON(Mockito.eq(GithubScmSaveFileRequest.class), Mockito.any(JSONObject.class))).thenReturn(new GithubScmSaveFileRequest(content));
    MultiBranchProject mbp = mockMbp(aliceCredentialId, user, GithubScm.DOMAIN_NAME);
    String request = "{\n" + "  \"content\" : {\n" + "    \"message\" : \"first commit\",\n" + "    \"path\" : \"Jenkinsfile\",\n" + "    \"branch\" : \"test1\",\n" + "    \"repo\" : \"PR-demo\",\n" + "    \"sha\" : \"e23b8ef5c2c4244889bf94db6c05cc08ea138aef\",\n" + "    \"base64Data\" : " + "\"c2xlZXAgMTUKbm9kZSB7CiAgY2hlY2tvdXQgc2NtCiAgc2ggJ2xzIC1sJwp9\\nCnNsZWVwIDE1Cg==\\n\"" + "  }\n" + "}";
    when(staplerRequest.getReader()).thenReturn(new BufferedReader(new StringReader(request), request.length()));
    try {
        // Bob trying to access content but his credential is not setup so should fail
        new GithubScmContentProvider().saveContent(staplerRequest, mbp);
    } catch (ServiceException.PreconditionRequired e) {
        assertEquals("Can't access content from github: no credential found", e.getMessage());
        return;
    }
    fail("Should have failed with PreConditionException");
}
Also used : User(hudson.model.User) StaplerRequest(org.kohsuke.stapler.StaplerRequest) Mailer(hudson.tasks.Mailer) GitContent(io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent) JSONObject(net.sf.json.JSONObject) ServiceException(io.jenkins.blueocean.commons.ServiceException) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) MultiBranchProject(jenkins.branch.MultiBranchProject) Test(org.junit.Test)

Example 18 with MultiBranchProject

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

the class GithubScmContentProviderTest method unauthorizedSaveContentToMbpGHEShouldFail.

@Test
public void unauthorizedSaveContentToMbpGHEShouldFail() throws UnirestException, IOException {
    User alice = User.get("alice");
    alice.setFullName("Alice Cooper");
    alice.addProperty(new Mailer.UserProperty("alice@jenkins-ci.org"));
    String aliceCredentialId = createGithubEnterpriseCredential(alice);
    StaplerRequest staplerRequest = mockStapler(GithubEnterpriseScm.ID);
    GitContent content = new GitContent.Builder().autoCreateBranch(true).base64Data("c2xlZXAgMTUKbm9kZSB7CiAgY2hlY2tvdXQgc2NtCiAgc2ggJ2xzIC1sJwp9\\nCnNsZWVwIDE1Cg==\\n").branch("test1").message("another commit").owner("cloudbeers").path("Jankinsfile").repo("PR-demo").sha("e23b8ef5c2c4244889bf94db6c05cc08ea138aef").build();
    when(staplerRequest.bindJSON(Mockito.eq(GithubScmSaveFileRequest.class), Mockito.any(JSONObject.class))).thenReturn(new GithubScmSaveFileRequest(content));
    MultiBranchProject mbp = mockMbp(aliceCredentialId, user, GithubEnterpriseScm.DOMAIN_NAME);
    String request = "{\n" + "  \"content\" : {\n" + "    \"message\" : \"first commit\",\n" + "    \"path\" : \"Jenkinsfile\",\n" + "    \"branch\" : \"test1\",\n" + "    \"repo\" : \"PR-demo\",\n" + "    \"sha\" : \"e23b8ef5c2c4244889bf94db6c05cc08ea138aef\",\n" + "    \"base64Data\" : " + "\"c2xlZXAgMTUKbm9kZSB7CiAgY2hlY2tvdXQgc2NtCiAgc2ggJ2xzIC1sJwp9\\nCnNsZWVwIDE1Cg==\\n\"" + "  }\n" + "}";
    when(staplerRequest.getReader()).thenReturn(new BufferedReader(new StringReader(request), request.length()));
    try {
        // Bob trying to access content but his credential is not setup so should fail
        new GithubScmContentProvider().saveContent(staplerRequest, mbp);
    } catch (ServiceException.PreconditionRequired e) {
        assertEquals("Can't access content from github: no credential found", e.getMessage());
        return;
    }
    fail("Should have failed with PreConditionException");
}
Also used : User(hudson.model.User) StaplerRequest(org.kohsuke.stapler.StaplerRequest) Mailer(hudson.tasks.Mailer) GitContent(io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent) JSONObject(net.sf.json.JSONObject) ServiceException(io.jenkins.blueocean.commons.ServiceException) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) MultiBranchProject(jenkins.branch.MultiBranchProject) Test(org.junit.Test)

Example 19 with MultiBranchProject

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

the class GithubScmContentProviderTest method getContentForOrgFolder.

@Test
public void getContentForOrgFolder() throws UnirestException {
    String credentialId = createGithubCredential(user);
    StaplerRequest staplerRequest = mockStapler();
    MultiBranchProject mbp = mockMbp(credentialId, user, GithubScm.DOMAIN_NAME);
    GithubFile content = (GithubFile) new GithubScmContentProvider().getContent(staplerRequest, mbp);
    assertEquals("Jenkinsfile", content.getContent().getName());
    assertEquals("e23b8ef5c2c4244889bf94db6c05cc08ea138aef", content.getContent().getSha());
    assertEquals("PR-demo", content.getContent().getRepo());
    assertEquals("cloudbeers", content.getContent().getOwner());
}
Also used : StaplerRequest(org.kohsuke.stapler.StaplerRequest) MultiBranchProject(jenkins.branch.MultiBranchProject) Test(org.junit.Test)

Example 20 with MultiBranchProject

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

the class GitScmTest method createMbp.

private String createMbp(User user) throws UnirestException {
    Map<String, Object> resp = new RequestBuilder(baseUrl).status(201).jwtToken(getJwtToken(j.jenkins, user.getId(), user.getId())).post("/organizations/" + getOrgName() + "/pipelines/").data(MapsHelper.of("name", "demo", "$class", "io.jenkins.blueocean.blueocean_git_pipeline.GitPipelineCreateRequest", "scmConfig", MapsHelper.of("uri", sampleRepo.fileUrl()))).build(Map.class);
    assertEquals("demo", resp.get("name"));
    Item item = getOrgRoot().getItem("demo");
    assertNotNull(item);
    assertTrue(item instanceof MultiBranchProject);
    return "demo";
}
Also used : Item(hudson.model.Item) MultiBranchProject(jenkins.branch.MultiBranchProject)

Aggregations

MultiBranchProject (jenkins.branch.MultiBranchProject)52 Test (org.junit.Test)36 StaplerRequest (org.kohsuke.stapler.StaplerRequest)28 GitContent (io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent)18 BufferedReader (java.io.BufferedReader)16 StringReader (java.io.StringReader)16 JSONObject (net.sf.json.JSONObject)16 ServiceException (io.jenkins.blueocean.commons.ServiceException)14 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)14 User (hudson.model.User)13 Mailer (hudson.tasks.Mailer)12 BitbucketScmSaveFileRequest (io.jenkins.blueocean.blueocean_bitbucket_pipeline.BitbucketScmSaveFileRequest)6 Job (hudson.model.Job)5 DescribableList (hudson.util.DescribableList)5 ScmFile (io.jenkins.blueocean.rest.impl.pipeline.scm.ScmFile)5 Map (java.util.Map)5 GitSCMSource (jenkins.plugins.git.GitSCMSource)5 GitHubSCMSource (org.jenkinsci.plugins.github_branch_source.GitHubSCMSource)5 BitbucketSCMSource (com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMSource)4 Item (hudson.model.Item)4