Search in sources :

Example 11 with TemporaryException

use of com.hp.octane.integrations.exceptions.TemporaryException in project octane-ci-java-sdk by MicroFocus.

the class EventsServiceImpl method worker.

// infallible everlasting worker function
private void worker() {
    while (!eventsPushExecutor.isShutdown()) {
        if (!workerPreflight.preflight()) {
            continue;
        }
        // build events list to be sent
        List<CIEvent> eventsChunk = null;
        CIEventsList eventsSnapshot;
        try {
            eventsChunk = getEventsChunk();
            CIServerInfo serverInfo = configurer.pluginServices.getServerInfo();
            serverInfo.setInstanceId(configurer.octaneConfiguration.getInstanceId());
            eventsSnapshot = dtoFactory.newDTO(CIEventsList.class).setServer(serverInfo).setEvents(eventsChunk);
        } catch (Throwable t) {
            logger.error(configurer.octaneConfiguration.getLocationForLog() + "failed to serialize chunk of " + (eventsChunk != null ? eventsChunk.size() : "[NULL]") + " events, dropping them off (if any) and continue");
            removeEvents(eventsChunk);
            continue;
        }
        // send the data to Octane
        try {
            String correlationId = CIPluginSDKUtils.getNextCorrelationId();
            logEventsToBeSent(eventsSnapshot, correlationId);
            sendEventsData(eventsSnapshot, correlationId);
            removeEvents(eventsChunk);
            if (events.size() > 0) {
                logger.info(configurer.octaneConfiguration.getLocationForLog() + "left to send " + events.size() + " events");
            }
        } catch (RequestTimeoutException rte) {
            requestTimeoutCount++;
            lastRequestTimeoutTime = System.currentTimeMillis();
            logger.info(configurer.octaneConfiguration.getLocationForLog() + rte.getMessage());
            CIPluginSDKUtils.doWait(TEMPORARY_FAILURE_PAUSE);
        } catch (TemporaryException tqie) {
            logger.error(configurer.octaneConfiguration.getLocationForLog() + "failed to send events with temporary error, breathing " + TEMPORARY_FAILURE_PAUSE + "ms and continue", tqie);
            CIPluginSDKUtils.doWait(TEMPORARY_FAILURE_PAUSE);
        } catch (PermanentException pqie) {
            logger.error(configurer.octaneConfiguration.getLocationForLog() + "failed to send events with permanent error, dropping this chunk and continue", pqie);
            removeEvents(eventsChunk);
        } catch (Throwable t) {
            logger.error(configurer.octaneConfiguration.getLocationForLog() + "failed to send events with unexpected error, dropping this chunk and continue", t);
            removeEvents(eventsChunk);
        }
    }
}
Also used : RequestTimeoutException(com.hp.octane.integrations.exceptions.RequestTimeoutException) TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) CIEvent(com.hp.octane.integrations.dto.events.CIEvent) CIEventsList(com.hp.octane.integrations.dto.events.CIEventsList) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) CIServerInfo(com.hp.octane.integrations.dto.general.CIServerInfo)

Example 12 with TemporaryException

use of com.hp.octane.integrations.exceptions.TemporaryException in project octane-ci-java-sdk by MicroFocus.

the class CoverageServiceImpl method pushCoverageWithPreflight.

private void pushCoverageWithPreflight(CoverageQueueItem queueItem) {
    // preflight
    if (!isSonarReportRelevant(queueItem.jobId)) {
        return;
    }
    // get coverage report content
    InputStream coverageReport = configurer.pluginServices.getCoverageReport(queueItem.jobId, queueItem.buildId, queueItem.reportFileName);
    if (coverageReport == null) {
        logger.info(configurer.octaneConfiguration.getLocationForLog() + "no log for " + queueItem + " found, abandoning");
        return;
    }
    // push coverage
    OctaneResponse response = pushCoverage(queueItem.jobId, queueItem.buildId, queueItem.reportType, coverageReport);
    if (response.getStatus() == HttpStatus.SC_OK) {
        logger.info(configurer.octaneConfiguration.getLocationForLog() + "successfully pushed coverage of " + queueItem + ", CorrelationId - " + response.getCorrelationId());
    } else if (response.getStatus() == HttpStatus.SC_SERVICE_UNAVAILABLE || response.getStatus() == HttpStatus.SC_BAD_GATEWAY) {
        throw new TemporaryException("temporary failed to push coverage of " + queueItem + ", status: " + HttpStatus.SC_SERVICE_UNAVAILABLE);
    } else {
        throw new PermanentException("permanently failed to push coverage of " + queueItem + ", status: " + response.getStatus());
    }
}
Also used : TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) InputStream(java.io.InputStream) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse)

Example 13 with TemporaryException

use of com.hp.octane.integrations.exceptions.TemporaryException in project octane-ci-java-sdk by MicroFocus.

the class SCMDataServiceImpl method pushSCMDataByRestAPI.

private void pushSCMDataByRestAPI(String jobId, String buildId, InputStream scmData) throws IOException {
    OctaneRestClient octaneRestClient = restService.obtainOctaneRestClient();
    Map<String, String> headers = new HashMap<>();
    headers.put(RestService.CONTENT_TYPE_HEADER, ContentType.APPLICATION_JSON.getMimeType());
    boolean base64 = isEncodeBase64();
    String encodedJobId = base64 ? CIPluginSDKUtils.urlEncodeBase64(jobId) : CIPluginSDKUtils.urlEncodeQueryParam(jobId);
    String encodedBuildId = CIPluginSDKUtils.urlEncodeQueryParam(buildId);
    String url = getSCMDataContextPath(configurer.octaneConfiguration.getUrl(), configurer.octaneConfiguration.getSharedSpace()) + "?instance-id=" + configurer.octaneConfiguration.getInstanceId() + "&job-ci-id=" + encodedJobId + "&build-ci-id=" + encodedBuildId;
    if (base64) {
        url = CIPluginSDKUtils.addParameterEncode64ToUrl(url);
    }
    OctaneRequest request = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.PUT).setUrl(url).setHeaders(headers).setBody(scmData);
    OctaneResponse response = octaneRestClient.execute(request);
    if (response.getStatus() == HttpStatus.SC_OK) {
        logger.info(configurer.octaneConfiguration.getLocationForLog() + "scmData for " + jobId + " #" + buildId + ", push SUCCEED : " + response.getBody());
    } else if (response.getStatus() == HttpStatus.SC_SERVICE_UNAVAILABLE) {
        throw new TemporaryException("scmData push FAILED, service unavailable");
    } else {
        throw new PermanentException("scmData push FAILED, status " + response.getStatus() + "; dropping this item from the queue \n" + response.getBody());
    }
}
Also used : TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) OctaneRestClient(com.hp.octane.integrations.services.rest.OctaneRestClient)

Example 14 with TemporaryException

use of com.hp.octane.integrations.exceptions.TemporaryException in project octane-ci-java-sdk by MicroFocus.

the class SonarServiceImpl method retrieveAndPushSonarDataToOctane.

private void retrieveAndPushSonarDataToOctane(SonarBuildCoverageQueueItem queueItem) {
    // preflight
    if (!coverageService.isSonarReportRelevant(queueItem.jobId)) {
        return;
    }
    StringBuilder errorMessage = new StringBuilder().append("failed to inject sonarqube coverage data to octane for project key: ").append(queueItem.projectKey).append(" with ciIdentity: ").append(configurer.octaneConfiguration.getInstanceId()).append(" with jobId: ").append(queueItem.jobId).append(" with buildId: ").append(queueItem.buildId);
    try {
        // retrieve coverage report from Sonar
        Integer pageIndex = 0;
        BuildCoverage buildCoverageReport = dtoFactory.newDTO(BuildCoverage.class);
        JsonNode jsonReport;
        do {
            pageIndex++;
            InputStream reportStream = getPageFromSonar(queueItem, pageIndex);
            jsonReport = CIPluginSDKUtils.getObjectMapper().readTree(reportStream);
            buildCoverageReport.mergeSonarCoverageReport(jsonReport);
        } while (SonarUtils.sonarReportHasAnotherPage(pageIndex, jsonReport));
        // push coverage to Octane
        OctaneResponse response = coverageService.pushCoverage(queueItem.jobId, queueItem.buildId, CoverageReportType.SONAR_REPORT, dtoFactory.dtoToJsonStream(buildCoverageReport));
        if (response.getStatus() == HttpStatus.SC_SERVICE_UNAVAILABLE) {
            errorMessage.append(" with status code: ").append(response.getStatus());
            throw new TemporaryException(errorMessage.toString());
        } else if (response.getStatus() != HttpStatus.SC_OK) {
            errorMessage.append(" with status code: ").append(response.getStatus()).append(" and response body: ").append(response.getBody());
            throw new PermanentException(errorMessage.toString());
        }
    } catch (Throwable throwable) {
        logger.error(configurer.octaneConfiguration.getLocationForLog() + errorMessage.toString(), throwable);
        throw new PermanentException(throwable);
    }
}
Also used : TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) InputStream(java.io.InputStream) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) BuildCoverage(com.hp.octane.integrations.dto.coverage.BuildCoverage) JsonNode(com.fasterxml.jackson.databind.JsonNode) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse)

Example 15 with TemporaryException

use of com.hp.octane.integrations.exceptions.TemporaryException in project octane-ci-java-sdk by MicroFocus.

the class TestsServiceImpl method doPreflightAndPushTestResult.

private void doPreflightAndPushTestResult(TestsResultQueueItem queueItem) {
    // validate test result - first to be done as it is the cheapest to 'fail fast'
    InputStream testsResultA = configurer.pluginServices.getTestsResult(queueItem.jobId, queueItem.buildId);
    if (testsResultA == null) {
        logger.warn(configurer.octaneConfiguration.getLocationForLog() + "test result of " + queueItem + " resolved to be NULL, skipping");
        return;
    }
    try {
        // preflight
        InputStream testsResultB;
        boolean isRelevant = isTestsResultRelevant(queueItem.jobId, queueItem.rootJobId);
        logger.info(configurer.octaneConfiguration.getLocationForLog() + "test results preflight " + queueItem + " = " + isRelevant);
        if (!isRelevant) {
            return;
        }
        if (!this.configurationService.isOctaneVersionGreaterOrEqual("15.1.60")) {
            try {
                // for 15.1.60 - instance id is passed by query param
                // for earlier version - instance id is part of test result body
                String testResultXML = CIPluginSDKUtils.inputStreamToUTF8String(testsResultA);
                testResultXML = testResultXML.replaceAll("<build.*?>", "<build server_id=\"" + configurer.octaneConfiguration.getInstanceId() + "\" job_id=\"" + queueItem.jobId + "\" build_id=\"" + queueItem.buildId + "\"/>").replace("</build>", // remove closing build element if exist
                "");
                testsResultB = new ByteArrayInputStream(testResultXML.getBytes(Charsets.UTF_8));
            } catch (Exception e) {
                throw new PermanentException("failed to update ci server instance ID in the test results XML");
            }
        } else {
            testsResultB = testsResultA;
        }
        // push
        try {
            OctaneResponse response = pushTestsResult(testsResultB, queueItem.jobId, queueItem.buildId);
            if (response.getStatus() == HttpStatus.SC_ACCEPTED) {
                logger.info(configurer.octaneConfiguration.getLocationForLog() + "successfully pushed test results for " + queueItem + "; status: " + response.getStatus() + ", response: " + response.getBody() + ", CorrelationId - " + response.getCorrelationId());
            } else if (response.getStatus() == HttpStatus.SC_SERVICE_UNAVAILABLE || response.getStatus() == HttpStatus.SC_BAD_GATEWAY) {
                throw new TemporaryException("push request TEMPORARILY failed with status " + response.getStatus());
            } else {
                throw new PermanentException("push request PERMANENTLY failed with status " + response.getStatus());
            }
        } catch (IOException ioe) {
            throw new TemporaryException("failed to perform push test results request for " + queueItem, ioe);
        } finally {
            try {
                testsResultB.close();
            } catch (IOException e) {
                logger.warn(configurer.octaneConfiguration.getLocationForLog() + "failed to close test result file after push test for " + queueItem);
            }
        }
    } finally {
        try {
            testsResultA.close();
        } catch (IOException e) {
            logger.warn(configurer.octaneConfiguration.getLocationForLog() + "failed to close test result file after push test for " + queueItem);
        }
    }
}
Also used : TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) RequestTimeoutException(com.hp.octane.integrations.exceptions.RequestTimeoutException) URISyntaxException(java.net.URISyntaxException) InterruptedIOException(java.io.InterruptedIOException) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) IOException(java.io.IOException)

Aggregations

PermanentException (com.hp.octane.integrations.exceptions.PermanentException)17 TemporaryException (com.hp.octane.integrations.exceptions.TemporaryException)17 OctaneResponse (com.hp.octane.integrations.dto.connectivity.OctaneResponse)10 IOException (java.io.IOException)10 OctaneRequest (com.hp.octane.integrations.dto.connectivity.OctaneRequest)7 RequestTimeoutException (com.hp.octane.integrations.exceptions.RequestTimeoutException)4 InputStream (java.io.InputStream)3 InterruptedIOException (java.io.InterruptedIOException)3 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)3 HttpGet (org.apache.http.client.methods.HttpGet)3 OctaneRestClient (com.hp.octane.integrations.services.rest.OctaneRestClient)2 URISyntaxException (java.net.URISyntaxException)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 BuildCoverage (com.hp.octane.integrations.dto.coverage.BuildCoverage)1 CIEvent (com.hp.octane.integrations.dto.events.CIEvent)1 CIEventsList (com.hp.octane.integrations.dto.events.CIEventsList)1 CIServerInfo (com.hp.octane.integrations.dto.general.CIServerInfo)1 Scan (com.hp.octane.integrations.services.vulnerabilities.fod.dto.pojos.Scan)1