Search in sources :

Example 1 with BbServerSaveContentResponse

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

the class BitbucketServerApi method saveContent.

@Override
@Nonnull
public BbSaveContentResponse saveContent(@Nonnull String projectKey, @Nonnull String repoSlug, @Nonnull String path, @Nonnull String content, @Nonnull String commitMessage, @Nullable String branch, @Nullable String sourceBranch, @Nullable String commitId) {
    try {
        String version = getVersion(apiUrl);
        if (!isSupportedVersion(version)) {
            throw new ServiceException.PreconditionRequired(Messages.bbserver_version_validation_error(version, BitbucketServerApi.MINIMUM_SUPPORTED_VERSION));
        }
        MultipartEntityBuilder builder = MultipartEntityBuilder.create().addTextBody("content", content).addTextBody("message", commitMessage);
        if (StringUtils.isNotBlank(branch)) {
            builder.addTextBody("branch", branch);
        }
        if (StringUtils.isNotBlank(sourceBranch)) {
            builder.addTextBody("sourceBranch", sourceBranch);
        }
        if (org.apache.commons.lang.StringUtils.isNotBlank(commitId)) {
            builder.addTextBody("sourceCommitId", commitId);
        }
        HttpEntity entity = builder.build();
        HttpResponse response = request.put(String.format("%s/%s/repos/%s/browse/%s", baseUrl + "projects", projectKey, repoSlug, path), entity);
        // we ignore error and return the response as if it was saved successfully
        if (commitId != null && response.getStatus() == 409) {
            return new BbServerSaveContentResponse(commitId);
        }
        InputStream inputStream = response.getContent();
        return om.readValue(inputStream, BbServerSaveContentResponse.class);
    } catch (IOException e) {
        throw new ServiceException.UnexpectedErrorException(e.getMessage(), e);
    }
}
Also used : MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) HttpEntity(org.apache.http.HttpEntity) ServiceException(io.jenkins.blueocean.commons.ServiceException) InputStream(java.io.InputStream) HttpResponse(io.jenkins.blueocean.blueocean_bitbucket_pipeline.HttpResponse) BbServerSaveContentResponse(io.jenkins.blueocean.blueocean_bitbucket_pipeline.server.model.BbServerSaveContentResponse) IOException(java.io.IOException) Nonnull(javax.annotation.Nonnull)

Aggregations

HttpResponse (io.jenkins.blueocean.blueocean_bitbucket_pipeline.HttpResponse)1 BbServerSaveContentResponse (io.jenkins.blueocean.blueocean_bitbucket_pipeline.server.model.BbServerSaveContentResponse)1 ServiceException (io.jenkins.blueocean.commons.ServiceException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Nonnull (javax.annotation.Nonnull)1 HttpEntity (org.apache.http.HttpEntity)1 MultipartEntityBuilder (org.apache.http.entity.mime.MultipartEntityBuilder)1