Search in sources :

Example 21 with OctaneRequest

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

the class EntitiesServiceImpl method postEntities.

@Override
public List<Entity> postEntities(Long workspaceId, String entityCollectionName, String jsonData, Collection<String> fields, Map<String, String> serviceArgument) {
    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());
    headers.put(OctaneRestClient.CLIENT_TYPE_HEADER, OctaneRestClient.CLIENT_TYPE_VALUE);
    String url = buildEntityUrl(workspaceId, entityCollectionName, null, fields, null, null, null, serviceArgument);
    OctaneRequest request = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.POST).setUrl(url).setBody(jsonData).setHeaders(headers);
    OctaneResponse response = executeRequest(octaneRestClient, request);
    ResponseEntityList responseEntityList = parseBody(HttpStatus.SC_CREATED, response);
    return responseEntityList.getData();
}
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)

Example 22 with OctaneRequest

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

the class PipelineContextServiceImpl method createPipeline.

@Override
public PipelineContext createPipeline(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());
    String jsonData = dtoFactory.dtoToJson(pipelineContext);
    OctaneRequest request = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.POST).setUrl(url).setBody(jsonData).setHeaders(headers);
    OctaneResponse response = octaneRestClient.execute(request);
    validateResponse(HttpStatus.SC_CREATED, response);
    PipelineContextList list = dtoFactory.dtoFromJson(response.getBody(), PipelineContextList.class);
    // we might receive several pipeline context from other workspaces.
    // find updated context by workspace id
    PipelineContext result = list.getData().stream().filter(p -> p.getWorkspaceId() == pipelineContext.getWorkspaceId()).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 23 with OctaneRequest

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

the class PipelineContextServiceImpl method getJobConfiguration.

@Override
public PipelineContextList getJobConfiguration(String serverIdentity, String jobName) throws IOException {
    String url = getConfigurationUrl(serverIdentity, 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.GET).setUrl(url).setHeaders(headers);
    OctaneResponse response = octaneRestClient.execute(request);
    validateResponse(HttpStatus.SC_OK, response);
    PipelineContextList result = dtoFactory.dtoFromJson(response.getBody(), PipelineContextList.class);
    return result;
}
Also used : HashMap(java.util.HashMap) 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 24 with OctaneRequest

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

the class PullRequestAndBranchServiceImpl method sendPullRequests.

@Override
public void sendPullRequests(List<PullRequest> pullRequests, String workspaceId, PullRequestFetchParameters pullRequestFetchParameters, Consumer<String> logConsumer) throws IOException {
    Map<String, String> headers = new LinkedHashMap<>();
    headers.put(RestService.CONTENT_TYPE_HEADER, ContentType.APPLICATION_JSON.getMimeType());
    String url = configurer.octaneConfiguration.getUrl() + RestService.SHARED_SPACE_API_PATH_PART + configurer.octaneConfiguration.getSharedSpace() + "/workspaces/" + workspaceId + RestService.ANALYTICS_CI_PATH_PART + "pull-requests/";
    int sentCounter = 0;
    List<List<PullRequest>> subSets = ListUtils.partition(pullRequests, 200);
    for (List<PullRequest> list : subSets) {
        String json = dtoFactory.dtoCollectionToJson(list);
        OctaneRequest octaneRequest = dtoFactory.newDTO(OctaneRequest.class).setMethod(HttpMethod.PUT).setUrl(url).setHeaders(headers).setBody(json);
        OctaneResponse octaneResponse = restService.obtainOctaneRestClient().execute(octaneRequest);
        if (octaneResponse.getStatus() != HttpStatus.SC_OK) {
            if (octaneResponse.getStatus() == HttpStatus.SC_NOT_FOUND) {
                throw new ResourceNotFoundException("Failed to sendPullRequests : received 404 status. Validate that you use correct workspace id and ALM Octane version is greater than " + PullRequestAndBranchService.BRANCH_COLLECTION_SUPPORTED_VERSION);
            } else {
                throw new RuntimeException("Failed to sendPullRequests : (" + octaneResponse.getStatus() + ")" + octaneResponse.getBody());
            }
        } else {
            sentCounter += list.size();
            logConsumer.accept(String.format("Sent %s/%s pull requests.", sentCounter, pullRequests.size()));
        }
    }
    long lastUpdateTime = pullRequests.stream().map(PullRequest::getUpdatedTime).max(Comparator.naturalOrder()).orElse(0L);
    savePullRequestLastUpdateTime(workspaceId, pullRequestFetchParameters.getRepoUrl(), lastUpdateTime);
    logConsumer.accept("Last update time set to " + lastUpdateTime);
}
Also used : PullRequest(com.hp.octane.integrations.dto.scm.PullRequest) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest) OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) ResourceNotFoundException(com.hp.octane.integrations.exceptions.ResourceNotFoundException)

Example 25 with OctaneRequest

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

the class GithubV3FetchHandler method getRateLimitationInfo.

/**
 * RE
 *
 * @param baseUrl
 * @param logConsumer
 * @return
 * @throws IOException
 */
private RateLimitationInfo getRateLimitationInfo(String baseUrl, Consumer<String> logConsumer) throws IOException {
    String rateUrl = baseUrl + "/rate_limit";
    OctaneRequest request = dtoFactory.newDTO(OctaneRequest.class).setUrl(rateUrl).setMethod(HttpMethod.GET);
    OctaneResponse response = restClient.executeRequest(request);
    if (response.getStatus() == HttpStatus.SC_OK && response.getHeaders().containsKey("X-RateLimit-Limit")) {
        RateLimitationInfo info = new RateLimitationInfo();
        fillRateLimitationInfo(response, info);
        long minToNextReset = (info.getReset() - System.currentTimeMillis() / 1000) / 60;
        logConsumer.accept(String.format("RateLimit Info: Limit-%s; Remaining-%s; Reset in %s min. ", info.getLimit(), info.getRemaining(), minToNextReset));
        return info;
    } else {
        return null;
    }
}
Also used : OctaneResponse(com.hp.octane.integrations.dto.connectivity.OctaneResponse) OctaneRequest(com.hp.octane.integrations.dto.connectivity.OctaneRequest)

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