]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-18213 - Make branches documentation link version sensitive in scanner logs
authorAntoine Vinot <antoine.vinot@sonarsource.com>
Wed, 31 May 2023 12:08:29 +0000 (14:08 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 31 May 2023 20:02:50 +0000 (20:02 +0000)
sonar-core/src/main/java/org/sonar/core/config/ScannerProperties.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorValidator.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java

index b6755a22fb7f5eefac42549678dfb1e6ca287204..0eccffea26c6e909f8962e9cf100f845ad04145a 100644 (file)
@@ -30,7 +30,7 @@ import static org.sonar.api.PropertyType.INTEGER;
 
 public class ScannerProperties {
 
-  public static final String BRANCHES_DOC_LINK = "https://docs.sonarqube.org/latest/analyzing-source-code/branches/branch-analysis/";
+  public static final String BRANCHES_DOC_LINK_SUFFIX = "/analyzing-source-code/branches/branch-analysis/";
 
   public static final String BRANCH_NAME = "sonar.branch.name";
 
index b3d07425332845ec5f0d1015efc06c0b9c20866b..f9b24a6054374edca87ae614b9904d39eccd5629 100644 (file)
@@ -27,6 +27,7 @@ import org.sonar.api.batch.bootstrap.ProjectDefinition;
 import org.sonar.api.batch.bootstrap.ProjectReactor;
 import org.sonar.api.utils.MessageException;
 import org.sonar.core.component.ComponentKeys;
+import org.sonar.core.documentation.DocumentationLinkGenerator;
 import org.sonar.scanner.bootstrap.GlobalConfiguration;
 import org.sonar.scanner.scan.branch.BranchParamsValidator;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -35,7 +36,7 @@ import static java.lang.String.format;
 import static java.util.Objects.nonNull;
 import static org.apache.commons.lang.StringUtils.isNotEmpty;
 import static org.sonar.core.component.ComponentKeys.ALLOWED_CHARACTERS_MESSAGE;
-import static org.sonar.core.config.ScannerProperties.BRANCHES_DOC_LINK;
+import static org.sonar.core.config.ScannerProperties.BRANCHES_DOC_LINK_SUFFIX;
 import static org.sonar.core.config.ScannerProperties.BRANCH_NAME;
 import static org.sonar.core.config.ScannerProperties.PULL_REQUEST_BASE;
 import static org.sonar.core.config.ScannerProperties.PULL_REQUEST_BRANCH;
@@ -53,15 +54,18 @@ public class ProjectReactorValidator {
   @Nullable
   private final BranchParamsValidator branchParamsValidator;
 
+  private final DocumentationLinkGenerator documentationLinkGenerator;
+
   @Autowired(required = false)
-  public ProjectReactorValidator(GlobalConfiguration settings, @Nullable BranchParamsValidator branchParamsValidator) {
+  public ProjectReactorValidator(GlobalConfiguration settings, @Nullable BranchParamsValidator branchParamsValidator, DocumentationLinkGenerator documentationLinkGenerator) {
     this.settings = settings;
     this.branchParamsValidator = branchParamsValidator;
+    this.documentationLinkGenerator = documentationLinkGenerator;
   }
 
   @Autowired(required = false)
-  public ProjectReactorValidator(GlobalConfiguration settings) {
-    this(settings, null);
+  public ProjectReactorValidator(GlobalConfiguration settings, DocumentationLinkGenerator documentationLinkGenerator) {
+    this(settings, null, documentationLinkGenerator);
   }
 
   public void validate(ProjectReactor reactor) {
@@ -87,7 +91,7 @@ public class ProjectReactorValidator {
   private void validateBranchParamsWhenPluginAbsent(List<String> validationMessages) {
     if (isNotEmpty(settings.get(BRANCH_NAME).orElse(null))) {
       validationMessages.add(format("To use the property \"%s\" and analyze branches, Developer Edition or above is required. "
-        + "See %s for more information.", BRANCH_NAME, BRANCHES_DOC_LINK));
+        + "See %s for more information.", BRANCH_NAME, documentationLinkGenerator.getDocumentationLink(BRANCHES_DOC_LINK_SUFFIX)));
     }
   }
 
@@ -95,7 +99,7 @@ public class ProjectReactorValidator {
     Stream.of(PULL_REQUEST_KEY, PULL_REQUEST_BRANCH, PULL_REQUEST_BASE)
       .filter(param -> nonNull(settings.get(param).orElse(null)))
       .forEach(param -> validationMessages.add(format("To use the property \"%s\" and analyze pull requests, Developer Edition or above is required. "
-        + "See %s for more information.", param, BRANCHES_DOC_LINK)));
+        + "See %s for more information.", param, documentationLinkGenerator.getDocumentationLink(BRANCHES_DOC_LINK_SUFFIX))));
   }
 
   private static void validateModule(ProjectDefinition projectDefinition, List<String> validationMessages) {
index 9fe39d63f1b59c2252ef1ef876c8c9bee5751aa0..9ec256fa5da89b249b906e86767b31b675ad2ac2 100644 (file)
@@ -25,6 +25,7 @@ import com.tngtech.java.junit.dataprovider.UseDataProvider;
 import java.util.Arrays;
 import java.util.Optional;
 import java.util.function.Consumer;
+import javax.annotation.Nullable;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -35,14 +36,17 @@ import org.sonar.api.batch.bootstrap.ProjectReactor;
 import org.sonar.api.utils.MessageException;
 import org.sonar.api.testfixtures.log.LogTester;
 import org.sonar.core.config.ScannerProperties;
+import org.sonar.core.documentation.DefaultDocumentationLinkGenerator;
 import org.sonar.scanner.ProjectInfo;
 import org.sonar.scanner.bootstrap.GlobalConfiguration;
 
+import static java.lang.String.format;
 import static org.apache.commons.lang.RandomStringUtils.randomAscii;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
+import static org.sonar.core.config.ScannerProperties.BRANCHES_DOC_LINK_SUFFIX;
 
 @RunWith(DataProviderRunner.class)
 public class ProjectReactorValidatorTest {
@@ -52,11 +56,14 @@ public class ProjectReactorValidatorTest {
 
   private final GlobalConfiguration settings = mock(GlobalConfiguration.class);
   private final ProjectInfo projectInfo = mock(ProjectInfo.class);
-  private final ProjectReactorValidator underTest = new ProjectReactorValidator(settings);
+  private final DefaultDocumentationLinkGenerator defaultDocumentationLinkGenerator = mock(DefaultDocumentationLinkGenerator.class);
+  private final ProjectReactorValidator underTest = new ProjectReactorValidator(settings, defaultDocumentationLinkGenerator);
+  private static final String LINK_TO_DOC = "link_to_documentation";
 
   @Before
   public void prepare() {
     when(settings.get(anyString())).thenReturn(Optional.empty());
+    when(defaultDocumentationLinkGenerator.getDocumentationLink(BRANCHES_DOC_LINK_SUFFIX)).thenReturn(LINK_TO_DOC);
   }
 
   @Test
@@ -125,7 +132,8 @@ public class ProjectReactorValidatorTest {
 
     assertThatThrownBy(() -> underTest.validate(reactor))
       .isInstanceOf(MessageException.class)
-      .hasMessageContaining("To use the property \"sonar.branch.name\" and analyze branches, Developer Edition or above is required");
+      .hasMessageContaining(format("To use the property \"sonar.branch.name\" and analyze branches, Developer Edition or above is required. See %s for more information.",
+        LINK_TO_DOC));
   }
 
   @Test
@@ -137,7 +145,8 @@ public class ProjectReactorValidatorTest {
 
     assertThatThrownBy(() -> underTest.validate(reactor))
       .isInstanceOf(MessageException.class)
-      .hasMessageContaining("To use the property \"sonar.pullrequest.key\" and analyze pull requests, Developer Edition or above is required");
+      .hasMessageContaining(format("To use the property \"sonar.pullrequest.key\" and analyze pull requests, Developer Edition or above is required. See %s for more information.",
+        LINK_TO_DOC));
   }
 
   @Test
@@ -149,7 +158,8 @@ public class ProjectReactorValidatorTest {
 
     assertThatThrownBy(() -> underTest.validate(reactor))
       .isInstanceOf(MessageException.class)
-      .hasMessageContaining("To use the property \"sonar.pullrequest.branch\" and analyze pull requests, Developer Edition or above is required");
+      .hasMessageContaining(format("To use the property \"sonar.pullrequest.branch\" and analyze pull requests, Developer Edition or above is required. See %s for more information.",
+        LINK_TO_DOC));
   }
 
   @Test
@@ -161,12 +171,13 @@ public class ProjectReactorValidatorTest {
 
     assertThatThrownBy(() -> underTest.validate(reactor))
       .isInstanceOf(MessageException.class)
-      .hasMessageContaining("To use the property \"sonar.pullrequest.base\" and analyze pull requests, Developer Edition or above is required");
+      .hasMessageContaining(format("To use the property \"sonar.pullrequest.base\" and analyze pull requests, Developer Edition or above is required. See %s for more information.",
+        LINK_TO_DOC));
   }
 
   @Test
   @UseDataProvider("validVersions")
-  public void not_fail_with_valid_version(String validVersion) {
+  public void not_fail_with_valid_version(@Nullable String validVersion) {
     when(projectInfo.getProjectVersion()).thenReturn(Optional.ofNullable(validVersion));
 
     ProjectReactor projectReactor = createProjectReactor("foo");
@@ -185,7 +196,7 @@ public class ProjectReactorValidatorTest {
 
   @Test
   @UseDataProvider("validBuildStrings")
-  public void not_fail_with_valid_buildString(String validBuildString) {
+  public void not_fail_with_valid_buildString(@Nullable String validBuildString) {
     when(projectInfo.getBuildString()).thenReturn(Optional.ofNullable(validBuildString));
 
     ProjectReactor projectReactor = createProjectReactor("foo");