Search in sources :

Example 21 with PermanentException

use of com.hp.octane.integrations.exceptions.PermanentException 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 22 with PermanentException

use of com.hp.octane.integrations.exceptions.PermanentException 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 23 with PermanentException

use of com.hp.octane.integrations.exceptions.PermanentException 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)

Example 24 with PermanentException

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

the class FODServiceImpl method scanIsCompleted.

private boolean scanIsCompleted(Long releaseId, Long scanId) {
    try {
        Scan completeScan = FODReleaseService.getCompleteScan(releaseId, scanId);
        if (completeScan == null) {
            return false;
        }
        logger.debug(configurer.octaneConfiguration.getLocationForLog() + "scan:" + scanId + " is:" + completeScan.status);
        if (completeScan.status == null) {
            return false;
        }
        // Scan that has not started, and not in progress is completed.
        return (!Scan.IN_PROGRESS.equals(completeScan.status) && !Scan.NOT_STARTED.equals(completeScan.status) && !Scan.QUEUED.equals(completeScan.status));
    } catch (PermanentException e) {
        throw e;
    } catch (TemporaryException e) {
        throw e;
    } catch (Exception e) {
        return false;
    }
}
Also used : TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) Scan(com.hp.octane.integrations.services.vulnerabilities.fod.dto.pojos.Scan) TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) IOException(java.io.IOException) PermanentException(com.hp.octane.integrations.exceptions.PermanentException)

Example 25 with PermanentException

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

the class FODServiceImpl method getVulnerabilitiesScanResultStream.

@Override
public InputStream getVulnerabilitiesScanResultStream(VulnerabilitiesQueueItem queueItem) throws IOException {
    logger.debug(configurer.octaneConfiguration.getLocationForLog() + "Entered getVulnerabilitiesScanResultStream");
    String targetDir = getTargetDir(configurer.pluginServices.getAllowedOctaneStorage(), queueItem.getJobId(), queueItem.getBuildId());
    logger.debug(configurer.octaneConfiguration.getLocationForLog() + "getVulnerabilitiesScanResultStream target Dir:" + targetDir);
    InputStream cachedScanResult = getCachedScanResult(targetDir);
    if (cachedScanResult != null) {
        logger.warn("results " + queueItem.toString() + "are cached!");
        return cachedScanResult;
    }
    enrichItemWithFODParams(queueItem);
    PplnRunStatus pplnRunStatus = fodScanIsStillInProgress(queueItem);
    if (pplnRunStatus.continuePolling) {
        return null;
    } else if (pplnRunStatus.tryGetIssues) {
        List<OctaneIssue> octaneIssues = fetchIssues(queueItem, getRelease(queueItem).toString());
        cacheIssues(targetDir, octaneIssues);
        return serializeIssues(octaneIssues);
    } else {
        throw new PermanentException(queueItem.getJobId() + "#" + queueItem.getBuildId() + " , Polling is stopped");
    }
}
Also used : InputStream(java.io.InputStream) PermanentException(com.hp.octane.integrations.exceptions.PermanentException)

Aggregations

PermanentException (com.hp.octane.integrations.exceptions.PermanentException)31 TemporaryException (com.hp.octane.integrations.exceptions.TemporaryException)19 IOException (java.io.IOException)15 OctaneResponse (com.hp.octane.integrations.dto.connectivity.OctaneResponse)10 OctaneRequest (com.hp.octane.integrations.dto.connectivity.OctaneRequest)8 InputStream (java.io.InputStream)6 URISyntaxException (java.net.URISyntaxException)6 URIBuilder (org.apache.http.client.utils.URIBuilder)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 RequestTimeoutException (com.hp.octane.integrations.exceptions.RequestTimeoutException)5 HashMap (java.util.HashMap)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)5 InterruptedIOException (java.io.InterruptedIOException)4 HttpGet (org.apache.http.client.methods.HttpGet)4 OctaneRestClient (com.hp.octane.integrations.services.rest.OctaneRestClient)3 LinkedHashMap (java.util.LinkedHashMap)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 OctaneSDKGeneralException (com.hp.octane.integrations.exceptions.OctaneSDKGeneralException)2 List (java.util.List)2 HttpEntity (org.apache.http.HttpEntity)2