aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2015-07-02 12:13:04 +0200
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2015-07-02 16:14:34 +0200
commit5f9538c9a90d52cf29b7c58035fe2ff1ecb6375a (patch)
tree99b8392231c574664b4dbb286d9c5ab70796dfb8 /sonar-batch
parent2e2c1fbdc5f17b9d8577c7582139d7427c398589 (diff)
downloadsonarqube-5f9538c9a90d52cf29b7c58035fe2ff1ecb6375a.tar.gz
sonarqube-5f9538c9a90d52cf29b7c58035fe2ff1ecb6375a.zip
add ability to dump batch report enabled by sonar.batch.dumpReportDir
batch report dump are saved in the directory specified by the property sonar.batch.dumpReportDir and the batch is NOT pushed to the server
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java56
1 files changed, 48 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 5660e8fc23f..0f60fe4f876 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
@@ -22,9 +22,12 @@ package org.sonar.batch.report;
import com.github.kevinsawicki.http.HttpRequest;
import com.google.common.annotations.VisibleForTesting;
import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
+import java.net.URISyntaxException;
import java.net.URL;
+import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.picocontainer.Startable;
import org.slf4j.Logger;
@@ -41,11 +44,14 @@ import org.sonar.batch.bootstrap.ServerClient;
import org.sonar.batch.protocol.output.BatchReportWriter;
import org.sonar.batch.scan.ImmutableProjectReactor;
+import static java.lang.String.format;
+
@BatchSide
public class ReportPublisher implements Startable {
private static final Logger LOG = LoggerFactory.getLogger(ReportPublisher.class);
public static final String KEEP_REPORT_PROP_KEY = "sonar.batch.keepReport";
+ public static final String DUMP_REPORT_PROP_KEY = "sonar.batch.dumpReportDir";
private final ServerClient serverClient;
private final Server server;
@@ -98,7 +104,7 @@ public class ReportPublisher implements Startable {
if (!analysisMode.isPreview()) {
File report = prepareReport();
if (!analysisMode.isMediumTest()) {
- uploadMultiPartReport(report);
+ sendOrDumpReport(report);
}
}
logSuccess(LoggerFactory.getLogger(getClass()));
@@ -125,38 +131,72 @@ public class ReportPublisher implements Startable {
}
@VisibleForTesting
- void uploadMultiPartReport(File report) {
+ void sendOrDumpReport(File report) {
+ ProjectDefinition projectDefinition = projectReactor.getRoot();
+ String effectiveKey = projectDefinition.getKeyWithBranch();
+ String relativeUrl = "/api/computation/submit_report?projectKey=" + effectiveKey + "&projectName=" + ServerClient.encodeForUrl(projectDefinition.getName());
+
+ String dumpDirLocation = settings.getString(DUMP_REPORT_PROP_KEY);
+ if (dumpDirLocation == null) {
+ uploadMultiPartReport(report, relativeUrl);
+ } else {
+ dumpReport(dumpDirLocation, effectiveKey, relativeUrl, report);
+ }
+ }
+
+ private void uploadMultiPartReport(File report, String relativeUrl) {
LOG.debug("Publish results");
long startTime = System.currentTimeMillis();
URL url;
try {
- ProjectDefinition projectDefinition = projectReactor.getRoot();
- String effectiveKey = projectDefinition.getKeyWithBranch();
- url = new URL(serverClient.getURL() + "/api/computation/submit_report?projectKey=" + effectiveKey + "&projectName=" + ServerClient.encodeForUrl(projectDefinition.getName()));
+ url = new URL(serverClient.getURL() + relativeUrl);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Invalid URL", e);
}
HttpRequest request = HttpRequest.post(url);
request.trustAllCerts();
request.trustAllHosts();
- request.header("User-Agent", String.format("SonarQube %s", server.getVersion()));
+ request.header("User-Agent", format("SonarQube %s", server.getVersion()));
request.basic(serverClient.getLogin(), serverClient.getPassword());
request.part("report", null, "application/octet-stream", report);
if (!request.ok()) {
int responseCode = request.code();
if (responseCode == 401) {
- throw new IllegalStateException(String.format(serverClient.getMessageWhenNotAuthorized(), CoreProperties.LOGIN, CoreProperties.PASSWORD));
+ throw new IllegalStateException(format(serverClient.getMessageWhenNotAuthorized(), CoreProperties.LOGIN, CoreProperties.PASSWORD));
}
if (responseCode == 403) {
// SONAR-4397 Details are in response content
throw new IllegalStateException(request.body());
}
- throw new IllegalStateException(String.format("Fail to execute request [code=%s, url=%s]: %s", responseCode, url, request.body()));
+ throw new IllegalStateException(format("Fail to execute request [code=%s, url=%s]: %s", responseCode, url, request.body()));
}
long stopTime = System.currentTimeMillis();
LOG.info("Analysis reports sent to server in " + (stopTime - startTime) + "ms");
}
+ private void dumpReport(String dumpDirLocation, String projectKey, String relativeUrl, File report) {
+ LOG.debug("Dump report to file");
+ try {
+ dumpReportImpl(dumpDirLocation, projectKey, relativeUrl, report);
+ } catch (IOException | URISyntaxException e) {
+ LOG.error("Failed to dump report to directory " + dumpDirLocation, e);
+ }
+ }
+
+ private void dumpReportImpl(String dumpDirLocation, String projectKey, String relativeUrl, File report) throws IOException, URISyntaxException {
+ File dumpDir = new File(dumpDirLocation);
+ if (!dumpDir.exists() || !dumpDir.isDirectory()) {
+ LOG.warn("Report dump directory '{}' does not exist or is not a directory", dumpDirLocation);
+ return;
+ }
+ long dateTime = new Date().getTime();
+ File dumpedZip = new File(dumpDir, format("batch-report_%s_%s.zip", projectKey, dateTime));
+ FileUtils.copyFile(report, new FileOutputStream(dumpedZip));
+ File dumpedMetadata = new File(dumpDir, format("batch-report_%s_%s.txt", projectKey, dateTime));
+ FileUtils.write(dumpedMetadata, relativeUrl);
+ LOG.info("Batch report dumped to {}", dumpedZip.getAbsolutePath());
+ }
+
@VisibleForTesting
void logSuccess(Logger logger) {
if (analysisMode.isPreview() || analysisMode.isMediumTest()) {