Search in sources :

Example 1 with BbSaveContentResponse

use of io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbSaveContentResponse in project blueocean-plugin by jenkinsci.

the class BitbucketApiTest method createNewRepoContent.

@Test
public void createNewRepoContent() throws JsonProcessingException, UnsupportedEncodingException {
    // create new file
    BbSaveContentResponse saveResponse = api.saveContent("vivekp7", "demo1", "foo", "This is test content in new file", "first commit", "null", null, null);
    assertNotNull(saveResponse.getCommitId());
    String content = api.getContent("vivekp7", "demo1", "foo", (String) saveResponse.getCommitId());
    assertEquals("This is test content in new file", content);
}
Also used : BbSaveContentResponse(io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbSaveContentResponse) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with BbSaveContentResponse

use of io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbSaveContentResponse in project blueocean-plugin by jenkinsci.

the class BitbucketApiTest method testEmptyRepo.

@Test
public void testEmptyRepo() {
    boolean empty = api.isEmptyRepo("TESTP", "empty-repo-test");
    assertTrue(empty);
    // create new file
    BbSaveContentResponse saveResponse = api.saveContent("TESTP", "empty-repo-test", "README.md", "This is test content in new file", "another commit", "master", null, null);
    assertNotNull(saveResponse.getCommitId());
    String content = api.getContent("TESTP", "empty-repo-test", "README.md", (String) saveResponse.getCommitId());
    assertEquals("This is test content in new file", content);
}
Also used : BbSaveContentResponse(io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbSaveContentResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with BbSaveContentResponse

use of io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbSaveContentResponse in project blueocean-plugin by jenkinsci.

the class BitbucketApiTest method createNewRepoContent.

@Test
public void createNewRepoContent() throws JsonProcessingException, UnsupportedEncodingException {
    boolean exists = api.fileExists("TESTP", "pipeline-demo-test", "README.md", "master");
    assertFalse(exists);
    // create new file
    BbSaveContentResponse saveResponse = api.saveContent("TESTP", "pipeline-demo-test", "README.md", "This is test content in new file", "another commit", "master", null, null);
    assertNotNull(saveResponse.getCommitId());
    String content = api.getContent("TESTP", "pipeline-demo-test", "README.md", (String) saveResponse.getCommitId());
    assertEquals("This is test content in new file", content);
}
Also used : BbSaveContentResponse(io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbSaveContentResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with BbSaveContentResponse

use of io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbSaveContentResponse 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)

Aggregations

BbSaveContentResponse (io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbSaveContentResponse)4 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 ErrorMessage (io.jenkins.blueocean.commons.ErrorMessage)1 ServiceException (io.jenkins.blueocean.commons.ServiceException)1 GitContent (io.jenkins.blueocean.rest.impl.pipeline.scm.GitContent)1 ScmFile (io.jenkins.blueocean.rest.impl.pipeline.scm.ScmFile)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 JSONObject (net.sf.json.JSONObject)1