Search in sources :

Example 46 with WsResponse

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

the class WsTestUtil method mockReader.

public static void mockReader(ScannerWsClient mock, String path, Reader reader) {
    WsResponse response = mock(WsResponse.class);
    when(response.contentReader()).thenReturn(reader);
    when(mock.call(argThat(new RequestMatcher(path)))).thenReturn(response);
}
Also used : WsResponse(org.sonarqube.ws.client.WsResponse)

Example 47 with WsResponse

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

the class UpgradeTest method checkUrlIsReturningNotFound.

private void checkUrlIsReturningNotFound(String url) {
    WsResponse response = newWsClient(orchestrator).wsConnector().call(new GetRequest(url));
    assertThat(response.code()).isEqualTo(HttpURLConnection.HTTP_NOT_FOUND);
}
Also used : GetRequest(org.sonarqube.ws.client.GetRequest) WsResponse(org.sonarqube.ws.client.WsResponse)

Example 48 with WsResponse

use of org.sonarqube.ws.client.WsResponse 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 49 with WsResponse

use of org.sonarqube.ws.client.WsResponse 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 50 with WsResponse

use of org.sonarqube.ws.client.WsResponse 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)

Aggregations

WsResponse (org.sonarqube.ws.client.WsResponse)58 Test (org.junit.Test)34 GetRequest (org.sonarqube.ws.client.GetRequest)27 WsRequest (org.sonarqube.ws.client.WsRequest)20 MockWsResponse (org.sonarqube.ws.client.MockWsResponse)15 MessageException (org.sonar.api.utils.MessageException)6 HttpException (org.sonarqube.ws.client.HttpException)6 PipedInputStream (java.io.PipedInputStream)5 PipedOutputStream (java.io.PipedOutputStream)5 PostRequest (org.sonarqube.ws.client.PostRequest)3 WsClient (org.sonarqube.ws.client.WsClient)3 File (java.io.File)2 FileUtils.readFileToString (org.apache.commons.io.FileUtils.readFileToString)2 Profiler (org.sonar.api.utils.log.Profiler)2 ItUtils.newAdminWsClient (util.ItUtils.newAdminWsClient)2 SonarScanner (com.sonar.orchestrator.build.SonarScanner)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 CheckForNull (javax.annotation.CheckForNull)1