From: Eric Giffon Date: Wed, 8 Feb 2023 14:16:21 +0000 (+0100) Subject: SONAR-18215 Add property to override the documentation base url X-Git-Tag: 10.0.0.68432~227 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b7be23ba79c2211d758ae005dbe456faae385a77;p=sonarqube.git SONAR-18215 Add property to override the documentation base url --- diff --git a/sonar-core/src/main/java/org/sonar/core/config/CorePropertyDefinitions.java b/sonar-core/src/main/java/org/sonar/core/config/CorePropertyDefinitions.java index cf2e851af1e..360e34ffee6 100644 --- a/sonar-core/src/main/java/org/sonar/core/config/CorePropertyDefinitions.java +++ b/sonar-core/src/main/java/org/sonar/core/config/CorePropertyDefinitions.java @@ -28,6 +28,7 @@ import org.sonar.api.PropertyType; import org.sonar.api.config.EmailSettings; import org.sonar.api.config.PropertyDefinition; import org.sonar.api.resources.Qualifiers; +import org.sonar.core.documentation.DefaultDocumentationLinkGenerator; import org.sonar.core.extension.PluginRiskConsent; import static java.util.Arrays.asList; @@ -47,6 +48,9 @@ public class CorePropertyDefinitions { public static final String DISABLE_NOTIFICATION_ON_BUILT_IN_QPROFILES = "sonar.builtInQualityProfiles.disableNotificationOnUpdate"; public static final String PLUGINS_RISK_CONSENT = "sonar.plugins.risk.consent"; + + public static final String DOCUMENTATION_BASE_URL = "sonar.documentation.baseUrl"; + public static final String SUBCATEGORY_PROJECT_CREATION = "subProjectCreation"; private CorePropertyDefinitions() { @@ -118,6 +122,13 @@ public class CorePropertyDefinitions { .hidden() .type(SINGLE_SELECT_LIST) .build(), + PropertyDefinition.builder(DOCUMENTATION_BASE_URL) + .name("Base URL of the documentation") + .description("Base URL to be used in SonarQube documentation links, such as https://docs.sonarqube.org/") + .defaultValue(DefaultDocumentationLinkGenerator.DOCUMENTATION_PUBLIC_URL) + .hidden() + .type(STRING) + .build(), // WEB LOOK&FEEL PropertyDefinition.builder(WebConstants.SONAR_LF_LOGO_URL) diff --git a/sonar-core/src/main/java/org/sonar/core/documentation/DefaultDocumentationLinkGenerator.java b/sonar-core/src/main/java/org/sonar/core/documentation/DefaultDocumentationLinkGenerator.java index 470a7b24f8b..6b447820b0d 100644 --- a/sonar-core/src/main/java/org/sonar/core/documentation/DefaultDocumentationLinkGenerator.java +++ b/sonar-core/src/main/java/org/sonar/core/documentation/DefaultDocumentationLinkGenerator.java @@ -21,17 +21,28 @@ package org.sonar.core.documentation; import java.util.Optional; import javax.annotation.Nullable; +import org.sonar.api.config.Configuration; import org.sonar.api.utils.Version; +import org.sonar.core.config.CorePropertyDefinitions; import org.sonar.core.platform.SonarQubeVersion; public class DefaultDocumentationLinkGenerator implements DocumentationLinkGenerator { - private static final String DOCUMENTATION_PUBLIC_URL = "https://docs.sonarqube.org/"; + public static final String DOCUMENTATION_PUBLIC_URL = "https://docs.sonarqube.org/"; private final String documentationBaseUrl; - public DefaultDocumentationLinkGenerator(SonarQubeVersion sonarQubeVersion) { - Version version = sonarQubeVersion.get(); - this.documentationBaseUrl = DOCUMENTATION_PUBLIC_URL + version.major() + "." + version.minor(); + public DefaultDocumentationLinkGenerator(SonarQubeVersion sonarQubeVersion, Configuration configuration) { + this.documentationBaseUrl = completeUrl(configuration.get(CorePropertyDefinitions.DOCUMENTATION_BASE_URL) + .orElse(DOCUMENTATION_PUBLIC_URL), sonarQubeVersion.get()); + } + + private static String completeUrl(String baseUrl, Version version) { + String url = baseUrl; + if (!url.endsWith("/")) { + url += "/"; + } + url += version.major() + "." + version.minor(); + return url; } @Override diff --git a/sonar-core/src/test/java/org/sonar/core/documentation/DefaultDocumentationLinkGeneratorTest.java b/sonar-core/src/test/java/org/sonar/core/documentation/DefaultDocumentationLinkGeneratorTest.java index 0f0eec1ccd1..5d6cc77e5c2 100644 --- a/sonar-core/src/test/java/org/sonar/core/documentation/DefaultDocumentationLinkGeneratorTest.java +++ b/sonar-core/src/test/java/org/sonar/core/documentation/DefaultDocumentationLinkGeneratorTest.java @@ -19,16 +19,20 @@ */ package org.sonar.core.documentation; +import java.util.Optional; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Answers; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; +import org.sonar.api.config.Configuration; import org.sonar.core.platform.SonarQubeVersion; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.when; +import static org.sonar.core.config.CorePropertyDefinitions.DOCUMENTATION_BASE_URL; +import static org.sonar.core.documentation.DefaultDocumentationLinkGenerator.DOCUMENTATION_PUBLIC_URL; @RunWith(MockitoJUnitRunner.class) public class DefaultDocumentationLinkGeneratorTest { @@ -36,6 +40,8 @@ public class DefaultDocumentationLinkGeneratorTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) private SonarQubeVersion sonarQubeVersion; + @Mock + private Configuration configuration; private DefaultDocumentationLinkGenerator documentationLinkGenerator; @@ -43,20 +49,54 @@ public class DefaultDocumentationLinkGeneratorTest { public void setUp() { when(sonarQubeVersion.get().major()).thenReturn(100); when(sonarQubeVersion.get().minor()).thenReturn(1000); - documentationLinkGenerator = new DefaultDocumentationLinkGenerator(sonarQubeVersion); + when(configuration.get(DOCUMENTATION_BASE_URL)).thenReturn(Optional.empty()); + documentationLinkGenerator = new DefaultDocumentationLinkGenerator(sonarQubeVersion, configuration); } @Test public void getDocumentationLink_whenSuffixProvided_concatenatesIt() { String generatedLink = documentationLinkGenerator.getDocumentationLink(TEST_SUFFIX); - assertThat(generatedLink).isEqualTo("https://docs.sonarqube.org/100.1000/documentation/analyzing-source-code/scm-integration/"); + assertThat(generatedLink).isEqualTo(DOCUMENTATION_PUBLIC_URL + "100.1000/documentation/analyzing-source-code/scm-integration/"); } @Test public void getDocumentationLink_whenSuffixNotProvided_returnsBaseUrl() { String generatedLink = documentationLinkGenerator.getDocumentationLink(null); - assertThat(generatedLink).isEqualTo("https://docs.sonarqube.org/100.1000"); + assertThat(generatedLink).isEqualTo(DOCUMENTATION_PUBLIC_URL + "100.1000"); + } + + @Test + public void getDocumentationLink_suffixProvided_withPropertyOverride() { + String propertyValue = "https://new-url.sonarqube.org/"; + when(configuration.get(DOCUMENTATION_BASE_URL)).thenReturn(Optional.of(propertyValue)); + documentationLinkGenerator = new DefaultDocumentationLinkGenerator(sonarQubeVersion, configuration); + + String generatedLink = documentationLinkGenerator.getDocumentationLink(TEST_SUFFIX); + + assertThat(generatedLink).isEqualTo(propertyValue + "100.1000/documentation/analyzing-source-code/scm-integration/"); + } + + @Test + public void getDocumentationLink_suffixNotProvided_withPropertyOverride() { + String propertyValue = "https://new-url.sonarqube.org/"; + when(configuration.get(DOCUMENTATION_BASE_URL)).thenReturn(Optional.of(propertyValue)); + documentationLinkGenerator = new DefaultDocumentationLinkGenerator(sonarQubeVersion, configuration); + + String generatedLink = documentationLinkGenerator.getDocumentationLink(null); + + assertThat(generatedLink).isEqualTo(propertyValue + "100.1000"); + } + + @Test + public void getDocumentationLink_suffixNotProvided_withPropertyOverride_missingSlash() { + String propertyValue = "https://new-url.sonarqube.org"; + when(configuration.get(DOCUMENTATION_BASE_URL)).thenReturn(Optional.of(propertyValue)); + documentationLinkGenerator = new DefaultDocumentationLinkGenerator(sonarQubeVersion, configuration); + + String generatedLink = documentationLinkGenerator.getDocumentationLink(null); + + assertThat(generatedLink).isEqualTo(propertyValue + "/100.1000"); } }