Skip to content

Commit 728aa4f

Browse files
SCANNERAPI-184 - read SONAR_HOST_URL env property (#69)
1 parent 6a38b62 commit 728aa4f

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
*/
4242
public class EmbeddedScanner {
4343
private static final String BITBUCKET_CLOUD_ENV_VAR = "BITBUCKET_BUILD_NUMBER";
44+
private static final String SONAR_HOST_URL_ENV_VAR = "SONAR_HOST_URL";
4445
private static final String SONARCLOUD_HOST = "https://sonarcloud.io";
4546
private final IsolatedLauncherFactory launcherFactory;
4647
private IsolatedLauncher launcher;
@@ -141,9 +142,12 @@ public void execute(Map<String, String> taskProps) {
141142
}
142143

143144
private void initGlobalDefaultValues() {
145+
String sonarHostUrl = system.getEnvironmentVariable(SONAR_HOST_URL_ENV_VAR);
144146
if (system.getEnvironmentVariable(BITBUCKET_CLOUD_ENV_VAR) != null) {
145147
setGlobalDefaultValue(ScannerProperties.HOST_URL, SONARCLOUD_HOST);
146148
logger.info("Bitbucket Cloud Pipelines detected");
149+
} else if (sonarHostUrl != null) {
150+
setGlobalDefaultValue(ScannerProperties.HOST_URL, sonarHostUrl);
147151
} else {
148152
setGlobalDefaultValue(ScannerProperties.HOST_URL, "http://localhost:9000");
149153
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ public void should_set_sonarcloud_as_host_if_executed_from_bitbucket_cloud() {
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() {
119+
when(system.getEnvironmentVariable("SONAR_HOST_URL")).thenReturn("http://from-env.org:9000");
120+
121+
scanner.start();
122+
123+
assertThat(scanner.globalProperty("sonar.host.url", null)).isEqualTo("http://from-env.org:9000");
124+
}
125+
117126
@Test
118127
public void should_set_properties() {
119128
EmbeddedScanner scanner = EmbeddedScanner.create("test", "1.0", mock(LogOutput.class));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void task_should_not_require_project() {
7878
public void delete_non_empty_directory() throws IOException {
7979
/*-
8080
* Create test structure:
81-
* tmp
81+
* tmp
8282
* |-folder1
8383
* |- file1
8484
* |- folder2

0 commit comments

Comments
 (0)