aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJanos Gyerik <janos.gyerik@sonarsource.com>2017-11-30 16:25:41 +0100
committerJanos Gyerik <janos.gyerik@sonarsource.com>2017-12-05 10:47:46 +0100
commit5a455faaea8e112b6d8c1500cfcbfeccb9c0f9d6 (patch)
tree9fb7d94887de671d828f3aaa5809f2569e5a21fd
parent4621e7abe869caa475adcd4c16e81ea4eb4fb249 (diff)
downloadsonarqube-5a455faaea8e112b6d8c1500cfcbfeccb9c0f9d6.tar.gz
sonarqube-5a455faaea8e112b6d8c1500cfcbfeccb9c0f9d6.zip
SONAR-10126 Add relative path of project basedir to scanner report
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java49
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java1
-rw-r--r--sonar-scanner-protocol/src/main/protobuf/scanner_report.proto3
3 files changed, 51 insertions, 2 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java
index 0a1caea0821..86ed6c122c4 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java
@@ -19,11 +19,18 @@
*/
package org.sonar.scanner.report;
+import java.io.File;
+import java.nio.file.Path;
import java.util.Map.Entry;
import java.util.Optional;
+import java.util.regex.Pattern;
+import javax.annotation.Nullable;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.batch.fs.internal.InputModuleHierarchy;
+import org.sonar.api.batch.scm.ScmProvider;
import org.sonar.api.config.Configuration;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
import org.sonar.scanner.ProjectAnalysisInfo;
import org.sonar.scanner.bootstrap.ScannerPlugin;
import org.sonar.scanner.bootstrap.ScannerPluginRepository;
@@ -34,11 +41,14 @@ import org.sonar.scanner.protocol.output.ScannerReportWriter;
import org.sonar.scanner.rule.ModuleQProfiles;
import org.sonar.scanner.rule.QProfile;
import org.sonar.scanner.scan.branch.BranchConfiguration;
+import org.sonar.scanner.scm.ScmConfiguration;
import static org.sonar.core.config.ScannerProperties.ORGANIZATION;
public class MetadataPublisher implements ReportPublisherStep {
+ private static final Logger LOG = Loggers.get(MetadataPublisher.class);
+
private final Configuration settings;
private final ModuleQProfiles qProfiles;
private final ProjectAnalysisInfo projectAnalysisInfo;
@@ -47,8 +57,12 @@ public class MetadataPublisher implements ReportPublisherStep {
private final ScannerPluginRepository pluginRepository;
private final BranchConfiguration branchConfiguration;
+ @Nullable
+ private final ScmConfiguration scmConfiguration;
+
public MetadataPublisher(ProjectAnalysisInfo projectAnalysisInfo, InputModuleHierarchy moduleHierarchy, Configuration settings,
- ModuleQProfiles qProfiles, CpdSettings cpdSettings, ScannerPluginRepository pluginRepository, BranchConfiguration branchConfiguration) {
+ ModuleQProfiles qProfiles, CpdSettings cpdSettings, ScannerPluginRepository pluginRepository, BranchConfiguration branchConfiguration,
+ @Nullable ScmConfiguration scmConfiguration) {
this.projectAnalysisInfo = projectAnalysisInfo;
this.moduleHierarchy = moduleHierarchy;
this.settings = settings;
@@ -56,6 +70,12 @@ public class MetadataPublisher implements ReportPublisherStep {
this.cpdSettings = cpdSettings;
this.pluginRepository = pluginRepository;
this.branchConfiguration = branchConfiguration;
+ this.scmConfiguration = scmConfiguration;
+ }
+
+ public MetadataPublisher(ProjectAnalysisInfo projectAnalysisInfo, InputModuleHierarchy moduleHierarchy, Configuration settings,
+ ModuleQProfiles qProfiles, CpdSettings cpdSettings, ScannerPluginRepository pluginRepository, BranchConfiguration branchConfiguration) {
+ this(projectAnalysisInfo, moduleHierarchy, settings, qProfiles, cpdSettings, pluginRepository, branchConfiguration, null);
}
@Override
@@ -80,6 +100,23 @@ public class MetadataPublisher implements ReportPublisherStep {
}
Optional.ofNullable(rootProject.getBranch()).ifPresent(builder::setDeprecatedBranch);
+ if (scmConfiguration != null) {
+ ScmProvider scmProvider = scmConfiguration.provider();
+ if (scmProvider != null) {
+ Path projectBasedir = moduleHierarchy.root().getBaseDir();
+ try {
+ builder.setRelativePathFromScmRoot(toSonarQubePath(scmProvider.relativePathFromScmRoot(projectBasedir)));
+ } catch (UnsupportedOperationException e) {
+ LOG.debug(e.getMessage());
+ }
+ try {
+ builder.setScmRevisionId(scmProvider.revisionId(projectBasedir));
+ } catch (UnsupportedOperationException e) {
+ LOG.debug(e.getMessage());
+ }
+ }
+ }
+
for (QProfile qp : qProfiles.findAll()) {
builder.getMutableQprofilesPerLanguage().put(qp.getLanguage(), ScannerReport.Metadata.QProfile.newBuilder()
.setKey(qp.getKey())
@@ -101,4 +138,14 @@ public class MetadataPublisher implements ReportPublisherStep {
}
return BranchType.SHORT;
}
+
+ private static String toSonarQubePath(Path path) {
+ String pathAsString = path.toString();
+ char sonarQubeSeparatorChar = '/';
+ if (File.separatorChar != sonarQubeSeparatorChar) {
+ return pathAsString.replaceAll(Pattern.quote(File.separator), String.valueOf(sonarQubeSeparatorChar));
+ }
+ return pathAsString;
+ }
+
}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java
index 618ce7fdf58..62011472da8 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java
@@ -504,5 +504,4 @@ public class ScannerMediumTester extends ExternalResource {
}
}
}
-
}
diff --git a/sonar-scanner-protocol/src/main/protobuf/scanner_report.proto b/sonar-scanner-protocol/src/main/protobuf/scanner_report.proto
index 5662ed6cfb2..e939fa8855e 100644
--- a/sonar-scanner-protocol/src/main/protobuf/scanner_report.proto
+++ b/sonar-scanner-protocol/src/main/protobuf/scanner_report.proto
@@ -43,6 +43,9 @@ message Metadata {
BranchType branch_type = 10;
string merge_branch_name = 11;
+ string relative_path_from_scm_root = 12;
+ string scm_revision_id = 13;
+
message QProfile {
string key = 1;
string name = 2;