Search in sources :

Example 6 with GitContent

use of io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent 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 7 with GitContent

use of io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent in project blueocean-plugin by jenkinsci.

the class GithubScmContentProviderTest method unauthorizedSaveContentToOrgFolderShouldFail.

@Test
public void unauthorizedSaveContentToOrgFolderShouldFail() 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 8 with GitContent

use of io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent in project blueocean-plugin by jenkinsci.

the class GithubScmContentProviderTest method saveContentNewBranchToMbp.

@Test
public void saveContentNewBranchToMbp() throws UnirestException, IOException {
    String credentialId = createGithubCredential(user);
    StaplerRequest staplerRequest = mockStapler();
    GitContent content = new GitContent.Builder().autoCreateBranch(true).base64Data("c2xlZXAgMTUKbm9kZSB7CiAgY2hlY2tvdXQgc2NtCiAgc2ggJ2xzIC1sJwp9\\nCnNsZWVwIDE1Cg==\\n").branch("test2").message("another commit").sourceBranch("master").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(credentialId, user, GithubScm.DOMAIN_NAME);
    String request = "{\n" + "  \"content\" : {\n" + "    \"message\" : \"first commit\",\n" + "    \"path\" : \"Jenkinsfile\",\n" + "    \"branch\" : \"test2\",\n" + "    \"sourceBranch\" : \"master\",\n" + "    \"repo\" : \"PR-demo\",\n" + "    \"sha\" : \"e23b8ef5c2c4244889bf94db6c05cc08ea138aef\",\n" + "    \"base64Data\" : " + "\"c2xlZXAgMTUKbm9kZSB7CiAgY2hlY2tvdXQgc2NtCiAgc2ggJ2xzIC1sJwp9\\nCnNsZWVwIDE1Cg==\\n\"" + "  }\n" + "}";
    when(staplerRequest.getReader()).thenReturn(new BufferedReader(new StringReader(request), request.length()));
    GithubFile file = (GithubFile) new GithubScmContentProvider().saveContent(staplerRequest, mbp);
    assertEquals("Jenkinsfile", file.getContent().getName());
    assertEquals("e23b8ef5c2c4244889bf94db6c05cc08ea138aef", file.getContent().getSha());
    assertEquals("PR-demo", file.getContent().getRepo());
    assertEquals("cloudbeers", file.getContent().getOwner());
}
Also used : JSONObject(net.sf.json.JSONObject) StaplerRequest(org.kohsuke.stapler.StaplerRequest) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) MultiBranchProject(jenkins.branch.MultiBranchProject) GitContent(io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent) Test(org.junit.Test)

Example 9 with GitContent

use of io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent in project blueocean-plugin by jenkinsci.

the class BitbucketCloudScmContentProviderTest method unauthorizedSaveContentShouldFail.

@Test
public void unauthorizedSaveContentShouldFail() throws UnirestException, IOException {
    User alice = User.get("alice");
    alice.setFullName("Alice Cooper");
    alice.addProperty(new Mailer.UserProperty("alice@jenkins-ci.org"));
    String aliceCredentialId = createCredential(BitbucketCloudScm.ID, alice);
    StaplerRequest staplerRequest = mockStapler();
    MultiBranchProject mbp = mockMbp(aliceCredentialId, alice);
    GitContent content = new GitContent.Builder().autoCreateBranch(true).base64Data("bm9kZXsKICBlY2hvICdoZWxsbyB3b3JsZCEnCn0K").branch("master").message("new commit").owner("TESTP").path("README.md").repo("pipeline-demo-test").build();
    when(staplerRequest.bindJSON(Mockito.eq(BitbucketScmSaveFileRequest.class), Mockito.any(JSONObject.class))).thenReturn(new BitbucketScmSaveFileRequest(content));
    String request = "{\n" + "  \"content\" : {\n" + "    \"message\" : \"new commit\",\n" + "    \"path\" : \"README.md\",\n" + "    \"branch\" : \"master\",\n" + "    \"repo\" : \"pipeline-demo-test\",\n" + "    \"base64Data\" : " + "\"bm9kZXsKICBlY2hvICdoZWxsbyB3b3JsZCEnCn0K\"" + "  }\n" + "}";
    when(staplerRequest.getReader()).thenReturn(new BufferedReader(new StringReader(request), request.length()));
    try {
        new BitbucketCloudScmContentProvider().saveContent(staplerRequest, mbp);
    } catch (ServiceException.PreconditionRequired e) {
        assertEquals("Can't access content from Bitbucket: 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) BitbucketScmSaveFileRequest(io.jenkins.blueocean.blueocean_bitbucket_pipeline.BitbucketScmSaveFileRequest) MultiBranchProject(jenkins.branch.MultiBranchProject) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 10 with GitContent

use of io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent in project blueocean-plugin by jenkinsci.

the class BitbucketServerScmContentProviderTest method newContent.

@Test
public void newContent() throws UnirestException, IOException {
    String credentialId = createCredential(BitbucketServerScm.ID);
    StaplerRequest staplerRequest = mockStapler();
    MultiBranchProject mbp = mockMbp(credentialId);
    GitContent content = new GitContent.Builder().autoCreateBranch(true).base64Data("bm9kZXsKICBlY2hvICdoZWxsbyB3b3JsZCEnCn0K").branch("master").message("new commit").owner("TESTP").path("README.md").repo("pipeline-demo-test").build();
    when(staplerRequest.bindJSON(Mockito.eq(BitbucketScmSaveFileRequest.class), Mockito.any(JSONObject.class))).thenReturn(new BitbucketScmSaveFileRequest(content));
    String request = "{\n" + "  \"content\" : {\n" + "    \"message\" : \"new commit\",\n" + "    \"path\" : \"README.md\",\n" + "    \"branch\" : \"master\",\n" + "    \"repo\" : \"pipeline-demo-test\",\n" + "    \"base64Data\" : " + "\"bm9kZXsKICBlY2hvICdoZWxsbyB3b3JsZCEnCn0K\"" + "  }\n" + "}";
    when(staplerRequest.getReader()).thenReturn(new BufferedReader(new StringReader(request), request.length()));
    ScmFile<GitContent> respContent = (ScmFile<GitContent>) new BitbucketServerScmContentProvider().saveContent(staplerRequest, mbp);
    assertEquals("README.md", respContent.getContent().getName());
    assertEquals("a77840d4108db2befe6c616723eb3f4485af5d24", respContent.getContent().getCommitId());
    assertEquals("pipeline-demo-test", respContent.getContent().getRepo());
    assertEquals("TESTP", respContent.getContent().getOwner());
    assertEquals("master", respContent.getContent().getBranch());
}
Also used : JSONObject(net.sf.json.JSONObject) StaplerRequest(org.kohsuke.stapler.StaplerRequest) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) BitbucketScmSaveFileRequest(io.jenkins.blueocean.blueocean_bitbucket_pipeline.BitbucketScmSaveFileRequest) MultiBranchProject(jenkins.branch.MultiBranchProject) GitContent(io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent) ScmFile(io.jenkins.blueocean.rest.impl.pipeline.scm.ScmFile) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

GitContent (io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent)21 MultiBranchProject (jenkins.branch.MultiBranchProject)17 JSONObject (net.sf.json.JSONObject)17 Test (org.junit.Test)17 StaplerRequest (org.kohsuke.stapler.StaplerRequest)17 BufferedReader (java.io.BufferedReader)15 StringReader (java.io.StringReader)15 ServiceException (io.jenkins.blueocean.commons.ServiceException)10 User (hudson.model.User)8 ScmFile (io.jenkins.blueocean.rest.impl.pipeline.scm.ScmFile)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 Mailer (hudson.tasks.Mailer)6 BitbucketScmSaveFileRequest (io.jenkins.blueocean.blueocean_bitbucket_pipeline.BitbucketScmSaveFileRequest)5 IOException (java.io.IOException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 BbBranch (io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbBranch)1 BbSaveContentResponse (io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbSaveContentResponse)1 ErrorMessage (io.jenkins.blueocean.commons.ErrorMessage)1