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;
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() {
.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)
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
*/
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 {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private SonarQubeVersion sonarQubeVersion;
+ @Mock
+ private Configuration configuration;
private DefaultDocumentationLinkGenerator documentationLinkGenerator;
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");
}
}