Search in sources :

Example 6 with HttpException

use of org.sonarqube.ws.client.HttpException in project sonarqube by SonarSource.

the class OrganizationTest method verifyUserNotAuthorized.

private void verifyUserNotAuthorized(String login, String password, Consumer<OrganizationService> consumer) {
    try {
        OrganizationService organizationService = ItUtils.newUserWsClient(orchestrator, login, password).organizations();
        consumer.accept(organizationService);
        fail("An HttpException should have been raised");
    } catch (HttpException e) {
        assertThat(e.code()).isEqualTo(403);
    }
}
Also used : OrganizationService(org.sonarqube.ws.client.organization.OrganizationService) HttpException(org.sonarqube.ws.client.HttpException)

Example 7 with HttpException

use of org.sonarqube.ws.client.HttpException in project sonarqube by SonarSource.

the class QualityGateCheck method getQualityGateStatus.

private Status getQualityGateStatus(String analysisId) {
    GetRequest getQualityGateReq = new GetRequest("api/qualitygates/project_status").setMediaType(MediaTypes.PROTOBUF).setParam("analysisId", analysisId);
    try {
        WsResponse getTaskResultResponse = wsClient.call(getQualityGateReq).failIfNotSuccessful();
        Qualitygates.ProjectStatusResponse.ProjectStatus status = parseQualityGateResponse(getTaskResultResponse);
        return status.getStatus();
    } catch (HttpException e) {
        throw MessageException.of(String.format("Failed to get Quality Gate status - %s", DefaultScannerWsClient.createErrorMessage(e)));
    }
}
Also used : GetRequest(org.sonarqube.ws.client.GetRequest) WsResponse(org.sonarqube.ws.client.WsResponse) HttpException(org.sonarqube.ws.client.HttpException)

Example 8 with HttpException

use of org.sonarqube.ws.client.HttpException in project sonarqube by SonarSource.

the class QualityGateCheck method waitForCeTaskToFinish.

private Ce.Task waitForCeTaskToFinish(String taskId) {
    GetRequest getTaskResultReq = new GetRequest("api/ce/task").setMediaType(MediaTypes.PROTOBUF).setParam("id", taskId);
    long currentTime = 0;
    while (qualityGateTimeoutInMs > currentTime) {
        try {
            WsResponse getTaskResultResponse = wsClient.call(getTaskResultReq).failIfNotSuccessful();
            Ce.Task task = parseCeTaskResponse(getTaskResultResponse);
            if (TASK_TERMINAL_STATUSES.contains(task.getStatus())) {
                return task;
            }
            Thread.sleep(POLLING_INTERVAL_IN_MS);
            currentTime += POLLING_INTERVAL_IN_MS;
        } catch (HttpException e) {
            throw MessageException.of(String.format("Failed to get CE Task status - %s", DefaultScannerWsClient.createErrorMessage(e)));
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new IllegalStateException("Quality Gate check has been interrupted", e);
        }
    }
    throw MessageException.of("Quality Gate check timeout exceeded - View details on " + ceTaskReportDataHolder.getDashboardUrl());
}
Also used : Ce(org.sonarqube.ws.Ce) GetRequest(org.sonarqube.ws.client.GetRequest) WsResponse(org.sonarqube.ws.client.WsResponse) HttpException(org.sonarqube.ws.client.HttpException)

Example 9 with HttpException

use of org.sonarqube.ws.client.HttpException in project sonarqube by SonarSource.

the class ReportPublisher method upload.

/**
 * Uploads the report file to server and returns the generated task id
 */
String upload(File report) {
    LOG.debug("Upload report");
    long startTime = System.currentTimeMillis();
    PostRequest.Part filePart = new PostRequest.Part(MediaTypes.ZIP, report);
    PostRequest post = new PostRequest("api/ce/submit").setMediaType(MediaTypes.PROTOBUF).setParam("projectKey", moduleHierarchy.root().key()).setParam("projectName", moduleHierarchy.root().getOriginalName()).setPart("report", filePart);
    String branchName = branchConfiguration.branchName();
    if (branchName != null) {
        if (branchConfiguration.branchType() != PULL_REQUEST) {
            post.setParam(CHARACTERISTIC, "branch=" + branchName);
            post.setParam(CHARACTERISTIC, "branchType=" + branchConfiguration.branchType().name());
        } else {
            post.setParam(CHARACTERISTIC, "pullRequest=" + branchConfiguration.pullRequestKey());
        }
    }
    WsResponse response;
    try {
        post.setWriteTimeOutInMs(DEFAULT_WRITE_TIMEOUT);
        response = wsClient.call(post);
    } catch (Exception e) {
        throw new IllegalStateException("Failed to upload report: " + e.getMessage(), e);
    }
    try {
        response.failIfNotSuccessful();
    } catch (HttpException e) {
        throw MessageException.of(String.format("Server failed to process report. Please check server logs: %s", DefaultScannerWsClient.createErrorMessage(e)));
    }
    try (InputStream protobuf = response.contentStream()) {
        return Ce.SubmitResponse.parser().parseFrom(protobuf).getTaskId();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        long stopTime = System.currentTimeMillis();
        LOG.info("Analysis report uploaded in " + (stopTime - startTime) + "ms");
    }
}
Also used : PostRequest(org.sonarqube.ws.client.PostRequest) InputStream(java.io.InputStream) WsResponse(org.sonarqube.ws.client.WsResponse) HttpException(org.sonarqube.ws.client.HttpException) MessageException(org.sonar.api.utils.MessageException) HttpException(org.sonarqube.ws.client.HttpException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 10 with HttpException

use of org.sonarqube.ws.client.HttpException in project sonarqube by SonarSource.

the class DefaultProjectRepositoriesLoaderTest method failFastHttpErrorMessageException.

@Test
public void failFastHttpErrorMessageException() {
    HttpException http = new HttpException("uri", 403, null);
    MessageException e = MessageException.of("http error", http);
    WsTestUtil.mockException(wsClient, e);
    assertThatThrownBy(() -> loader.load(PROJECT_KEY, null)).isInstanceOf(MessageException.class).hasMessage("http error");
}
Also used : MessageException(org.sonar.api.utils.MessageException) HttpException(org.sonarqube.ws.client.HttpException) Test(org.junit.Test)

Aggregations

HttpException (org.sonarqube.ws.client.HttpException)17 Test (org.junit.Test)11 WsResponse (org.sonarqube.ws.client.WsResponse)6 MessageException (org.sonar.api.utils.MessageException)5 GetRequest (org.sonarqube.ws.client.GetRequest)4 MockWsResponse (org.sonarqube.ws.client.MockWsResponse)3 WsRequest (org.sonarqube.ws.client.WsRequest)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Profiler (org.sonar.api.utils.log.Profiler)1 Ce (org.sonarqube.ws.Ce)1 Settings (org.sonarqube.ws.Settings)1 PostRequest (org.sonarqube.ws.client.PostRequest)1 OrganizationService (org.sonarqube.ws.client.organization.OrganizationService)1