]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6905 Batch should dump analysis-related URL into a JSON file
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Mon, 23 Nov 2015 09:04:01 +0000 (10:04 +0100)
committerDuarte Meneses <duarte.meneses@sonarsource.com>
Thu, 26 Nov 2015 07:40:41 +0000 (08:40 +0100)
sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java
sonar-batch/src/test/java/org/sonar/batch/report/ReportPublisherTest.java

index 42b549de019bb4c4977f36d3057b0edb8b73adcf..17331448b1050bbd4d728f3c3a4ee603b2d73058 100644 (file)
  */
 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);
     }
   }
 }
index 93582aa6d7d056df2a1400445a3b7e333822e1aa..75b8af6dc5147b860e48ab31def0ac2c44d5eafe 100644 (file)
  */
 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);