Search in sources :

Example 16 with OctaneResponse

use of com.hp.octane.integrations.dto.connectivity.OctaneResponse in project octane-ci-java-sdk by MicroFocus.

the class LogsServiceImpl method pushBuildLog.

private void pushBuildLog(String serverId, BuildLogQueueItem queueItem) {
    OctaneConfiguration octaneConfiguration = configurer.octaneConfiguration;
    String encodedServerId = CIPluginSDKUtils.urlEncodePathParam(serverId);
    String encodedBuildId = CIPluginSDKUtils.urlEncodePathParam(queueItem.buildId);
    boolean base64 = isEncodeBase64();
    String encodedJobId = base64 ? CIPluginSDKUtils.urlEncodeBase64(queueItem.jobId) : CIPluginSDKUtils.urlEncodePathParam(queueItem.jobId);
    String encodedRootJobId = base64 ? CIPluginSDKUtils.urlEncodeBase64(queueItem.rootJobId) : CIPluginSDKUtils.urlEncodeQueryParam(queueItem.rootJobId);
    // preflight
    String[] workspaceIDs = preflightRequest(octaneConfiguration, encodedServerId, encodedJobId, encodedRootJobId, base64);
    if (workspaceIDs.length == 0) {
        logger.info(configurer.octaneConfiguration.getLocationForLog() + "log of " + queueItem + ", no interested workspace is found");
        return;
    } else {
        logger.info(configurer.octaneConfiguration.getLocationForLog() + "log of " + queueItem + ", found " + workspaceIDs.length + " interested workspace/s");
    }
    // submit log for each workspace returned by the 'preflight' API
    // [YG] TODO: the code below should and will be refactored to a simpler state once Octane's APIs will be adjusted
    InputStream log;
    OctaneRequest request;
    OctaneResponse response;
    for (String workspaceId : workspaceIDs) {
        String url = octaneConfiguration.getUrl() + RestService.SHARED_SPACE_INTERNAL_API_PATH_PART + octaneConfiguration.getSharedSpace() + "/workspaces/" + workspaceId + RestService.ANALYTICS_CI_PATH_PART + encodedServerId + "/" + encodedJobId + "/" + encodedBuildId + "/logs";
        if (base64) {
            url = CIPluginSDKUtils.addParameterEncode64ToUrl(url);
        }
        String correlationId = CIPluginSDKUtils.getNextCorrelationId();
        Map<String, String> headers = new HashMap<>();
        headers.put(CORRELATION_ID_HEADER, correlationId);
        request = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.POST).setHeaders(headers).setUrl(url);
        try {
            log = configurer.pluginServices.getBuildLog(queueItem.jobId, queueItem.buildId);
            if (log == null) {
                logger.info(configurer.octaneConfiguration.getLocationForLog() + "no log for " + queueItem + " found, abandoning");
                break;
            }
            request.setBody(log);
            response = restService.obtainOctaneRestClient().execute(request);
            if (response.getStatus() == HttpStatus.SC_OK) {
                logger.info(configurer.octaneConfiguration.getLocationForLog() + "successfully pushed log of " + queueItem + " to WS " + workspaceId + ", correlation Id = " + correlationId);
            } else {
                logger.error(configurer.octaneConfiguration.getLocationForLog() + "failed to push log of " + queueItem + " to WS " + workspaceId + ", status: " + response.getStatus() + ", correlation Id = " + correlationId);
            }
        } catch (IOException ioe) {
            logger.error(configurer.octaneConfiguration.getLocationForLog() + "failed to push log of " + queueItem + " to WS " + workspaceId + ", breathing " + TEMPORARY_ERROR_BREATHE_INTERVAL + "ms and retrying one more time due to IOException", ioe);
            CIPluginSDKUtils.doWait(TEMPORARY_ERROR_BREATHE_INTERVAL);
            log = configurer.pluginServices.getBuildLog(queueItem.jobId, queueItem.buildId);
            if (log == null) {
                logger.info(configurer.octaneConfiguration.getLocationForLog() + "no log for " + queueItem + " found, abandoning");
                break;
            }
            request.setBody(log);
            try {
                response = restService.obtainOctaneRestClient().execute(request);
                if (response.getStatus() == HttpStatus.SC_OK) {
                    logger.info(configurer.octaneConfiguration.getLocationForLog() + "successfully pushed log of " + queueItem + " to WS " + workspaceId);
                } else {
                    logger.error(configurer.octaneConfiguration.getLocationForLog() + "failed to push log of " + queueItem + " to WS " + workspaceId + ", status: " + response.getStatus());
                }
            } catch (IOException ioem) {
                logger.error(configurer.octaneConfiguration.getLocationForLog() + "failed to push log of " + queueItem + " to WS " + workspaceId + " for the second time, abandoning", ioem);
            }
        }
    }
}
Also used : InputStream(java.io.InputStream) OctaneConfiguration(com.hp.octane.integrations.OctaneConfiguration) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) IOException(java.io.IOException)

Example 17 with OctaneResponse

use of com.hp.octane.integrations.dto.connectivity.OctaneResponse in project octane-ci-java-sdk by MicroFocus.

the class LogsServiceImpl method preflightRequest.

private String[] preflightRequest(OctaneConfiguration octaneConfiguration, String encodedServerId, String encodedJobId, String encodedRootJobId, boolean base64) {
    String[] result = new String[0];
    OctaneResponse response;
    // get result
    String url = getAnalyticsContextPath(configurer.octaneConfiguration.getUrl(), octaneConfiguration.getSharedSpace()) + "servers/" + encodedServerId + "/jobs/" + encodedJobId + "/workspaceId";
    if (encodedRootJobId != null && !encodedRootJobId.isEmpty()) {
        url += "?rootJobId=" + encodedRootJobId;
    }
    if (base64) {
        url = CIPluginSDKUtils.addParameterEncode64ToUrl(url);
    }
    try {
        OctaneRequest preflightRequest = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.GET).setUrl(url);
        response = restService.obtainOctaneRestClient().execute(preflightRequest);
        if (response.getStatus() == HttpStatus.SC_SERVICE_UNAVAILABLE || response.getStatus() == HttpStatus.SC_BAD_GATEWAY) {
            throw new TemporaryException("preflight request failed with status " + response.getStatus());
        } else if (response.getStatus() == HttpStatus.SC_UNAUTHORIZED || response.getStatus() == HttpStatus.SC_FORBIDDEN) {
            CIPluginSDKUtils.doWait(30000);
            throw new PermanentException("preflight request failed with status " + response.getStatus());
        } else if (response.getStatus() != HttpStatus.SC_OK && response.getStatus() != HttpStatus.SC_NO_CONTENT) {
            throw new PermanentException("preflight request failed with status " + response.getStatus() + ". JobId: '" + encodedJobId + "'. Request URL : " + url);
        }
    } catch (IOException ioe) {
        throw new TemporaryException(ioe);
    }
    // parse result
    if (response.getBody() != null && !response.getBody().isEmpty()) {
        try {
            result = CIPluginSDKUtils.getObjectMapper().readValue(response.getBody(), String[].class);
        } catch (IOException ioe) {
            if (CIPluginSDKUtils.isServiceTemporaryUnavailable(response.getBody())) {
                throw new TemporaryException("Saas service is temporary unavailable.");
            } else {
                throw new PermanentException("failed to parse preflight response '" + response.getBody() + "' for '" + encodedJobId + "'");
            }
        }
    }
    return result;
}
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) IOException(java.io.IOException)

Example 18 with OctaneResponse

use of com.hp.octane.integrations.dto.connectivity.OctaneResponse in project octane-ci-java-sdk by MicroFocus.

the class ConfigurationServiceImpl method resetOctaneRootsCache.

@Override
public Future<Boolean> resetOctaneRootsCache() {
    if (isOctaneRootsCacheActivated() && isOctaneVersionGreaterOrEqual(OCTANE_ROOTS_VERSION) && !configurer.octaneConfiguration.isDisabled()) {
        return octaneRootsCacheExecutor.submit(() -> {
            logger.info(configurer.octaneConfiguration.getLocationForLog() + "resetOctaneRootCache started");
            try {
                long startTime = System.currentTimeMillis();
                String url = configurer.octaneConfiguration.getUrl() + RestService.SHARED_SPACE_INTERNAL_API_PATH_PART + configurer.octaneConfiguration.getSharedSpace() + String.format(PIPELINE_ROOTS_URL, configurer.octaneConfiguration.getInstanceId());
                OctaneRequest request = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.GET).setUrl(url);
                OctaneResponse response = restService.obtainOctaneRestClient().execute(request, configurer.octaneConfiguration);
                octaneRoots = mapper.readValue(response.getBody(), mapper.getTypeFactory().constructCollectionType(Set.class, String.class));
                logger.info(configurer.octaneConfiguration.getLocationForLog() + "resetOctaneRootCache: successfully update octane roots, found " + octaneRoots.size() + " roots, processing time is " + ((System.currentTimeMillis() - startTime) / 1000) + " seconds");
                return true;
            } catch (Exception e) {
                logger.info(configurer.octaneConfiguration.getLocationForLog() + "Failed to resetOctaneRootCache : " + e.getMessage());
                return false;
            }
        });
    } else {
        if (octaneRoots != null) {
            logger.info(configurer.octaneConfiguration.getLocationForLog() + "resetOctaneRootsCache : cache is cleared");
        }
        octaneRoots = null;
        return CompletableFuture.completedFuture(false);
    }
}
Also used : OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) IOException(java.io.IOException) OctaneConnectivityException(com.hp.octane.integrations.exceptions.OctaneConnectivityException)

Example 19 with OctaneResponse

use of com.hp.octane.integrations.dto.connectivity.OctaneResponse in project octane-ci-java-sdk by MicroFocus.

the class CoverageServiceImpl method isSonarReportRelevant.

@Override
public boolean isSonarReportRelevant(String jobId) {
    if (jobId == null || jobId.isEmpty()) {
        throw new IllegalArgumentException("job ID MUST NOT be null nor empty");
    }
    boolean result = false;
    OctaneResponse response;
    boolean base64 = isEncodeBase64();
    String encodedJobId = base64 ? CIPluginSDKUtils.urlEncodeBase64(jobId) : CIPluginSDKUtils.urlEncodePathParam(jobId);
    // get result
    try {
        String url = getAnalyticsContextPath(configurer.octaneConfiguration.getUrl(), configurer.octaneConfiguration.getSharedSpace()) + "servers/" + CIPluginSDKUtils.urlEncodePathParam(configurer.octaneConfiguration.getInstanceId()) + "/jobs/" + encodedJobId + "/workspaceId";
        if (base64) {
            url = CIPluginSDKUtils.addParameterEncode64ToUrl(url);
        }
        OctaneRequest preflightRequest = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.GET).setUrl(url);
        response = restService.obtainOctaneRestClient().execute(preflightRequest);
        if (response.getStatus() == HttpStatus.SC_SERVICE_UNAVAILABLE || response.getStatus() == HttpStatus.SC_BAD_GATEWAY) {
            throw new TemporaryException("preflight request failed with status " + response.getStatus());
        } else if (response.getStatus() != HttpStatus.SC_OK && response.getStatus() != HttpStatus.SC_NO_CONTENT) {
            throw new PermanentException("preflight request failed with status " + response.getStatus());
        }
    } catch (IOException ioe) {
        throw new TemporaryException("failed to perform preflight request", ioe);
    }
    // parse result
    if (response.getStatus() == HttpStatus.SC_OK && response.getBody() != null && !response.getBody().isEmpty()) {
        try {
            String[] wss = CIPluginSDKUtils.getObjectMapper().readValue(response.getBody(), String[].class);
            if (wss.length > 0) {
                logger.info(configurer.octaneConfiguration.getLocationForLog() + "coverage of " + jobId + " found " + wss.length + " interested workspace/s in Octane, dispatching the coverage");
                result = true;
            } else {
                logger.info(configurer.octaneConfiguration.getLocationForLog() + "coverage of " + jobId + ", found no interested workspace in Octane");
            }
        } catch (IOException ioe) {
            throw new PermanentException("failed to parse preflight response '" + response.getBody() + "' for '" + jobId + "'");
        }
    }
    return result;
}
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) IOException(java.io.IOException)

Example 20 with OctaneResponse

use of com.hp.octane.integrations.dto.connectivity.OctaneResponse in project octane-ci-java-sdk by MicroFocus.

the class GithubV3FetchHandler method getPagedEntities.

/**
 * @param url
 * @param entityType
 * @param pageSize
 * @param maxTotal
 * @param minUpdateTime
 * @param <T>
 * @return
 */
private <T extends Entity & SupportUpdatedTime> List<T> getPagedEntities(String url, Class<T> entityType, int pageSize, int maxTotal, long minUpdateTime) {
    try {
        List<T> result = new ArrayList<>();
        boolean finished;
        String myUrl = url + (url.contains("?") ? "" : "?") + "&per_page=" + pageSize;
        do {
            finished = true;
            OctaneRequest request = dtoFactory.newDTO(OctaneRequest.class).setUrl(myUrl).setMethod(HttpMethod.GET);
            OctaneResponse response = restClient.executeRequest(request);
            List<T> collection = JsonConverter.convertCollection(response.getBody(), entityType);
            result.addAll(collection);
            myUrl = getNextPageLink(response);
            if (myUrl != null) {
                finished = false;
            }
            // remove outdated items
            for (int i = result.size() - 1; i >= 0 && minUpdateTime > 0; i--) {
                if (result.get(i).getUpdatedTime() <= minUpdateTime) {
                    result.remove(i);
                }
            }
            // remove exceeding items
            while (result.size() > maxTotal) {
                result.remove(result.size() - 1);
                finished = true;
            }
        } while (!finished);
        return result;
    } catch (Exception e) {
        throw new RuntimeException("Failed to getPagedEntities : " + e.getMessage(), e);
    }
}
Also used : OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ResourceNotFoundException(com.hp.octane.integrations.exceptions.ResourceNotFoundException)

Aggregations

OctaneResponse (com.hp.octane.integrations.dto.connectivity.OctaneResponse)36 OctaneRequest (com.hp.octane.integrations.dto.connectivity.OctaneRequest)27 IOException (java.io.IOException)14 PermanentException (com.hp.octane.integrations.exceptions.PermanentException)10 TemporaryException (com.hp.octane.integrations.exceptions.TemporaryException)10 OctaneRestClient (com.hp.octane.integrations.services.rest.OctaneRestClient)10 HashMap (java.util.HashMap)7 InputStream (java.io.InputStream)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 PipelineContextList (com.hp.octane.integrations.dto.pipelines.PipelineContextList)3 RequestTimeoutException (com.hp.octane.integrations.exceptions.RequestTimeoutException)3 ResourceNotFoundException (com.hp.octane.integrations.exceptions.ResourceNotFoundException)3 InterruptedIOException (java.io.InterruptedIOException)3 HttpResponse (org.apache.http.HttpResponse)3 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)3 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)3 OctaneConfiguration (com.hp.octane.integrations.OctaneConfiguration)2 PipelineContext (com.hp.octane.integrations.dto.pipelines.PipelineContext)2 OctaneConnectivityException (com.hp.octane.integrations.exceptions.OctaneConnectivityException)2 Header (org.apache.http.Header)2