Skip to content

Commit 0783ad3

Browse files
SCANMAVEN-264 Add support for SonarQube Cloud regions (#284)
1 parent 8f52888 commit 0783ad3

File tree

6 files changed

+79
-12
lines changed

6 files changed

+79
-12
lines changed

cirrus/cirrus-qa.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ set -euo pipefail
1111
else
1212
export SQ_VERSION="${SQ_VERSION:-LATEST_RELEASE}"
1313
export MAVEN_VERSION="${MAVEN_VERSION:-3.9.4}"
14+
mvn --batch-mode --errors clean
15+
mvn --batch-mode --errors \
16+
--file 'sonar-maven-plugin/pom.xml' \
17+
-DskipTests=true -Dinvoker.skip=true \
18+
clean install
1419
fi
1520

16-
mvn --batch-mode --errors clean
17-
1821
# Install a specific version of Maven. We want to test against multiple versions.
1922
MAVEN_HOME_IT="${REPOSITORY_DIR}/target/downloaded-maven-${MAVEN_VERSION}"
2023
mkdir -p "${MAVEN_HOME_IT}"

sonar-maven-plugin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<dependency>
6565
<groupId>org.sonarsource.scanner.lib</groupId>
6666
<artifactId>sonar-scanner-java-library</artifactId>
67-
<version>3.2.1.385</version>
67+
<version>3.3.0.442</version>
6868
</dependency>
6969

7070
<dependency>

sonar-maven-plugin/src/main/java/org/sonarsource/scanner/maven/SonarQubeMojo.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.common.annotations.VisibleForTesting;
2323
import java.util.Arrays;
24+
import java.util.HashMap;
2425
import java.util.List;
2526
import java.util.Map;
2627
import java.util.stream.Stream;
@@ -82,6 +83,9 @@ public class SonarQubeMojo extends AbstractMojo {
8283
@Component
8384
private ToolchainManager toolchainManager;
8485

86+
// Visible for testing
87+
Map<String, String> environmentVariables = new HashMap<>(System.getenv());
88+
8589
@Override
8690
public void execute() throws MojoExecutionException, MojoFailureException {
8791

@@ -92,7 +96,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
9296

9397
warnAboutUnspecifiedSonarPluginVersion();
9498

95-
Map<String, String> envProps = EnvironmentConfig.load(System.getenv());
99+
Map<String, String> envProps = EnvironmentConfig.load(environmentVariables);
96100

97101
MavenCompilerResolver mavenCompilerResolver = new MavenCompilerResolver(session, lifecycleExecutor, getLog(), toolchainManager);
98102
MavenProjectConverter mavenProjectConverter = new MavenProjectConverter(getLog(), mavenCompilerResolver, envProps);

sonar-maven-plugin/src/main/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void execute() throws MojoExecutionException {
7373
throw new MojoFailureException("The scanner boostrapping has failed! See the logs for more details.");
7474
}
7575
try (ScannerEngineFacade engineFacade = bootstrapResult.getEngineFacade()) {
76-
if (!engineFacade.isSonarCloud()) {
76+
if (!engineFacade.isSonarQubeCloud()) {
7777
serverVersion = engineFacade.getServerVersion();
7878
checkSQVersion();
7979
}

sonar-maven-plugin/src/test/java/org/sonarsource/scanner/maven/SonarQubeMojoTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.junit.Test;
4848

4949
import static org.assertj.core.api.Assertions.assertThat;
50+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5051
import static org.assertj.core.api.Assertions.entry;
5152

5253
public class SonarQubeMojoTest {
@@ -285,11 +286,63 @@ public void verbose() throws Exception {
285286
assertThat(readProps("target/dump.properties")).contains((entry("sonar.verbose", "true")));
286287
}
287288

289+
@Test
290+
public void test_without_sonar_region() throws Exception {
291+
executeProject("sample-project", DEFAULT_GOAL);
292+
assertThat(readProps("target/dump.properties"))
293+
.contains((entry("sonar.host.url", "https://sonarcloud.io")))
294+
.contains((entry("sonar.scanner.apiBaseUrl", "https://api.sonarcloud.io")));
295+
}
296+
297+
@Test
298+
public void test_without_sonar_region_but_sonar_host_url() throws Exception {
299+
executeProject("sample-project", DEFAULT_GOAL, "sonar.host.url", "https://my.sonarqube.com/sonarqube");
300+
assertThat(readProps("target/dump.properties"))
301+
.contains((entry("sonar.host.url", "https://my.sonarqube.com/sonarqube")))
302+
.contains((entry("sonar.scanner.apiBaseUrl", "https://my.sonarqube.com/sonarqube/api/v2")));
303+
}
304+
305+
@Test
306+
public void test_without_sonar_region_but_sonar_host_url_env() throws Exception {
307+
var env = Map.of("SONAR_HOST_URL", "https://my.sonarqube.com/sonarqube");
308+
executeProject("sample-project", DEFAULT_GOAL, env);
309+
assertThat(readProps("target/dump.properties"))
310+
.contains((entry("sonar.host.url", "https://my.sonarqube.com/sonarqube")))
311+
.contains((entry("sonar.scanner.apiBaseUrl", "https://my.sonarqube.com/sonarqube/api/v2")));
312+
}
313+
314+
@Test
315+
public void test_sonar_region_us() throws Exception {
316+
executeProject("sample-project", DEFAULT_GOAL, "sonar.region", "us");
317+
assertThat(readProps("target/dump.properties"))
318+
.contains((entry("sonar.host.url", "https://sonarqube.us")))
319+
.contains((entry("sonar.scanner.apiBaseUrl", "https://api.sonarqube.us")));
320+
}
321+
322+
@Test
323+
public void test_sonar_region_us_using_env() throws Exception {
324+
var env = Map.of("SONAR_REGION", "us");
325+
executeProject("sample-project", DEFAULT_GOAL, env);
326+
assertThat(readProps("target/dump.properties"))
327+
.contains((entry("sonar.host.url", "https://sonarqube.us")))
328+
.contains((entry("sonar.scanner.apiBaseUrl", "https://api.sonarqube.us")));
329+
}
330+
331+
@Test
332+
public void test_sonar_region_invalid() {
333+
assertThatThrownBy(() -> executeProject("sample-project", DEFAULT_GOAL, "sonar.region", "invalid"))
334+
.hasMessageContaining("Invalid region 'invalid'.");
335+
}
336+
288337
private File executeProject(String projectName) throws Exception {
289338
return executeProject(projectName, DEFAULT_GOAL);
290339
}
291340

292341
private File executeProject(String projectName, String goal, String... properties) throws Exception {
342+
return executeProject(projectName, goal, Collections.emptyMap(), properties);
343+
}
344+
345+
private File executeProject(String projectName, String goal, Map<String, String> env, String... properties) throws Exception {
293346
File baseDir = new File("src/test/projects/" + projectName).getAbsoluteFile();
294347
SonarQubeMojo mojo = getMojo(baseDir);
295348
mojo.getSession().getRequest().setGoals(Collections.singletonList(goal));
@@ -312,6 +365,13 @@ private File executeProject(String projectName, String goal, String... propertie
312365
userProperties.put(properties[i], properties[i + 1]);
313366
}
314367

368+
// Clean environment variables. We don't want the context of the CI to interfere with the tests.
369+
// For example, we don't want the SONAR_HOST_URL to be set to https://next.sonarqube.com/sonarqube/
370+
mojo.environmentVariables.entrySet()
371+
.removeIf(entry -> entry.getKey().startsWith("SONAR_") || entry.getKey().startsWith("SONARQUBE_"));
372+
373+
mojo.environmentVariables.putAll(env);
374+
315375
mojo.execute();
316376

317377
return baseDir;

sonar-maven-plugin/src/test/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapperTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ void setUp()
131131

132132
@Test
133133
void testSQBefore56() {
134-
when(scannerEngineFacade.isSonarCloud()).thenReturn(false);
134+
when(scannerEngineFacade.isSonarQubeCloud()).thenReturn(false);
135135
when(scannerEngineFacade.getServerVersion()).thenReturn("5.1");
136136

137137
MojoExecutionException exception = assertThrows(MojoExecutionException.class,
@@ -144,7 +144,7 @@ void testSQBefore56() {
144144

145145
@Test
146146
void testSQ56() throws MojoExecutionException {
147-
when(scannerEngineFacade.isSonarCloud()).thenReturn(false);
147+
when(scannerEngineFacade.isSonarQubeCloud()).thenReturn(false);
148148
when(scannerEngineFacade.getServerVersion()).thenReturn("5.6");
149149
scannerBootstrapper.execute();
150150

@@ -155,7 +155,7 @@ void testSQ56() throws MojoExecutionException {
155155
void when_ScannerEngineBootstrapper_is_not_successful_getEngineFacade_should_not_be_called() {
156156
when(scannerEngineBootstrapResult.isSuccessful()).thenReturn(false);
157157
when(scannerEngineBootstrapResult.getEngineFacade()).thenThrow(new IllegalAccessError("Should not be called"));
158-
when(scannerEngineFacade.isSonarCloud()).thenReturn(false);
158+
when(scannerEngineFacade.isSonarQubeCloud()).thenReturn(false);
159159
when(scannerEngineFacade.getServerVersion()).thenReturn("5.6");
160160

161161
assertThatThrownBy( () -> scannerBootstrapper.execute())
@@ -166,7 +166,7 @@ void when_ScannerEngineBootstrapper_is_not_successful_getEngineFacade_should_not
166166
@Test
167167
void throw_an_exception_when_analyze_fail() {
168168
when(scannerEngineFacade.analyze(any())).thenReturn(false);
169-
when(scannerEngineFacade.isSonarCloud()).thenReturn(false);
169+
when(scannerEngineFacade.isSonarQubeCloud()).thenReturn(false);
170170
when(scannerEngineFacade.getServerVersion()).thenReturn("5.6");
171171

172172
assertThatThrownBy( () -> scannerBootstrapper.execute())
@@ -176,7 +176,7 @@ void throw_an_exception_when_analyze_fail() {
176176

177177
@Test
178178
void testVersionComparisonWithBuildNumber() throws MojoExecutionException {
179-
when(scannerEngineFacade.isSonarCloud()).thenReturn(false);
179+
when(scannerEngineFacade.isSonarQubeCloud()).thenReturn(false);
180180
when(scannerEngineFacade.getServerVersion()).thenReturn("6.3.0.12345");
181181
scannerBootstrapper.execute();
182182

@@ -309,7 +309,7 @@ class EnvironmentInformation {
309309
@BeforeEach
310310
void before() {
311311
when(scannerEngineFacade.getServerVersion()).thenReturn("9.9");
312-
when(scannerEngineFacade.isSonarCloud()).thenReturn(false);
312+
when(scannerEngineFacade.isSonarQubeCloud()).thenReturn(false);
313313
mockedSystem = mockStatic(SystemWrapper.class);
314314
}
315315

@@ -368,7 +368,7 @@ private void verifyCollectedSources(Consumer<String[]> sourceDirsAssertions) thr
368368
}
369369

370370
private void verifyCommonCalls() {
371-
verify(scannerEngineFacade).isSonarCloud();
371+
verify(scannerEngineFacade).isSonarQubeCloud();
372372
verify(scannerEngineFacade).analyze(projectProperties);
373373
}
374374
}

0 commit comments

Comments
 (0)