Search in sources :

Example 1 with HttpResponse

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

the class BitbucketServerApi method getVersion.

/**
 * Gives Bitbucket server version
 * @param apiUrl API url of Bitbucket server
 * @return version of Bitbucket server
 */
@Nonnull
public static String getVersion(@Nonnull String apiUrl) {
    try {
        apiUrl = ensureTrailingSlash(apiUrl);
        HttpRequest request = new HttpRequest.HttpRequestBuilder(apiUrl).build();
        HttpResponse response = request.get(apiUrl + "rest/api/1.0/application-properties");
        int status = response.getStatus();
        if ((status >= 301 && status <= 303) || status == 307 || status == 308) {
            String location = response.getHeader("Location");
            String error = String.format("%s is invalid. Bitbucket server sent redirect response", apiUrl);
            if (StringUtils.isNotBlank(location)) {
                URL url = new URL(location);
                String host = url.getHost();
                int port = url.getPort();
                String protocol = url.getProtocol();
                String baseUrl = protocol + "://" + host + ((port == -1) ? "/" : port + "/");
                error += " with location at: " + baseUrl;
            }
            error += ". \nPlease use correct Bitbucket Server endpoint.";
            throw new ServiceException(status, error);
        }
        InputStream inputStream = response.getContent();
        Map<String, String> resp = om.readValue(inputStream, new TypeReference<Map<String, String>>() {
        });
        String version = resp.get("version");
        if (StringUtils.isBlank(version)) {
            throw new ServiceException.PreconditionRequired("Unsupported Bitbucket server, no version information could be determined");
        }
        return version;
    } catch (IOException e) {
        throw new ServiceException.UnexpectedErrorException(e.getMessage(), e);
    }
}
Also used : HttpRequest(io.jenkins.blueocean.blueocean_bitbucket_pipeline.HttpRequest) InputStream(java.io.InputStream) HttpResponse(io.jenkins.blueocean.blueocean_bitbucket_pipeline.HttpResponse) IOException(java.io.IOException) URL(java.net.URL) ServiceException(io.jenkins.blueocean.commons.ServiceException) Map(java.util.Map) Nonnull(javax.annotation.Nonnull)

Example 2 with HttpResponse

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

Example 3 with HttpResponse

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

Example 4 with HttpResponse

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

the class BitbucketServerApi method fileExists.

@Override
public boolean fileExists(@Nonnull String projectKey, @Nonnull String repoSlug, @Nonnull String path, @Nonnull String branch) {
    try {
        URIBuilder uriBuilder = new URIBuilder(String.format("%s/%s/repos/%s/browse/%s", baseUrl + "projects", projectKey, repoSlug, path));
        if (branch != null) {
            uriBuilder.addParameter("at", "refs/heads/" + branch);
        }
        HttpResponse response = request.head(uriBuilder.build().toString());
        return response.getStatus() == 200;
    } catch (URISyntaxException e) {
        throw handleException(e);
    }
}
Also used : HttpResponse(io.jenkins.blueocean.blueocean_bitbucket_pipeline.HttpResponse) URISyntaxException(java.net.URISyntaxException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 5 with HttpResponse

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

the class BitbucketServerApi method isEmptyRepo.

@Override
public boolean isEmptyRepo(@Nonnull String orgId, @Nonnull String repoSlug) {
    try {
        URIBuilder uriBuilder = new URIBuilder(String.format("%s/%s/repos/%s/branches/default", baseUrl + "projects", orgId, repoSlug));
        HttpResponse response = request.head(uriBuilder.build().toString());
        int status = response.getStatus();
        // with BB server 5.6.0 204 is returned for empty repo
        return status == 404 || status == 204;
    } catch (URISyntaxException e) {
        throw handleException(e);
    }
}
Also used : HttpResponse(io.jenkins.blueocean.blueocean_bitbucket_pipeline.HttpResponse) URISyntaxException(java.net.URISyntaxException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Aggregations

HttpResponse (io.jenkins.blueocean.blueocean_bitbucket_pipeline.HttpResponse)7 ServiceException (io.jenkins.blueocean.commons.ServiceException)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Nonnull (javax.annotation.Nonnull)3 URISyntaxException (java.net.URISyntaxException)2 HttpEntity (org.apache.http.HttpEntity)2 URIBuilder (org.apache.http.client.utils.URIBuilder)2 MultipartEntityBuilder (org.apache.http.entity.mime.MultipartEntityBuilder)2 HttpRequest (io.jenkins.blueocean.blueocean_bitbucket_pipeline.HttpRequest)1 BbCloudSaveContentResponse (io.jenkins.blueocean.blueocean_bitbucket_pipeline.cloud.model.BbCloudSaveContentResponse)1 BbServerBranch (io.jenkins.blueocean.blueocean_bitbucket_pipeline.server.model.BbServerBranch)1 BbServerSaveContentResponse (io.jenkins.blueocean.blueocean_bitbucket_pipeline.server.model.BbServerSaveContentResponse)1 URL (java.net.URL)1 Map (java.util.Map)1 CheckForNull (javax.annotation.CheckForNull)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1