Skip to content

Commit 4b86947

Browse files
authored
MSONAR-204 "SonarQube version" shouldn't be displayed when communicating with SonarCloud
1 parent dd4f9cd commit 4b86947

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

src/main/java/org/sonarsource/scanner/maven/bootstrap/MavenProjectConverter.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ public boolean isSourceDirsOverridden() {
141141
return sourceDirsIsOverridden;
142142
}
143143

144+
public Properties getEnvProperties() {
145+
return new Properties(envProperties);
146+
}
147+
144148
Map<String, String> configure(List<MavenProject> mavenProjects, MavenProject root, Properties userProperties) throws MojoExecutionException {
145149
this.userProperties = userProperties;
146150
this.specifiedProjectKey = specifiedProjectKey(userProperties, root);
@@ -545,8 +549,7 @@ private List<File> sourcePaths(MavenProject pom, String propertyKey, Collection<
545549
boolean userDefined = false;
546550

547551

548-
String prop = StringUtils.defaultIfEmpty(userProperties.getProperty(propertyKey), envProperties.getProperty(propertyKey));
549-
prop = StringUtils.defaultIfEmpty(prop, pom.getProperties().getProperty(propertyKey));
552+
String prop = getPropertyByKey(propertyKey, pom);
550553

551554
if (prop != null) {
552555
List<String> paths = Arrays.asList(StringUtils.split(prop, ","));
@@ -568,6 +571,16 @@ private List<File> sourcePaths(MavenProject pom, String propertyKey, Collection<
568571
}
569572
}
570573

574+
private String getPropertyByKey(String propertyKey, MavenProject pom) {
575+
return getPropertyByKey(propertyKey, pom, userProperties, envProperties);
576+
}
577+
578+
public static String getPropertyByKey(String propertyKey, MavenProject pom, Properties userProperties, Properties envProperties) {
579+
String prop = StringUtils.defaultIfEmpty(userProperties.getProperty(propertyKey), envProperties.getProperty(propertyKey));
580+
prop = StringUtils.defaultIfEmpty(prop, pom.getProperties().getProperty(propertyKey));
581+
return prop;
582+
}
583+
571584
private static List<File> existingPathsOrFail(List<File> dirs, MavenProject pom, String propertyKey)
572585
throws MojoExecutionException {
573586
for (File dir : dirs) {

src/main/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapper.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,37 @@
2020
package org.sonarsource.scanner.maven.bootstrap;
2121

2222
import com.google.common.annotations.VisibleForTesting;
23+
2324
import java.io.IOException;
2425
import java.nio.file.Files;
2526
import java.nio.file.Path;
2627
import java.nio.file.Paths;
2728
import java.util.ArrayList;
2829
import java.util.List;
2930
import java.util.Map;
31+
import java.util.Objects;
3032
import java.util.Properties;
3133
import java.util.Set;
3234
import java.util.stream.Collectors;
35+
3336
import org.apache.maven.artifact.versioning.ComparableVersion;
3437
import org.apache.maven.execution.MavenSession;
3538
import org.apache.maven.plugin.MojoExecutionException;
3639
import org.apache.maven.plugin.logging.Log;
3740
import org.apache.maven.project.MavenProject;
3841
import org.sonarsource.scanner.api.EmbeddedScanner;
3942
import org.sonarsource.scanner.api.ScanProperties;
43+
import org.sonarsource.scanner.api.ScannerProperties;
44+
45+
import static org.sonarsource.scanner.maven.bootstrap.MavenProjectConverter.getPropertyByKey;
4046

4147
/**
4248
* Configure properties and bootstrap using SonarQube scanner API
4349
*/
4450
public class ScannerBootstrapper {
4551

4652
static final String UNSUPPORTED_BELOW_SONARQUBE_56_MESSAGE = "With SonarQube server prior to 5.6, use sonar-maven-plugin <= 3.3";
53+
private static final String SONARCLOUD_HOST_URL = "https://sonarcloud.io";
4754

4855
private final Log log;
4956
private final MavenSession session;
@@ -65,7 +72,15 @@ public void execute() throws MojoExecutionException {
6572
scanner.start();
6673
serverVersion = scanner.serverVersion();
6774

68-
checkSQVersion();
75+
if (isSonarCloudUsed()) {
76+
log.info("Communicating with SonarCloud");
77+
} else {
78+
if (serverVersion != null) {
79+
log.info("Communicating with SonarQube Server " + serverVersion);
80+
}
81+
checkSQVersion();
82+
}
83+
6984

7085
if (log.isDebugEnabled()) {
7186
scanner.setGlobalProperty("sonar.verbose", "true");
@@ -77,6 +92,20 @@ public void execute() throws MojoExecutionException {
7792
}
7893
}
7994

95+
96+
// TODO remove this workaround when discovering if the sevrer is SC or SQ is available through the API
97+
private boolean isSonarCloudUsed() {
98+
return session.getProjects().stream()
99+
// We can use EnvProperties from MavenProjectConverter as they are initialized at construction time,
100+
// but we can't use UserProperties from the MavenProjectConverter as they are only initialized
101+
// in the "collectProperties" method.
102+
.map(project ->
103+
getPropertyByKey(ScannerProperties.HOST_URL, project, session.getUserProperties(), mavenProjectConverter.getEnvProperties())
104+
)
105+
.filter(Objects::nonNull)
106+
.anyMatch(hostUrl -> hostUrl.startsWith(SONARCLOUD_HOST_URL));
107+
}
108+
80109
@VisibleForTesting
81110
Map<String, String> collectProperties()
82111
throws MojoExecutionException {
@@ -143,10 +172,6 @@ private void collectAllSources(Map<String, String> props) {
143172
}
144173

145174
private void checkSQVersion() {
146-
if (serverVersion != null) {
147-
log.info("SonarQube version: " + serverVersion);
148-
}
149-
150175
if (isVersionPriorTo("5.6")) {
151176
throw new UnsupportedOperationException(UNSUPPORTED_BELOW_SONARQUBE_56_MESSAGE);
152177
}

src/test/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapperTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ public void setUp()
105105

106106

107107
when(mavenProjectConverter.configure(any(), any(), any())).thenReturn(projectProperties);
108+
when(mavenProjectConverter.getEnvProperties()).thenReturn(new Properties());
109+
when(rootProject.getProperties()).thenReturn(new Properties());
108110

109111
when(scanner.mask(anyString())).thenReturn(scanner);
110112
when(scanner.unmask(anyString())).thenReturn(scanner);
@@ -249,6 +251,28 @@ void can_collect_sources_with_commas_in_paths() throws MojoExecutionException, I
249251
assertThat(values).hasSize(4);
250252
}
251253

254+
@Test
255+
void test_logging_SQ_version() throws MojoExecutionException {
256+
when(scanner.serverVersion()).thenReturn("10.5");
257+
scannerBootstrapper.execute();
258+
259+
verify(log).info("Communicating with SonarQube Server 10.5");
260+
}
261+
262+
@Test
263+
void test_not_logging_the_version_when_sonarcloud_is_used() throws MojoExecutionException {
264+
// if SC is the server this property value should be ignored
265+
when(scanner.serverVersion()).thenReturn("8.0");
266+
267+
Properties withSonarCloudHost = new Properties();
268+
withSonarCloudHost.put("sonar.host.url", "https://sonarcloud.io");
269+
when(session.getUserProperties()).thenReturn(withSonarCloudHost);
270+
scannerBootstrapper.execute();
271+
272+
verify(log).info("Communicating with SonarCloud");
273+
verify(log, never()).info("Communicating with SonarQube Server 8.0");
274+
}
275+
252276
private void verifyCommonCalls() {
253277
verify(scanner).start();
254278
verify(scanner).serverVersion();

0 commit comments

Comments
 (0)