diff options
author | Michal Duda <michal.duda@sonarsource.com> | 2019-05-07 14:28:09 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-05-22 20:21:17 +0200 |
commit | 8f699a85b3f9f87df99e2a91550e4da60d092cc7 (patch) | |
tree | 34c2ba8dec656e48e317be851a82ba1b9f10668f /sonar-scanner-engine | |
parent | 110e263d93cb23f6134c7e263dccbe994842254a (diff) | |
download | sonarqube-8f699a85b3f9f87df99e2a91550e4da60d092cc7.tar.gz sonarqube-8f699a85b3f9f87df99e2a91550e4da60d092cc7.zip |
SONAR-11950 add scm revision scanner parameter
Diffstat (limited to 'sonar-scanner-engine')
3 files changed, 33 insertions, 4 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 9bf40f4a94e..dc79f7262f4 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 @@ -23,6 +23,7 @@ import java.io.File; import java.nio.file.Path; import java.util.LinkedList; 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.AbstractProjectOrModule; @@ -145,13 +146,23 @@ public class MetadataPublisher implements ReportPublisherStep { LOG.debug(e.getMessage()); } try { - builder.setScmRevisionId(scmProvider.revisionId(projectBasedir)); + computeScmRevision().ifPresent(builder::setScmRevisionId); } catch (UnsupportedOperationException e) { LOG.debug(e.getMessage()); } } } + private Optional<String> computeScmRevision() { + Optional<String> scmRevision = properties.getScmRevision(); + ScmProvider scmProvider = scmConfiguration.provider(); + if (!scmRevision.isPresent() && scmProvider != null) { + scmRevision = Optional.ofNullable(scmProvider.revisionId(moduleHierarchy.root().getBaseDir())); + } + + return scmRevision; + } + private void addBranchInformation(ScannerReport.Metadata.Builder builder) { builder.setBranchName(branchConfiguration.branchName()); BranchType branchType = toProtobufBranchType(branchConfiguration.branchType()); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ScanProperties.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ScanProperties.java index 1da90cf6bf7..36fd9b5d41e 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ScanProperties.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ScanProperties.java @@ -40,6 +40,7 @@ public class ScanProperties { public static final String SONAR_REPORT_EXPORT_PATH = "sonar.report.export.path"; public static final String PRELOAD_FILE_METADATA_KEY = "sonar.preloadFileMetadata"; public static final String FORCE_RELOAD_KEY = "sonar.scm.forceReloadAll"; + public static final String SCM_REVISION = "sonar.scm.revision"; private final Configuration configuration; private final DefaultInputProject project; @@ -78,6 +79,10 @@ public class ScanProperties { } } + public Optional<String> getScmRevision() { + return configuration.get(SCM_REVISION); + } + /** * This should be called in the beginning of the analysis to fail fast */ diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java index 2c90a1a8eed..cf83df2448d 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java @@ -143,9 +143,9 @@ public class MetadataPublisherTest { .setRulesUpdatedAt(date.getTime()) .build())); assertThat(metadata.getPluginsByKey()).containsOnly(entry("java", org.sonar.scanner.protocol.output.ScannerReport.Metadata.Plugin.newBuilder() - .setKey("java") - .setUpdatedAt(12345) - .build()), + .setKey("java") + .setUpdatedAt(12345) + .build()), entry("php", org.sonar.scanner.protocol.output.ScannerReport.Metadata.Plugin.newBuilder() .setKey("php") .setUpdatedAt(45678) @@ -304,6 +304,19 @@ public class MetadataPublisherTest { assertThat(metadata.getScmRevisionId()).isEqualTo(revisionId); } + public void revision_from_scanner_props_overrides_scm_provider_revision_if_specified() throws IOException { + String revisionId = "some-sha1"; + when(scmProvider.revisionId(any(Path.class))).thenReturn(revisionId); + when(properties.getScmRevision()).thenReturn(Optional.of("123")); + + File outputDir = temp.newFolder(); + underTest.publish(new ScannerReportWriter(outputDir)); + + ScannerReportReader reader = new ScannerReportReader(outputDir); + ScannerReport.Metadata metadata = reader.readMetadata(); + assertThat(metadata.getScmRevisionId()).isEqualTo("123"); + } + @Test public void should_not_crash_when_scm_provider_does_not_support_relativePathFromScmRoot() throws IOException { String revisionId = "some-sha1"; |