Search in sources :

Example 6 with OctaneRequest

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

the class VulnerabilitiesServiceImpl method pushVulnerabilities.

private void pushVulnerabilities(InputStream vulnerabilities, String jobId, String buildId) 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.urlEncodePathParam(jobId);
    String encodedBuildId = CIPluginSDKUtils.urlEncodePathParam(buildId);
    String url = getVulnerabilitiesContextPath(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.POST).setUrl(url).setHeaders(headers).setBody(vulnerabilities);
    OctaneResponse response = octaneRestClient.execute(request);
    logger.info(configurer.octaneConfiguration.getLocationForLog() + "vulnerabilities pushed; status: " + response.getStatus() + ", response: " + response.getBody());
    if (response.getStatus() == HttpStatus.SC_ACCEPTED) {
        logger.info(configurer.octaneConfiguration.getLocationForLog() + "vulnerabilities push SUCCEED for " + jobId + " #" + buildId);
    } else if (response.getStatus() == HttpStatus.SC_SERVICE_UNAVAILABLE) {
        throw new TemporaryException("vulnerabilities push FAILED, service unavailable");
    } else {
        throw new PermanentException("vulnerabilities push FAILED, status " + response.getStatus() + "; dropping this item from the queue \n" + response.getBody());
    }
}
Also used : TemporaryException(com.hp.octane.integrations.exceptions.TemporaryException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) 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 7 with OctaneRequest

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

the class VulnerabilitiesServiceImpl method getBaselineDateFromOctane.

private OctaneResponse getBaselineDateFromOctane(String jobId, String buildId) throws IOException {
    boolean base64 = isEncodeBase64();
    String encodedJobId = base64 ? CIPluginSDKUtils.urlEncodeBase64(jobId) : CIPluginSDKUtils.urlEncodeQueryParam(jobId);
    String encodedBuildId = CIPluginSDKUtils.urlEncodeQueryParam(buildId);
    String url = getVulnerabilitiesPreFlightContextPath(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 preflightRequest = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.GET).setUrl(url);
    return restService.obtainOctaneRestClient().execute(preflightRequest);
}
Also used : OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest)

Example 8 with OctaneRequest

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

the class PipelineContextServiceImpl method updatePipeline.

@Override
public PipelineContext updatePipeline(String serverIdentity, String jobName, PipelineContext pipelineContext) throws IOException {
    String url = getConfigurationUrl(serverIdentity, jobName);
    validateReleaseAndMilestone(pipelineContext);
    OctaneRestClient octaneRestClient = restService.obtainOctaneRestClient();
    Map<String, String> headers = new HashMap<>();
    headers.put(ACCEPT_HEADER, ContentType.APPLICATION_JSON.getMimeType());
    headers.put(CONTENT_TYPE_HEADER, ContentType.APPLICATION_JSON.getMimeType());
    PipelineContextList list = dtoFactory.newDTO(PipelineContextList.class).setData(Arrays.asList(pipelineContext));
    String jsonData = dtoFactory.dtoToJson(list);
    OctaneRequest request = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.PUT).setUrl(url).setBody(jsonData).setHeaders(headers);
    OctaneResponse response = octaneRestClient.execute(request);
    validateResponse(HttpStatus.SC_OK, response);
    PipelineContextList resultList = dtoFactory.dtoFromJson(response.getBody(), PipelineContextList.class);
    // we might receive several pipeline context from other workspaces.
    // find updated context by id
    PipelineContext result = resultList.getData().stream().filter(p -> p.getContextEntityId() == pipelineContext.getContextEntityId()).findFirst().get();
    return result;
}
Also used : HashMap(java.util.HashMap) PipelineContext(com.hp.octane.integrations.dto.pipelines.PipelineContext) PipelineContextList(com.hp.octane.integrations.dto.pipelines.PipelineContextList) 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 9 with OctaneRequest

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

the class PipelineContextServiceImpl method deleteTestsFromPipelineNodes.

@Override
public void deleteTestsFromPipelineNodes(String jobName, long pipelineId, long workspaceId) throws IOException {
    String url = getWorkspaceAnalyticsContextPath(configurer.octaneConfiguration.getUrl(), configurer.octaneConfiguration.getSharedSpace(), workspaceId) + String.format("pipelines/%s/jobs/%s/tests", pipelineId, CIPluginSDKUtils.urlEncodePathParam(jobName));
    OctaneRestClient octaneRestClient = restService.obtainOctaneRestClient();
    Map<String, String> headers = new HashMap<>();
    headers.put(ACCEPT_HEADER, ContentType.APPLICATION_JSON.getMimeType());
    OctaneRequest request = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.DELETE).setUrl(url).setHeaders(headers);
    OctaneResponse response = octaneRestClient.execute(request);
    validateResponse(HttpStatus.SC_OK, response);
// Object result = response.getBody();
// return;
}
Also used : HashMap(java.util.HashMap) 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 10 with OctaneRequest

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

the class BitbucketServerFetchHandler method getPagedEntities.

private <T extends Entity & SupportUpdatedTime> List<T> getPagedEntities(String url, Class<T> entityType, int pageSize, int maxTotal, Long minUpdateTime) {
    try {
        // https://developer.atlassian.com/server/confluence/pagination-in-the-rest-api/
        List<T> result = new ArrayList<>();
        boolean finished;
        int limit = pageSize;
        int start = 0;
        do {
            String myUrl = url + (url.contains("?") ? "" : "?") + String.format("&limit=%d&start=%d", limit, start);
            OctaneRequest request = dtoFactory.newDTO(OctaneRequest.class).setUrl(myUrl).setMethod(HttpMethod.GET);
            OctaneResponse response = restClient.executeRequest(request);
            if (response.getStatus() != HttpStatus.SC_OK) {
                throw new RuntimeException(String.format("Request to '%s' is ended with result %d : %s", myUrl, response.getStatus(), JsonConverter.getErrorMessage(response.getBody())));
            }
            EntityCollection<T> collection = JsonConverter.convertCollection(response.getBody(), entityType);
            result.addAll(collection.getValues());
            finished = collection.isLastPage() || result.size() > maxTotal;
            limit = collection.getLimit();
            start = collection.getStart() + collection.getLimit();
            // remove outdated items
            if (minUpdateTime != null) {
                for (int i = result.size() - 1; i >= 0; i--) {
                    if (result.get(i).getUpdatedTime() <= minUpdateTime) {
                        result.remove(i);
                        finished = true;
                    } else {
                        break;
                    }
                }
            }
        } while (!finished);
        // remove exceeded items
        while (result.size() > maxTotal) {
            result.remove(result.size() - 1);
        }
        return result;
    } catch (RuntimeException e) {
        throw e;
    } 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)

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