Search in sources :

Example 1 with ScmFile

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

Example 2 with ScmFile

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

the class BitbucketServerScmContentProviderTest method updateContent.

@Test
public void updateContent() 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("another commit").owner("TESTP").path("Jenkinsfile").repo("pipeline-demo-test").commitId("0bae0ddbed2e897d3b44abc3aca9ba26e2f61710").build();
    when(staplerRequest.bindJSON(Mockito.eq(BitbucketScmSaveFileRequest.class), Mockito.any(JSONObject.class))).thenReturn(new BitbucketScmSaveFileRequest(content));
    String request = "{\n" + "  \"content\" : {\n" + "    \"message\" : \"another commit\",\n" + "    \"path\" : \"Jenkinsfile\",\n" + "    \"branch\" : \"master\",\n" + "    \"repo\" : \"pipeline-demo-test\",\n" + "    \"commitId\" : \"0bae0ddbed2e897d3b44abc3aca9ba26e2f61710\",\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("Jenkinsfile", respContent.getContent().getName());
    assertEquals("e587b620844b1b230783976f00cfb8383488aeca", 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)

Example 3 with ScmFile

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

the class AbstractBitbucketScmContentProvider method getContent.

@Override
protected Object getContent(ScmGetRequest request) {
    BitbucketApi api = BitbucketServerScm.getApi(request.getApiUrl(), this.getScmId(), request.getCredentials());
    BbBranch branch = null;
    String branchName = request.getBranch();
    BbBranch defaultBranch = api.getDefaultBranch(request.getOwner(), request.getRepo());
    if (defaultBranch == null) {
        // empty repo
        throw new ServiceException.NotFoundException(request.getPath() + " not found. This is empty and un-initialized repository");
    }
    if (branchName == null) {
        branch = defaultBranch;
    }
    if (branchName != null) {
        branch = api.getBranch(request.getOwner(), request.getRepo(), branchName);
        // Given branchName create this branch
        if (branch == null) {
            throw new ServiceException.BadRequestException("branch: " + branchName + " not found");
        }
    }
    String content = api.getContent(request.getOwner(), request.getRepo(), request.getPath(), branch.getLatestCommit());
    try {
        final GitContent gitContent = new GitContent.Builder().base64Data(Base64.encodeBase64String(content.getBytes("UTF-8"))).branch(request.getBranch()).size(content.length()).path(request.getPath()).owner(request.getOwner()).repo(request.getRepo()).name(request.getPath()).sha(branch.getLatestCommit()).commitId(branch.getLatestCommit()).build();
        return new ScmFile<GitContent>() {

            @Override
            public GitContent getContent() {
                return gitContent;
            }
        };
    } catch (UnsupportedEncodingException e) {
        throw new ServiceException.UnexpectedErrorException("Failed to base64 encode content: " + e.getMessage(), e);
    }
}
Also used : ServiceException(io.jenkins.blueocean.commons.ServiceException) BbBranch(io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbBranch) UnsupportedEncodingException(java.io.UnsupportedEncodingException) GitContent(io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent) ScmFile(io.jenkins.blueocean.rest.impl.pipeline.scm.ScmFile)

Example 4 with ScmFile

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

the class AbstractBitbucketScmContentProvider method saveContent.

@Override
public Object saveContent(@Nonnull StaplerRequest staplerRequest, @Nonnull Item item) {
    JSONObject body;
    try {
        body = JSONObject.fromObject(IOUtils.toString(staplerRequest.getReader()));
    } catch (IOException e) {
        throw new ServiceException.UnexpectedErrorException("Failed to read request body");
    }
    BitbucketScmSaveFileRequest scmSaveFileRequest = staplerRequest.bindJSON(BitbucketScmSaveFileRequest.class, body);
    if (scmSaveFileRequest == null) {
        throw new ServiceException.BadRequestException(new ErrorMessage(400, "Failed to bind request"));
    }
    GitContent gitContent = scmSaveFileRequest.getContent();
    BitbucketScmParams scmParamsFromItem = new BitbucketScmParams(item);
    String owner = scmParamsFromItem.getOwner();
    String repo = scmParamsFromItem.getRepo();
    String commitId = StringUtils.isNotBlank(gitContent.getCommitId()) ? gitContent.getCommitId() : gitContent.getSha();
    BitbucketApi api = BitbucketServerScm.getApi(scmParamsFromItem.getApiUrl(), this.getScmId(), scmParamsFromItem.getCredentials());
    String content;
    try {
        content = new String(Base64.decodeBase64(gitContent.getBase64Data()), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new ServiceException.UnexpectedErrorException(e.getMessage(), e);
    }
    String message = gitContent.getMessage();
    if (message == null) {
        message = gitContent.getPath() + " created with BlueOcean";
    }
    BbSaveContentResponse response = api.saveContent(owner, repo, gitContent.getPath(), content, message, gitContent.getBranch(), gitContent.getSourceBranch(), commitId);
    final GitContent respContent = new GitContent.Builder().branch(gitContent.getBranch()).path(gitContent.getPath()).owner(gitContent.getOwner()).repo(gitContent.getRepo()).sha(response.getCommitId()).name(gitContent.getPath()).commitId(response.getCommitId()).build();
    return new ScmFile<GitContent>() {

        @Override
        public GitContent getContent() {
            return respContent;
        }
    };
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) GitContent(io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent) ScmFile(io.jenkins.blueocean.rest.impl.pipeline.scm.ScmFile) JSONObject(net.sf.json.JSONObject) ServiceException(io.jenkins.blueocean.commons.ServiceException) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage) BbSaveContentResponse(io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbSaveContentResponse)

Example 5 with ScmFile

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

the class BitbucketCloudScmContentProviderTest method newContent.

@Test
public void newContent() throws UnirestException, IOException {
    String credentialId = createCredential(BitbucketCloudScm.ID);
    StaplerRequest staplerRequest = mockStapler();
    MultiBranchProject mbp = mockMbp(credentialId);
    GitContent content = new GitContent.Builder().autoCreateBranch(true).base64Data("VGhpcyBpcyB0ZXN0IGNvbnRlbnQgaW4gbmV3IGZpbGUK").branch("master").message("new commit").owner("vivekp7").path("foo").repo("demo1").build();
    when(staplerRequest.bindJSON(Mockito.eq(BitbucketScmSaveFileRequest.class), Mockito.any(JSONObject.class))).thenReturn(new BitbucketScmSaveFileRequest(content));
    String request = "{\n" + "  \"content\" : {\n" + "    \"message\" : \"first commit\",\n" + "    \"path\" : \"foo\",\n" + "    \"branch\" : \"master\",\n" + "    \"repo\" : \"demo1\",\n" + "    \"base64Data\" : " + "\"VGhpcyBpcyB0ZXN0IGNvbnRlbnQgaW4gbmV3IGZpbGUK\"" + "  }\n" + "}";
    when(staplerRequest.getReader()).thenReturn(new BufferedReader(new StringReader(request), request.length()));
    ScmFile<GitContent> respContent = (ScmFile<GitContent>) new BitbucketCloudScmContentProvider().saveContent(staplerRequest, mbp);
    assertEquals("foo", respContent.getContent().getName());
    assertEquals(respContent.getContent().getCommitId(), respContent.getContent().getCommitId());
    assertEquals("demo1", respContent.getContent().getRepo());
    assertEquals("vivekp7", 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)7 ScmFile (io.jenkins.blueocean.rest.impl.pipeline.scm.ScmFile)7 MultiBranchProject (jenkins.branch.MultiBranchProject)5 Test (org.junit.Test)5 StaplerRequest (org.kohsuke.stapler.StaplerRequest)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 JSONObject (net.sf.json.JSONObject)4 BitbucketScmSaveFileRequest (io.jenkins.blueocean.blueocean_bitbucket_pipeline.BitbucketScmSaveFileRequest)3 BufferedReader (java.io.BufferedReader)3 StringReader (java.io.StringReader)3 ServiceException (io.jenkins.blueocean.commons.ServiceException)2 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 IOException (java.io.IOException)1