diff --git a/src/main/java/org/sonarsource/scanner/maven/bootstrap/MavenProjectConverter.java b/src/main/java/org/sonarsource/scanner/maven/bootstrap/MavenProjectConverter.java index 0e37eb3b..64d53200 100644 --- a/src/main/java/org/sonarsource/scanner/maven/bootstrap/MavenProjectConverter.java +++ b/src/main/java/org/sonarsource/scanner/maven/bootstrap/MavenProjectConverter.java @@ -141,6 +141,10 @@ public boolean isSourceDirsOverridden() { return sourceDirsIsOverridden; } + public Properties getEnvProperties() { + return new Properties(envProperties); + } + Map configure(List mavenProjects, MavenProject root, Properties userProperties) throws MojoExecutionException { this.userProperties = userProperties; this.specifiedProjectKey = specifiedProjectKey(userProperties, root); @@ -545,8 +549,7 @@ private List sourcePaths(MavenProject pom, String propertyKey, Collection< boolean userDefined = false; - String prop = StringUtils.defaultIfEmpty(userProperties.getProperty(propertyKey), envProperties.getProperty(propertyKey)); - prop = StringUtils.defaultIfEmpty(prop, pom.getProperties().getProperty(propertyKey)); + String prop = getPropertyByKey(propertyKey, pom); if (prop != null) { List paths = Arrays.asList(StringUtils.split(prop, ",")); @@ -568,6 +571,16 @@ private List sourcePaths(MavenProject pom, String propertyKey, Collection< } } + private String getPropertyByKey(String propertyKey, MavenProject pom) { + return getPropertyByKey(propertyKey, pom, userProperties, envProperties); + } + + public static String getPropertyByKey(String propertyKey, MavenProject pom, Properties userProperties, Properties envProperties) { + String prop = StringUtils.defaultIfEmpty(userProperties.getProperty(propertyKey), envProperties.getProperty(propertyKey)); + prop = StringUtils.defaultIfEmpty(prop, pom.getProperties().getProperty(propertyKey)); + return prop; + } + private static List existingPathsOrFail(List dirs, MavenProject pom, String propertyKey) throws MojoExecutionException { for (File dir : dirs) { diff --git a/src/main/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapper.java b/src/main/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapper.java index f0beeb00..8a388d30 100644 --- a/src/main/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapper.java +++ b/src/main/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapper.java @@ -20,6 +20,7 @@ package org.sonarsource.scanner.maven.bootstrap; import com.google.common.annotations.VisibleForTesting; + import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -27,9 +28,11 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Properties; import java.util.Set; import java.util.stream.Collectors; + import org.apache.maven.artifact.versioning.ComparableVersion; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.MojoExecutionException; @@ -37,6 +40,9 @@ import org.apache.maven.project.MavenProject; import org.sonarsource.scanner.api.EmbeddedScanner; import org.sonarsource.scanner.api.ScanProperties; +import org.sonarsource.scanner.api.ScannerProperties; + +import static org.sonarsource.scanner.maven.bootstrap.MavenProjectConverter.getPropertyByKey; /** * Configure properties and bootstrap using SonarQube scanner API @@ -44,6 +50,7 @@ public class ScannerBootstrapper { static final String UNSUPPORTED_BELOW_SONARQUBE_56_MESSAGE = "With SonarQube server prior to 5.6, use sonar-maven-plugin <= 3.3"; + private static final String SONARCLOUD_HOST_URL = "https://sonarcloud.io"; private final Log log; private final MavenSession session; @@ -65,7 +72,15 @@ public void execute() throws MojoExecutionException { scanner.start(); serverVersion = scanner.serverVersion(); - checkSQVersion(); + if (isSonarCloudUsed()) { + log.info("Communicating with SonarCloud"); + } else { + if (serverVersion != null) { + log.info("Communicating with SonarQube Server " + serverVersion); + } + checkSQVersion(); + } + if (log.isDebugEnabled()) { scanner.setGlobalProperty("sonar.verbose", "true"); @@ -77,6 +92,20 @@ public void execute() throws MojoExecutionException { } } + + // TODO remove this workaround when discovering if the sevrer is SC or SQ is available through the API + private boolean isSonarCloudUsed() { + return session.getProjects().stream() + // We can use EnvProperties from MavenProjectConverter as they are initialized at construction time, + // but we can't use UserProperties from the MavenProjectConverter as they are only initialized + // in the "collectProperties" method. + .map(project -> + getPropertyByKey(ScannerProperties.HOST_URL, project, session.getUserProperties(), mavenProjectConverter.getEnvProperties()) + ) + .filter(Objects::nonNull) + .anyMatch(hostUrl -> hostUrl.startsWith(SONARCLOUD_HOST_URL)); + } + @VisibleForTesting Map collectProperties() throws MojoExecutionException { @@ -143,10 +172,6 @@ private void collectAllSources(Map props) { } private void checkSQVersion() { - if (serverVersion != null) { - log.info("SonarQube version: " + serverVersion); - } - if (isVersionPriorTo("5.6")) { throw new UnsupportedOperationException(UNSUPPORTED_BELOW_SONARQUBE_56_MESSAGE); } diff --git a/src/test/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapperTest.java b/src/test/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapperTest.java index 70fb4e5b..367a0837 100644 --- a/src/test/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapperTest.java +++ b/src/test/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapperTest.java @@ -105,6 +105,8 @@ public void setUp() when(mavenProjectConverter.configure(any(), any(), any())).thenReturn(projectProperties); + when(mavenProjectConverter.getEnvProperties()).thenReturn(new Properties()); + when(rootProject.getProperties()).thenReturn(new Properties()); when(scanner.mask(anyString())).thenReturn(scanner); when(scanner.unmask(anyString())).thenReturn(scanner); @@ -249,6 +251,28 @@ void can_collect_sources_with_commas_in_paths() throws MojoExecutionException, I assertThat(values).hasSize(4); } + @Test + void test_logging_SQ_version() throws MojoExecutionException { + when(scanner.serverVersion()).thenReturn("10.5"); + scannerBootstrapper.execute(); + + verify(log).info("Communicating with SonarQube Server 10.5"); + } + + @Test + void test_not_logging_the_version_when_sonarcloud_is_used() throws MojoExecutionException { + // if SC is the server this property value should be ignored + when(scanner.serverVersion()).thenReturn("8.0"); + + Properties withSonarCloudHost = new Properties(); + withSonarCloudHost.put("sonar.host.url", "https://sonarcloud.io"); + when(session.getUserProperties()).thenReturn(withSonarCloudHost); + scannerBootstrapper.execute(); + + verify(log).info("Communicating with SonarCloud"); + verify(log, never()).info("Communicating with SonarQube Server 8.0"); + } + private void verifyCommonCalls() { verify(scanner).start(); verify(scanner).serverVersion();