*/
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;
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;
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);
}
}
}
*/
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;
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;
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);