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;
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;
@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) {
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)));
}
}
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) {
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;
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 {
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
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
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
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
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");
@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");