Search in sources :

Example 16 with OctaneRequest

use of com.hp.octane.integrations.dto.connectivity.OctaneRequest 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 17 with OctaneRequest

use of com.hp.octane.integrations.dto.connectivity.OctaneRequest 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 18 with OctaneRequest

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

the class CoverageServiceImpl method pushCoverage.

@Override
public OctaneResponse pushCoverage(String jobId, String buildId, CoverageReportType reportType, InputStream coverageReport) {
    if (jobId == null || jobId.isEmpty()) {
        throw new IllegalArgumentException("job ID MUST NOT be null nor empty");
    }
    if (buildId == null || buildId.isEmpty()) {
        throw new IllegalArgumentException("build ID MUST NOT be null nor empty");
    }
    if (reportType == null) {
        throw new IllegalArgumentException("report type MUST NOT be null");
    }
    if (coverageReport == null) {
        throw new IllegalArgumentException("coverage report data MUST NOT be null");
    }
    boolean base64 = isEncodeBase64();
    String tempJobId = jobId;
    if (base64) {
        tempJobId = CIPluginSDKUtils.urlEncodeBase64(jobId);
    }
    String url;
    try {
        url = new URIBuilder(getAnalyticsContextPath(configurer.octaneConfiguration.getUrl(), configurer.octaneConfiguration.getSharedSpace()) + "coverage").setParameter("ci-server-identity", configurer.octaneConfiguration.getInstanceId()).setParameter("ci-job-id", tempJobId).setParameter("ci-build-id", buildId).setParameter("file-type", reportType.name()).toString();
        if (base64) {
            url = CIPluginSDKUtils.addParameterEncode64ToUrl(url);
        }
    } catch (URISyntaxException urise) {
        throw new PermanentException("failed to build URL to push coverage report", urise);
    }
    String correlationId = CIPluginSDKUtils.getNextCorrelationId();
    Map<String, String> headers = new HashMap<>();
    headers.put(CORRELATION_ID_HEADER, correlationId);
    OctaneRequest pushCoverageRequest = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.PUT).setUrl(url).setHeaders(headers).setBody(coverageReport);
    try {
        return restService.obtainOctaneRestClient().execute(pushCoverageRequest);
    } catch (IOException ioe) {
        throw new TemporaryException("failed to push coverage report", ioe);
    }
}
Also used : TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PermanentException(com.hp.octane.integrations.exceptions.PermanentException) URISyntaxException(java.net.URISyntaxException) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) IOException(java.io.IOException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 19 with OctaneRequest

use of com.hp.octane.integrations.dto.connectivity.OctaneRequest 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)

Example 20 with OctaneRequest

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

the class EntitiesServiceImpl method getPagedEntities.

@Override
public ResponseEntityList getPagedEntities(String url) {
    OctaneRestClient octaneRestClient = restService.obtainOctaneRestClient();
    Map<String, String> headers = new HashMap<>();
    headers.put(ACCEPT_HEADER, ContentType.APPLICATION_JSON.getMimeType());
    headers.put(OctaneRestClient.CLIENT_TYPE_HEADER, OctaneRestClient.CLIENT_TYPE_VALUE);
    OctaneRequest request = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.GET).setUrl(url).setHeaders(headers);
    OctaneResponse response = executeRequest(octaneRestClient, request);
    ResponseEntityList pagedEntityList = parseBody(HttpStatus.SC_OK, response);
    return pagedEntityList;
}
Also used : OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) OctaneRestClient(com.hp.octane.integrations.services.rest.OctaneRestClient)

Aggregations

OctaneRequest (com.hp.octane.integrations.dto.connectivity.OctaneRequest)30 OctaneResponse (com.hp.octane.integrations.dto.connectivity.OctaneResponse)27 IOException (java.io.IOException)12 OctaneRestClient (com.hp.octane.integrations.services.rest.OctaneRestClient)11 PermanentException (com.hp.octane.integrations.exceptions.PermanentException)8 HashMap (java.util.HashMap)8 TemporaryException (com.hp.octane.integrations.exceptions.TemporaryException)7 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 LinkedHashMap (java.util.LinkedHashMap)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 URISyntaxException (java.net.URISyntaxException)2 URIBuilder (org.apache.http.client.utils.URIBuilder)2 OctaneClient (com.hp.octane.integrations.OctaneClient)1 OctaneConnectivityStatus (com.hp.octane.integrations.dto.general.OctaneConnectivityStatus)1