Search in sources :

Example 1 with SonarIntegrationException

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);
    }
}
Also used : SonarIntegrationException(com.hp.octane.integrations.exceptions.SonarIntegrationException) InputStream(java.io.InputStream) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) SonarIntegrationException(com.hp.octane.integrations.exceptions.SonarIntegrationException) URISyntaxException(java.net.URISyntaxException) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) IOException(java.io.IOException) AuthenticationException(org.apache.http.auth.AuthenticationException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 2 with SonarIntegrationException

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);
    }
}
Also used : SonarIntegrationException(com.hp.octane.integrations.exceptions.SonarIntegrationException) HttpPost(org.apache.http.client.methods.HttpPost) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) SonarIntegrationException(com.hp.octane.integrations.exceptions.SonarIntegrationException) URISyntaxException(java.net.URISyntaxException) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) IOException(java.io.IOException) AuthenticationException(org.apache.http.auth.AuthenticationException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Aggregations

PermanentException (com.hp.octane.integrations.exceptions.PermanentException)2 SonarIntegrationException (com.hp.octane.integrations.exceptions.SonarIntegrationException)2 TemporaryException (com.hp.octane.integrations.exceptions.TemporaryException)2 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 HttpResponse (org.apache.http.HttpResponse)2 AuthenticationException (org.apache.http.auth.AuthenticationException)2 HttpClient (org.apache.http.client.HttpClient)2 URIBuilder (org.apache.http.client.utils.URIBuilder)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 InputStream (java.io.InputStream)1 HttpGet (org.apache.http.client.methods.HttpGet)1 HttpPost (org.apache.http.client.methods.HttpPost)1