From 348cf1ccf9d993949608736dd193c19c30c241cc Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 5 Oct 2015 19:27:14 +0200 Subject: [PATCH] SONAR-6893 Log URL of CE task details --- .../sonar/batch/report/ReportPublisher.java | 37 ++++++++++++++++--- .../batch/report/ReportPublisherTest.java | 23 +++++++++++- 2 files changed, 52 insertions(+), 8 deletions(-) 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 7601804b56c..cf5030fac07 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 @@ -21,6 +21,7 @@ package org.sonar.batch.report; import com.github.kevinsawicki.http.HttpRequest; import com.google.common.annotations.VisibleForTesting; +import com.google.gson.Gson; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -28,6 +29,8 @@ import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; import java.util.Date; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import org.apache.commons.io.FileUtils; import org.picocontainer.Startable; import org.slf4j.Logger; @@ -105,13 +108,14 @@ public class ReportPublisher implements Startable { public void execute() { // If this is a issues mode analysis then we should not upload reports + String taskId = null; if (!analysisMode.isIssues()) { File report = prepareReport(); if (!analysisMode.isMediumTest()) { - sendOrDumpReport(report); + taskId = sendOrDumpReport(report); } } - logSuccess(LoggerFactory.getLogger(getClass())); + logSuccess(LoggerFactory.getLogger(getClass()), taskId); } private File prepareReport() { @@ -134,8 +138,9 @@ public class ReportPublisher implements Startable { } } + @CheckForNull @VisibleForTesting - void sendOrDumpReport(File report) { + String sendOrDumpReport(File report) { ProjectDefinition projectDefinition = projectReactor.getRoot(); String effectiveKey = projectDefinition.getKeyWithBranch(); String relativeUrl = String.format("/api/ce/submit?projectKey=%s&projectName=%s&projectBranch=%s", @@ -143,13 +148,14 @@ public class ReportPublisher implements Startable { String dumpDirLocation = settings.getString(DUMP_REPORT_PROP_KEY); if (dumpDirLocation == null) { - uploadMultiPartReport(report, relativeUrl); + return uploadMultiPartReport(report, relativeUrl); } else { dumpReport(dumpDirLocation, effectiveKey, relativeUrl, report); + return null; } } - private void uploadMultiPartReport(File report, String relativeUrl) { + private String uploadMultiPartReport(File report, String relativeUrl) { LOG.debug("Publish results"); long startTime = System.currentTimeMillis(); URL url; @@ -177,6 +183,21 @@ public class ReportPublisher implements Startable { } long stopTime = System.currentTimeMillis(); LOG.info("Analysis reports sent to server in " + (stopTime - startTime) + "ms"); + String responseStr = request.body(); + SubmitResponse response = new Gson().fromJson(responseStr, SubmitResponse.class); + return response.getTaskId(); + } + + private static class SubmitResponse { + private String taskId; + + public String getTaskId() { + return taskId; + } + + public void setTaskId(String taskId) { + this.taskId = taskId; + } } private void dumpReport(String dumpDirLocation, String projectKey, String relativeUrl, File report) { @@ -203,7 +224,7 @@ public class ReportPublisher implements Startable { } @VisibleForTesting - void logSuccess(Logger logger) { + void logSuccess(Logger logger, @Nullable String taskId) { if (analysisMode.isIssues() || analysisMode.isMediumTest()) { logger.info("ANALYSIS SUCCESSFUL"); @@ -220,6 +241,10 @@ 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."); + if (taskId != null) { + String taskUrl = baseUrl + "api/ce/task?id=" + taskId; + logger.info("More about the report processing at {}", taskUrl); + } } } } 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 7d1c47d058d..c12e616e863 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 @@ -33,6 +33,7 @@ import org.sonar.batch.scan.ImmutableProjectReactor; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; public class ReportPublisherTest { @@ -56,10 +57,27 @@ public class ReportPublisherTest { mock(TempFolder.class), new ReportPublisherStep[0]); Logger logger = mock(Logger.class); - job.logSuccess(logger); + job.logSuccess(logger, null); verify(logger).info("ANALYSIS SUCCESSFUL, you can browse {}", "http://myserver/dashboard/index/struts"); verify(logger).info("Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report."); + verifyNoMoreInteractions(logger); + } + + @Test + public void should_log_successful_analysis_with_ce_task() { + Settings settings = new Settings(); + 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]); + + Logger logger = mock(Logger.class); + job.logSuccess(logger, "abc123"); + + verify(logger).info("ANALYSIS SUCCESSFUL, you can browse {}", "http://myserver/dashboard/index/struts"); + verify(logger).info("Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report."); + verify(logger).info("More about the report processing at {}", "http://myserver/api/ce/task?id=abc123"); + verifyNoMoreInteractions(logger); } @Test @@ -70,9 +88,10 @@ public class ReportPublisherTest { mock(TempFolder.class), new ReportPublisherStep[0]); Logger logger = mock(Logger.class); - job.logSuccess(logger); + job.logSuccess(logger, null); verify(logger).info("ANALYSIS SUCCESSFUL"); + verifyNoMoreInteractions(logger); } } -- 2.39.5