use of com.hp.octane.integrations.exceptions.SonarIntegrationException in project octane-ci-java-sdk by MicroFocus.
the class SonarServiceImpl method getWebhookKey.
private String getWebhookKey(String ciNotificationUrl, String sonarURL, String token) throws SonarIntegrationException {
try {
URIBuilder uriBuilder = new URIBuilder(sonarURL + WEBHOOK_LIST_URI);
HttpClient httpClient = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(uriBuilder.build());
setTokenInHttpRequest(request, token);
HttpResponse response = httpClient.execute(request);
InputStream content = response.getEntity().getContent();
// if webhooks exist
if (content.available() != 0) {
JsonNode jsonResponse = CIPluginSDKUtils.getObjectMapper().readTree(content);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
ArrayNode webhooksListJson = (ArrayNode) jsonResponse.get("webhooks");
if (webhooksListJson.size() > 0) {
for (JsonNode webhookNode : webhooksListJson) {
String entryURL = webhookNode.get("url").textValue();
if (entryURL.equals(ciNotificationUrl)) {
return webhookNode.get("key").textValue();
}
}
}
return null;
} else {
String errorMessage = "".concat("failed to get webhook key from sonarqube with notification URL: ").concat(ciNotificationUrl).concat(" with status code: ").concat(String.valueOf(response.getStatusLine().getStatusCode())).concat(" with errors: ").concat(jsonResponse.get("errors").toString());
throw new SonarIntegrationException(errorMessage);
}
}
return null;
} catch (SonarIntegrationException e) {
logger.error(configurer.octaneConfiguration.getLocationForLog() + e.getMessage(), e);
throw e;
} catch (Exception e) {
String errorMessage = "".concat("failed to get webhook key from sonarqube with notification URL: ").concat(ciNotificationUrl);
logger.error(configurer.octaneConfiguration.getLocationForLog() + errorMessage, e);
throw new SonarIntegrationException(errorMessage, e);
}
}
use of com.hp.octane.integrations.exceptions.SonarIntegrationException in project octane-ci-java-sdk by MicroFocus.
the class SonarServiceImpl method ensureSonarWebhookExist.
@Override
public synchronized void ensureSonarWebhookExist(String ciCallbackUrl, String sonarURL, String sonarToken) throws SonarIntegrationException {
// problem in sonar project key in new project
try {
String webhookKey = getWebhookKey(ciCallbackUrl, sonarURL, sonarToken);
if (webhookKey == null) {
HttpClient httpClient = HttpClientBuilder.create().build();
String name = configurer.pluginServices.getServerInfo().getType() + "-" + configurer.pluginServices.getServerInfo().getUrl().replaceAll("[<>:\"/\\|?*]", "_").trim();
URIBuilder uriBuilder = new URIBuilder(sonarURL + WEBHOOK_CREATE_URI).setParameter("name", name).setParameter("url", ciCallbackUrl);
HttpPost request = new HttpPost(uriBuilder.toString());
setTokenInHttpRequest(request, sonarToken);
HttpResponse response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
// error can sometimes return empty results
String errorMessage = "exception during webhook registration for ciNotificationUrl: ".concat(ciCallbackUrl).concat(" with status code: ").concat(String.valueOf(response.getStatusLine().getStatusCode()));
throw new SonarIntegrationException(errorMessage);
}
}
} catch (SonarIntegrationException e) {
logger.error(configurer.octaneConfiguration.getLocationForLog() + e.getMessage(), e);
throw e;
} catch (Exception e) {
String errorMessage = "exception during webhook registration for ciNotificationUrl: " + ciCallbackUrl;
logger.error(configurer.octaneConfiguration.getLocationForLog() + errorMessage, e);
throw new SonarIntegrationException(errorMessage, e);
}
}
Aggregations