Search in sources :

Example 1 with SonarScanner

use of com.sonar.orchestrator.build.SonarScanner in project sonarqube by SonarSource.

the class QualityGateUiTest method scanSample.

private void scanSample(@Nullable String date, @Nullable String profile) {
    SonarScanner scan = SonarScanner.create(projectDir("shared/xoo-sample")).setProperty("sonar.cpd.exclusions", "**/*");
    if (date != null) {
        scan.setProperty("sonar.projectDate", date);
    }
    if (profile != null) {
        scan.setProfile(profile);
    }
    orchestrator.executeBuild(scan);
}
Also used : SonarScanner(com.sonar.orchestrator.build.SonarScanner)

Example 2 with SonarScanner

use of com.sonar.orchestrator.build.SonarScanner in project sonarqube by SonarSource.

the class ScmTest method scm_optimization.

@Test
public void scm_optimization() throws Exception {
    SonarScanner build = SonarScanner.create(projectDir("scm/xoo-sample-with-scm")).setProperty("sonar.scm.provider", "xoo").setProperty("sonar.scm.disabled", "false");
    // First run
    BuildResult buildResult = orchestrator.executeBuild(build);
    assertThat(getScmData("sample-scm:src/main/xoo/sample/Sample.xoo")).containsExactly(MapEntry.entry(1, new LineData("1", "2013-01-04T00:00:00+0000", "jhenry")), MapEntry.entry(3, new LineData("2", "2013-01-04T00:00:00+0000", "jhenry")), MapEntry.entry(4, new LineData("1", "2013-01-04T00:00:00+0000", "jhenry")), MapEntry.entry(8, new LineData("3", "2014-01-04T00:00:00+0000", "toto")));
    assertThat(buildResult.getLogs()).containsSequence("1 files to be analyzed", "1/1 files analyzed");
    // Second run with same file should not trigger blame but SCM data are copied from previous analysis
    buildResult = orchestrator.executeBuild(build);
    assertThat(getScmData("sample-scm:src/main/xoo/sample/Sample.xoo")).containsExactly(MapEntry.entry(1, new LineData("1", "2013-01-04T00:00:00+0000", "jhenry")), MapEntry.entry(3, new LineData("2", "2013-01-04T00:00:00+0000", "jhenry")), MapEntry.entry(4, new LineData("1", "2013-01-04T00:00:00+0000", "jhenry")), MapEntry.entry(8, new LineData("3", "2014-01-04T00:00:00+0000", "toto")));
    assertThat(buildResult.getLogs()).doesNotContain("1 files to be analyzed");
    assertThat(buildResult.getLogs()).doesNotContain("1/1 files analyzed");
    // Now if SCM is explicitely disabled it should clear SCM data on server side
    buildResult = orchestrator.executeBuild(build.setProperty("sonar.scm.disabled", "true"));
    assertThat(getScmData("sample-scm:src/main/xoo/sample/Sample.xoo")).isEmpty();
    assertThat(buildResult.getLogs()).doesNotContain("1 files to be analyzed");
    assertThat(buildResult.getLogs()).doesNotContain("1/1 files analyzed");
}
Also used : BuildResult(com.sonar.orchestrator.build.BuildResult) SonarScanner(com.sonar.orchestrator.build.SonarScanner) Test(org.junit.Test)

Example 3 with SonarScanner

use of com.sonar.orchestrator.build.SonarScanner in project sonarqube by SonarSource.

the class QualityGateNotificationTest method status_on_metric_variation_and_send_notifications.

@Test
public void status_on_metric_variation_and_send_notifications() throws Exception {
    setServerProperty(orchestrator, "email.smtp_host.secured", "localhost");
    setServerProperty(orchestrator, "email.smtp_port.secured", Integer.toString(smtpServer.getServer().getPort()));
    // Create user, who will receive notifications for new violations
    userRule.createUser("tester", "Tester", "tester@example.org", "tester");
    // Send test email to the test user
    newAdminWsClient(orchestrator).wsConnector().call(new PostRequest("api/emails/send").setParam("to", "test@example.org").setParam("message", "This is a test message from SonarQube")).failIfNotSuccessful();
    // Add notifications to the test user
    WsClient wsClient = newUserWsClient(orchestrator, "tester", "tester");
    wsClient.wsConnector().call(new PostRequest("api/notifications/add").setParam("type", "NewAlerts").setParam("channel", "EmailNotificationChannel")).failIfNotSuccessful();
    // Create quality gate with conditions on variations
    QualityGate simple = qgClient().create("SimpleWithDifferential");
    qgClient().setDefault(simple.id());
    qgClient().createCondition(NewCondition.create(simple.id()).metricKey("ncloc").period(1).operator("EQ").warningThreshold("0"));
    SonarScanner analysis = SonarScanner.create(projectDir("qualitygate/xoo-sample"));
    orchestrator.executeBuild(analysis);
    assertThat(getGateStatusMeasure().getValue()).isEqualTo("OK");
    orchestrator.executeBuild(analysis);
    assertThat(getGateStatusMeasure().getValue()).isEqualTo("WARN");
    qgClient().unsetDefault();
    qgClient().destroy(simple.id());
    waitUntilAllNotificationsAreDelivered(smtpServer);
    Iterator<WiserMessage> emails = smtpServer.getMessages().iterator();
    MimeMessage message = emails.next().getMimeMessage();
    assertThat(message.getHeader("To", null)).isEqualTo("<test@example.org>");
    assertThat((String) message.getContent()).contains("This is a test message from SonarQube");
    assertThat(emails.hasNext()).isTrue();
    message = emails.next().getMimeMessage();
    assertThat(message.getHeader("To", null)).isEqualTo("<tester@example.org>");
    assertThat((String) message.getContent()).contains("Quality gate status: Orange (was Green)");
    assertThat((String) message.getContent()).contains("Quality gate threshold: Lines of Code variation = 0 since previous analysis");
    assertThat((String) message.getContent()).contains("/dashboard/index/sample");
    assertThat(emails.hasNext()).isFalse();
}
Also used : PostRequest(org.sonarqube.ws.client.PostRequest) MimeMessage(javax.mail.internet.MimeMessage) WiserMessage(org.subethamail.wiser.WiserMessage) WsClient(org.sonarqube.ws.client.WsClient) ItUtils.newUserWsClient(util.ItUtils.newUserWsClient) ItUtils.newAdminWsClient(util.ItUtils.newAdminWsClient) QualityGate(org.sonar.wsclient.qualitygate.QualityGate) SonarScanner(com.sonar.orchestrator.build.SonarScanner) Test(org.junit.Test)

Example 4 with SonarScanner

use of com.sonar.orchestrator.build.SonarScanner in project sonarqube by SonarSource.

the class QualityGateTest method test_status_error.

@Test
public void test_status_error() throws IOException {
    QualityGate simple = qgClient().create("SimpleWithLowThreshold");
    qgClient().setDefault(simple.id());
    qgClient().createCondition(NewCondition.create(simple.id()).metricKey("ncloc").operator("GT").errorThreshold("10"));
    try {
        SonarScanner build = SonarScanner.create(projectDir("qualitygate/xoo-sample"));
        BuildResult buildResult = orchestrator.executeBuild(build);
        verifyQGStatusInPostTask(buildResult, TASK_STATUS_SUCCESS, QG_STATUS_ERROR);
        assertThat(getGateStatusMeasure().getValue()).isEqualTo("ERROR");
    } finally {
        qgClient().unsetDefault();
        qgClient().destroy(simple.id());
    }
}
Also used : BuildResult(com.sonar.orchestrator.build.BuildResult) QualityGate(org.sonar.wsclient.qualitygate.QualityGate) SonarScanner(com.sonar.orchestrator.build.SonarScanner) Test(org.junit.Test)

Example 5 with SonarScanner

use of com.sonar.orchestrator.build.SonarScanner in project sonarqube by SonarSource.

the class QualityGateTest method ad_hoc_build_break_strategy.

@Test
public void ad_hoc_build_break_strategy() throws IOException {
    QualityGate simple = qgClient().create("SimpleWithLowThresholdForBuildBreakStrategy");
    qgClient().setDefault(simple.id());
    qgClient().createCondition(NewCondition.create(simple.id()).metricKey("ncloc").operator("GT").errorThreshold("7"));
    try {
        File projectDir = projectDir("qualitygate/xoo-sample");
        SonarScanner build = SonarScanner.create(projectDir);
        BuildResult buildResult = orchestrator.executeBuild(build);
        verifyQGStatusInPostTask(buildResult, TASK_STATUS_SUCCESS, QG_STATUS_ERROR);
        String taskId = getTaskIdInLocalReport(projectDir);
        String analysisId = getAnalysisId(taskId);
        ProjectStatusWsResponse projectStatusWsResponse = wsClient.qualityGates().projectStatus(new ProjectStatusWsRequest().setAnalysisId(analysisId));
        ProjectStatusWsResponse.ProjectStatus projectStatus = projectStatusWsResponse.getProjectStatus();
        assertThat(projectStatus.getStatus()).isEqualTo(ProjectStatusWsResponse.Status.ERROR);
        assertThat(projectStatus.getConditionsCount()).isEqualTo(1);
        ProjectStatusWsResponse.Condition condition = projectStatus.getConditionsList().get(0);
        assertThat(condition.getMetricKey()).isEqualTo("ncloc");
        assertThat(condition.getErrorThreshold()).isEqualTo("7");
    } finally {
        qgClient().unsetDefault();
        qgClient().destroy(simple.id());
    }
}
Also used : BuildResult(com.sonar.orchestrator.build.BuildResult) ProjectStatusWsResponse(org.sonarqube.ws.WsQualityGates.ProjectStatusWsResponse) ProjectStatusWsRequest(org.sonarqube.ws.client.qualitygate.ProjectStatusWsRequest) File(java.io.File) QualityGate(org.sonar.wsclient.qualitygate.QualityGate) SonarScanner(com.sonar.orchestrator.build.SonarScanner) Test(org.junit.Test)

Aggregations

SonarScanner (com.sonar.orchestrator.build.SonarScanner)74 Test (org.junit.Test)56 BuildResult (com.sonar.orchestrator.build.BuildResult)38 File (java.io.File)17 QualityGate (org.sonar.wsclient.qualitygate.QualityGate)8 JSONArray (org.json.simple.JSONArray)4 JSONObject (org.json.simple.JSONObject)4 Issue (org.sonar.wsclient.issue.Issue)3 Favorites (org.sonarqube.ws.Favorites)3 SearchRequest (org.sonarqube.ws.client.favorite.SearchRequest)3 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 SonarClient (org.sonar.wsclient.SonarClient)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 BuildFailureException (com.sonar.orchestrator.build.BuildFailureException)1 SonarScannerInstaller (com.sonar.orchestrator.build.SonarScannerInstaller)1 FileSystem (com.sonar.orchestrator.config.FileSystem)1 AbapCheck (it.plugins.checks.AbapCheck)1 CCheck (it.plugins.checks.CCheck)1 Check (it.plugins.checks.Check)1