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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations