Search in sources :

Example 1 with BbCloudSaveContentResponse

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

the class BitbucketCloudApi method saveContent.

@Nonnull
@Override
public BbSaveContentResponse saveContent(@Nonnull String orgId, @Nonnull String repoSlug, @Nonnull String path, @Nonnull String content, @Nonnull String commitMessage, @Nullable String branch, @Nullable String sourceBranch, @Nullable String commitId) {
    MultipartEntityBuilder builder = MultipartEntityBuilder.create().addTextBody(path, content).addTextBody("message", commitMessage);
    if (isNotBlank(branch)) {
        builder.addTextBody("branch", branch);
    }
    if (isNotBlank(commitId)) {
        builder.addTextBody("parents", commitId);
    }
    HttpEntity entity = builder.build();
    HttpResponse response = request.post(String.format("%s/%s/%s/src", baseUrl + "repositories", orgId, repoSlug), entity);
    int status = response.getStatus();
    if (status == 201) {
        String location = response.getHeader("Location");
        if (location == null) {
            throw new ServiceException.UnexpectedErrorException("Location header is missing on save content response");
        }
        String cid = location.substring(location.lastIndexOf("/") + 1);
        return new BbCloudSaveContentResponse(cid);
    } else {
        throw new ServiceException.UnexpectedErrorException("Failed to save file: " + path + " server returned status: " + status);
    }
}
Also used : MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) HttpEntity(org.apache.http.HttpEntity) HttpResponse(io.jenkins.blueocean.blueocean_bitbucket_pipeline.HttpResponse) BbCloudSaveContentResponse(io.jenkins.blueocean.blueocean_bitbucket_pipeline.cloud.model.BbCloudSaveContentResponse) Nonnull(javax.annotation.Nonnull)

Aggregations

HttpResponse (io.jenkins.blueocean.blueocean_bitbucket_pipeline.HttpResponse)1 BbCloudSaveContentResponse (io.jenkins.blueocean.blueocean_bitbucket_pipeline.cloud.model.BbCloudSaveContentResponse)1 Nonnull (javax.annotation.Nonnull)1 HttpEntity (org.apache.http.HttpEntity)1 MultipartEntityBuilder (org.apache.http.entity.mime.MultipartEntityBuilder)1