Skip to content

Commit c06c9e2

Browse files
SCANNERAPI-190 Don't override the SONAR_HOST_URL env variable if using Bitbucket Pipelines
1 parent 82a884b commit c06c9e2

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ public void execute(Map<String, String> taskProps) {
143143

144144
private void initGlobalDefaultValues() {
145145
String sonarHostUrl = system.getEnvironmentVariable(SONAR_HOST_URL_ENV_VAR);
146-
if (system.getEnvironmentVariable(BITBUCKET_CLOUD_ENV_VAR) != null) {
147-
setGlobalDefaultValue(ScannerProperties.HOST_URL, SONARCLOUD_HOST);
148-
logger.info("Bitbucket Cloud Pipelines detected");
149-
} else if (sonarHostUrl != null) {
146+
if (sonarHostUrl != null) {
150147
setGlobalDefaultValue(ScannerProperties.HOST_URL, sonarHostUrl);
148+
} else if (system.getEnvironmentVariable(BITBUCKET_CLOUD_ENV_VAR) != null) {
149+
setGlobalDefaultValue(ScannerProperties.HOST_URL, SONARCLOUD_HOST);
150+
logger.info("Bitbucket Cloud Pipelines detected, no host variable set. Defaulting to sonarcloud.io.");
151151
} else {
152152
setGlobalDefaultValue(ScannerProperties.HOST_URL, "http://localhost:9000");
153153
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,24 @@ public void should_set_localhost_as_host_by_default() {
106106
}
107107

108108
@Test
109-
public void should_set_sonarcloud_as_host_if_executed_from_bitbucket_cloud() {
109+
public void should_set_sonarcloud_as_host_if_executed_from_bitbucket_cloud_and_no_host_env() {
110110
when(system.getEnvironmentVariable("BITBUCKET_BUILD_NUMBER")).thenReturn("123");
111111

112112
scanner.start();
113113

114114
assertThat(scanner.globalProperty("sonar.host.url", null)).isEqualTo("https://sonarcloud.io");
115115
}
116116

117+
@Test
118+
public void should_set_url_from_env_as_host_if_host_env_var_provided_even_on_bitbucket_cloud() {
119+
when(system.getEnvironmentVariable("BITBUCKET_BUILD_NUMBER")).thenReturn("123");
120+
when(system.getEnvironmentVariable("SONAR_HOST_URL")).thenReturn("http://from-env.org:9000");
121+
122+
scanner.start();
123+
124+
assertThat(scanner.globalProperty("sonar.host.url", null)).isEqualTo("http://from-env.org:9000");
125+
}
126+
117127
@Test
118128
public void should_set_url_from_env_as_host_if_host_env_var_provided() {
119129
when(system.getEnvironmentVariable("SONAR_HOST_URL")).thenReturn("http://from-env.org:9000");

0 commit comments

Comments
 (0)