From: Duarte Meneses Date: Mon, 23 Nov 2015 09:04:01 +0000 (+0100) Subject: SONAR-6905 Batch should dump analysis-related URL into a JSON file X-Git-Tag: 5.3-RC1~165 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=aced281145eae09c60d25656b4fef53adb80175e;p=sonarqube.git SONAR-6905 Batch should dump analysis-related URL into a JSON file --- diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java index 42b549de019..17331448b10 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java +++ b/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java @@ -19,16 +19,22 @@ */ package org.sonar.batch.report; +import org.sonar.api.utils.text.JsonWriter; + import com.github.kevinsawicki.http.HttpRequest; import com.google.common.annotations.VisibleForTesting; import com.google.gson.Gson; +import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.Date; import javax.annotation.CheckForNull; @@ -59,6 +65,7 @@ public class ReportPublisher implements Startable { public static final String KEEP_REPORT_PROP_KEY = "sonar.batch.keepReport"; public static final String VERBOSE_KEY = "sonar.verbose"; public static final String DUMP_REPORT_PROP_KEY = "sonar.batch.dumpReportDir"; + public static final String JSON_DETAILS_FILE = "analysis-details.json"; private final ServerClient serverClient; private final Server server; @@ -244,10 +251,30 @@ public class ReportPublisher implements Startable { String url = baseUrl + "dashboard/index/" + effectiveKey; logger.info("ANALYSIS SUCCESSFUL, you can browse {}", url); logger.info("Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report."); + String taskUrl = null; if (taskId != null) { - String taskUrl = baseUrl + "api/ce/task?id=" + taskId; + taskUrl = baseUrl + "api/ce/task?id=" + taskId; logger.info("More about the report processing at {}", taskUrl); } + + writeJson(url, taskUrl); + } + } + + private void writeJson(String dashboardUrl, @Nullable String taskUrl) { + File exportFile = new File(projectReactor.getRoot().getWorkDir(), JSON_DETAILS_FILE); + try (Writer output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile), StandardCharsets.UTF_8))) { + JsonWriter json = JsonWriter.of(output); + json.beginObject(); + json.prop("dashboardUrl", dashboardUrl); + if (taskUrl != null) { + json.prop("ceTaskUrl", taskUrl); + } + json.endObject(); + + LOG.debug("Analysis URLs written to {}", exportFile); + } catch (IOException e) { + throw new IllegalStateException("Unable to write analysis URLs in file " + exportFile.getAbsolutePath(), e); } } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java b/sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java index 93582aa6d7d..75b8af6dc51 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java @@ -19,17 +19,10 @@ */ package org.sonar.batch.report; -import org.junit.Rule; -import org.junit.rules.TemporaryFolder; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; - -import static org.assertj.core.api.Assertions.assertThat; - import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.slf4j.Logger; import org.sonar.api.CoreProperties; import org.sonar.api.batch.bootstrap.ProjectDefinition; @@ -39,6 +32,14 @@ import org.sonar.api.utils.TempFolder; import org.sonar.batch.analysis.DefaultAnalysisMode; import org.sonar.batch.bootstrap.ServerClient; import org.sonar.batch.scan.ImmutableProjectReactor; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; @@ -95,6 +96,23 @@ public class ReportPublisherTest { verifyNoMoreInteractions(logger); } + @Test + public void should_write_json_file() throws IOException { + settings.setProperty(CoreProperties.SERVER_BASE_URL, "http://myserver/"); + + ReportPublisher job = new ReportPublisher(settings, mock(ServerClient.class), mock(Server.class), mock(AnalysisContextReportPublisher.class), reactor, mode, + mock(TempFolder.class), new ReportPublisherStep[0]); + job.logSuccess(mock(Logger.class), "abc123"); + + File jsonFile = new File(temp.getRoot(), "analysis-details.json"); + assertThat(jsonFile).exists(); + + String jsonFileContent = new String(Files.readAllBytes(jsonFile.toPath()), StandardCharsets.UTF_8); + String expectedContent = "\"dashboardUrl\":\"http://myserver/dashboard/index/struts\",\"ceTaskUrl\":\"http://myserver/api/ce/task?id=abc123\""; + assertThat(jsonFileContent).contains(expectedContent); + + } + @Test public void should_log_successful_issues_analysis() { when(mode.isIssues()).thenReturn(true);