use of io.jenkins.blueocean.blueocean_bitbucket_pipeline.HttpRequest 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);
}
}
Aggregations