Skip to content

Commit

Permalink
MSONAR-204 "SonarQube version" shouldn't be displayed when communicat…
Browse files Browse the repository at this point in the history
…ing with SonarCloud
  • Loading branch information
leveretka authored Feb 29, 2024
1 parent dd4f9cd commit 4b86947
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ public boolean isSourceDirsOverridden() {
return sourceDirsIsOverridden;
}

public Properties getEnvProperties() {
return new Properties(envProperties);
}

Map<String, String> configure(List<MavenProject> mavenProjects, MavenProject root, Properties userProperties) throws MojoExecutionException {
this.userProperties = userProperties;
this.specifiedProjectKey = specifiedProjectKey(userProperties, root);
Expand Down Expand Up @@ -545,8 +549,7 @@ private List<File> 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<String> paths = Arrays.asList(StringUtils.split(prop, ","));
Expand All @@ -568,6 +571,16 @@ private List<File> 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<File> existingPathsOrFail(List<File> dirs, MavenProject pom, String propertyKey)
throws MojoExecutionException {
for (File dir : dirs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,37 @@
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;
import java.nio.file.Paths;
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;
import org.apache.maven.plugin.logging.Log;
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
*/
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;
Expand All @@ -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");
Expand All @@ -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<String, String> collectProperties()
throws MojoExecutionException {
Expand Down Expand Up @@ -143,10 +172,6 @@ private void collectAllSources(Map<String, String> props) {
}

private void checkSQVersion() {
if (serverVersion != null) {
log.info("SonarQube version: " + serverVersion);
}

if (isVersionPriorTo("5.6")) {
throw new UnsupportedOperationException(UNSUPPORTED_BELOW_SONARQUBE_56_MESSAGE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 4b86947

Please sign in to comment.