]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-18215 Add property to override the documentation base url
authorEric Giffon <eric.giffon@sonarsource.com>
Wed, 8 Feb 2023 14:16:21 +0000 (15:16 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 15 Feb 2023 20:03:07 +0000 (20:03 +0000)
sonar-core/src/main/java/org/sonar/core/config/CorePropertyDefinitions.java
sonar-core/src/main/java/org/sonar/core/documentation/DefaultDocumentationLinkGenerator.java
sonar-core/src/test/java/org/sonar/core/documentation/DefaultDocumentationLinkGeneratorTest.java

index cf2e851af1ee10338b82897028d894a614fddc6e..360e34ffee6d96f6ebefc7063992a1d522bfa252 100644 (file)
@@ -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 <i>https://docs.sonarqube.org/</i>")
+        .defaultValue(DefaultDocumentationLinkGenerator.DOCUMENTATION_PUBLIC_URL)
+        .hidden()
+        .type(STRING)
+        .build(),
 
       // WEB LOOK&FEEL
       PropertyDefinition.builder(WebConstants.SONAR_LF_LOGO_URL)
index 470a7b24f8bbd3b870e1f61215ccfbd4622f1f5f..6b447820b0d6ec4a0797ec0210632a00ff2356c8 100644 (file)
@@ -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
index 0f0eec1ccd1f16da735751b79f6f474f372e36fb..5d6cc77e5c2ae0c73f46198bf874e1755f2c6408 100644 (file)
  */
 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");
   }
 }