From: Antoine Vinot Date: Wed, 31 May 2023 12:08:29 +0000 (+0200) Subject: SONAR-18213 - Make branches documentation link version sensitive in scanner logs X-Git-Tag: 10.1.0.73491~186 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=977d49ebdcce0b576bd4f08fe93853c2e6a73721;p=sonarqube.git SONAR-18213 - Make branches documentation link version sensitive in scanner logs --- diff --git a/sonar-core/src/main/java/org/sonar/core/config/ScannerProperties.java b/sonar-core/src/main/java/org/sonar/core/config/ScannerProperties.java index b6755a22fb7..0eccffea26c 100644 --- a/sonar-core/src/main/java/org/sonar/core/config/ScannerProperties.java +++ b/sonar-core/src/main/java/org/sonar/core/config/ScannerProperties.java @@ -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"; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorValidator.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorValidator.java index b3d07425332..f9b24a60543 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorValidator.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorValidator.java @@ -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 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 validationMessages) { diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java index 9fe39d63f1b..9ec256fa5da 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java @@ -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");