Skip to content

Commit 9586c1d

Browse files
SCANNERAPI-180 sonarcloud.io a default host for BitbucketCloud
Make sonarcloud.io the default host when scanner is executed from BitbucketCloud pipeline.
1 parent c1bca63 commit 9586c1d

3 files changed

Lines changed: 74 additions & 7 deletions

File tree

api/src/main/java/org/sonarsource/scanner/api/EmbeddedScanner.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,40 @@
3636

3737
/**
3838
* Entry point to run SonarQube analysis programmatically.
39+
*
3940
* @since 2.2
4041
*/
4142
public class EmbeddedScanner {
43+
private static final String BITBUCKET_CLOUD_ENV_VAR = "BITBUCKET_BUILD_NUMBER";
44+
private static final String SONARCLOUD_HOST = "https://sonarcloud.io";
4245
private final IsolatedLauncherFactory launcherFactory;
4346
private IsolatedLauncher launcher;
4447
private final LogOutput logOutput;
4548
private final Map<String, String> globalProperties = new HashMap<>();
4649
private final Logger logger;
4750
private final Set<String> classloaderMask = new HashSet<>();
4851
private final Set<String> classloaderUnmask = new HashSet<>();
52+
private final System2 system;
4953

50-
EmbeddedScanner(IsolatedLauncherFactory bl, Logger logger, LogOutput logOutput) {
54+
EmbeddedScanner(IsolatedLauncherFactory bl, Logger logger, LogOutput logOutput, System2 system) {
5155
this.logger = logger;
5256
this.launcherFactory = bl;
5357
this.logOutput = logOutput;
5458
this.classloaderUnmask.add("org.sonarsource.scanner.api.internal.batch.");
59+
this.system = system;
5560
}
5661

57-
public static EmbeddedScanner create(String app, String version, final LogOutput logOutput) {
62+
public static EmbeddedScanner create(String app, String version, final LogOutput logOutput, System2 system2) {
5863
Logger logger = new LoggerAdapter(logOutput);
59-
return new EmbeddedScanner(new IsolatedLauncherFactory(logger), logger, logOutput)
64+
return new EmbeddedScanner(new IsolatedLauncherFactory(logger), logger, logOutput, system2)
6065
.setGlobalProperty(InternalProperties.SCANNER_APP, app)
6166
.setGlobalProperty(InternalProperties.SCANNER_APP_VERSION, version);
6267
}
6368

69+
public static EmbeddedScanner create(String app, String version, final LogOutput logOutput) {
70+
return create(app, version, logOutput, new System2());
71+
}
72+
6473
public Map<String, String> globalProperties() {
6574
return globalProperties;
6675
}
@@ -132,7 +141,12 @@ public void execute(Map<String, String> taskProps) {
132141
}
133142

134143
private void initGlobalDefaultValues() {
135-
setGlobalDefaultValue(ScannerProperties.HOST_URL, "http://localhost:9000");
144+
if (system.getEnvironmentVariable(BITBUCKET_CLOUD_ENV_VAR) != null) {
145+
setGlobalDefaultValue(ScannerProperties.HOST_URL, SONARCLOUD_HOST);
146+
logger.info("Bitbucket Cloud Pipelines detected");
147+
} else {
148+
setGlobalDefaultValue(ScannerProperties.HOST_URL, "http://localhost:9000");
149+
}
136150
}
137151

138152
private void initAnalysisProperties(Map<String, String> p) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* SonarQube Scanner API
3+
* Copyright (C) 2011-2018 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonarsource.scanner.api;
21+
22+
import javax.annotation.CheckForNull;
23+
import javax.annotation.Nonnull;
24+
25+
/**
26+
* A proxy class for java.lang.System (for mocking).
27+
*/
28+
public class System2 {
29+
@CheckForNull
30+
public String getEnvironmentVariable(@Nonnull String key) {
31+
return System.getenv(key);
32+
}
33+
}

api/src/test/java/org/sonarsource/scanner/api/EmbeddedScannerTest.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
import static org.assertj.core.api.Assertions.assertThat;
4242
import static org.mockito.ArgumentMatchers.any;
43+
import static org.mockito.ArgumentMatchers.anyMap;
4344
import static org.mockito.ArgumentMatchers.argThat;
4445
import static org.mockito.Mockito.mock;
4546
import static org.mockito.Mockito.verify;
@@ -62,15 +63,18 @@ public void should_create() {
6263
private IsolatedLauncher launcher;
6364
private EmbeddedScanner scanner;
6465
private Logger logger;
66+
private System2 system;
6567

6668
@Before
6769
public void setUp() {
6870
launcherFactory = mock(IsolatedLauncherFactory.class);
6971
launcher = mock(IsolatedLauncher.class);
7072
logger = mock(Logger.class);
73+
system = mock(System2.class);
74+
7175
when(launcher.getVersion()).thenReturn("5.2");
72-
when(launcherFactory.createLauncher(any(Map.class), any(ClassloadRules.class))).thenReturn(launcher);
73-
scanner = new EmbeddedScanner(launcherFactory, logger, mock(LogOutput.class));
76+
when(launcherFactory.createLauncher(anyMap(), any(ClassloadRules.class))).thenReturn(launcher);
77+
scanner = new EmbeddedScanner(launcherFactory, logger, mock(LogOutput.class), system);
7478
}
7579

7680
@Test
@@ -94,6 +98,22 @@ public void test_app() {
9498
assertThat(scanner.appVersion()).isEqualTo("3.1");
9599
}
96100

101+
@Test
102+
public void should_set_localhost_as_host_by_default() {
103+
scanner.start();
104+
105+
assertThat(scanner.globalProperty("sonar.host.url", null)).isEqualTo("http://localhost:9000");
106+
}
107+
108+
@Test
109+
public void should_set_sonarcloud_as_host_if_executed_from_bitbucket_cloud() {
110+
when(system.getEnvironmentVariable("BITBUCKET_BUILD_NUMBER")).thenReturn("123");
111+
112+
scanner.start();
113+
114+
assertThat(scanner.globalProperty("sonar.host.url", null)).isEqualTo("https://sonarcloud.io");
115+
}
116+
97117
@Test
98118
public void should_set_properties() {
99119
EmbeddedScanner scanner = EmbeddedScanner.create("test", "1.0", mock(LogOutput.class));
@@ -167,7 +187,7 @@ public boolean matches(Map o) {
167187
@Test
168188
public void should_launch_in_simulation_mode() throws IOException {
169189
launcherFactory = new IsolatedLauncherFactory(mock(Logger.class));
170-
scanner = new EmbeddedScanner(launcherFactory, mock(Logger.class), mock(LogOutput.class));
190+
scanner = new EmbeddedScanner(launcherFactory, mock(Logger.class), mock(LogOutput.class), system);
171191

172192
File dump = temp.newFile();
173193
Map<String, String> p = new HashMap<>();

0 commit comments

Comments
 (0)