You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProjectReactorValidator.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.scan;
  21. import com.google.common.base.Joiner;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.List;
  25. import java.util.stream.Stream;
  26. import javax.annotation.Nullable;
  27. import org.sonar.api.batch.AnalysisMode;
  28. import org.sonar.api.batch.bootstrap.ProjectDefinition;
  29. import org.sonar.api.batch.bootstrap.ProjectReactor;
  30. import org.sonar.api.utils.MessageException;
  31. import org.sonar.core.component.ComponentKeys;
  32. import org.sonar.scanner.bootstrap.GlobalConfiguration;
  33. import org.sonar.scanner.scan.branch.BranchParamsValidator;
  34. import static java.lang.String.format;
  35. import static java.util.Objects.nonNull;
  36. import static org.apache.commons.lang.StringUtils.isNotEmpty;
  37. import static org.sonar.core.config.ScannerProperties.BRANCHES_DOC_LINK;
  38. import static org.sonar.core.config.ScannerProperties.BRANCH_NAME;
  39. import static org.sonar.core.config.ScannerProperties.BRANCH_TARGET;
  40. import static org.sonar.core.config.ScannerProperties.PULL_REQUEST_BASE;
  41. import static org.sonar.core.config.ScannerProperties.PULL_REQUEST_BRANCH;
  42. import static org.sonar.core.config.ScannerProperties.PULL_REQUEST_KEY;
  43. /**
  44. * This class aims at validating project reactor
  45. * @since 3.6
  46. */
  47. public class ProjectReactorValidator {
  48. private final AnalysisMode mode;
  49. private final GlobalConfiguration settings;
  50. // null = branch plugin is not available
  51. @Nullable
  52. private final BranchParamsValidator branchParamsValidator;
  53. public ProjectReactorValidator(AnalysisMode mode, GlobalConfiguration settings, @Nullable BranchParamsValidator branchParamsValidator) {
  54. this.mode = mode;
  55. this.settings = settings;
  56. this.branchParamsValidator = branchParamsValidator;
  57. }
  58. public ProjectReactorValidator(AnalysisMode mode, GlobalConfiguration settings) {
  59. this(mode, settings, null);
  60. }
  61. public void validate(ProjectReactor reactor) {
  62. List<String> validationMessages = new ArrayList<>();
  63. for (ProjectDefinition moduleDef : reactor.getProjects()) {
  64. if (mode.isIssues()) {
  65. validateModuleIssuesMode(moduleDef, validationMessages);
  66. } else {
  67. validateModule(moduleDef, validationMessages);
  68. }
  69. }
  70. String deprecatedBranchName = reactor.getRoot().getBranch();
  71. if (isBranchFeatureAvailable()) {
  72. branchParamsValidator.validate(validationMessages, deprecatedBranchName);
  73. } else {
  74. validateBranchParamsWhenPluginAbsent(validationMessages);
  75. validatePullRequestParamsWhenPluginAbsent(validationMessages);
  76. }
  77. validateLegacyBranch(validationMessages, deprecatedBranchName);
  78. if (!validationMessages.isEmpty()) {
  79. throw MessageException.of("Validation of project reactor failed:\n o " + Joiner.on("\n o ").join(validationMessages));
  80. }
  81. }
  82. private void validateBranchParamsWhenPluginAbsent(List<String> validationMessages) {
  83. for (String param : Arrays.asList(BRANCH_NAME, BRANCH_TARGET)) {
  84. if (isNotEmpty(settings.get(param).orElse(null))) {
  85. validationMessages.add(format("To use the property \"%s\", the branch plugin is required but not installed. "
  86. + "See the documentation of branch support: %s.", param, BRANCHES_DOC_LINK));
  87. }
  88. }
  89. }
  90. private void validatePullRequestParamsWhenPluginAbsent(List<String> validationMessages) {
  91. Stream.of(PULL_REQUEST_KEY, PULL_REQUEST_BRANCH, PULL_REQUEST_BASE)
  92. .filter(param -> nonNull(settings.get(param).orElse(null)))
  93. .forEach(param -> validationMessages.add(format("To use the property \"%s\", the branch plugin is required but not installed. "
  94. + "See the documentation of branch support: %s.", param, BRANCHES_DOC_LINK)));
  95. }
  96. private static void validateModuleIssuesMode(ProjectDefinition moduleDef, List<String> validationMessages) {
  97. if (!ComponentKeys.isValidProjectKeyIssuesMode(moduleDef.getKey())) {
  98. validationMessages.add(format("\"%s\" is not a valid project or module key. "
  99. + "Allowed characters in issues mode are alphanumeric, '-', '_', '.', '/' and ':', with at least one non-digit.", moduleDef.getKey()));
  100. }
  101. }
  102. private static void validateModule(ProjectDefinition moduleDef, List<String> validationMessages) {
  103. if (!ComponentKeys.isValidProjectKey(moduleDef.getKey())) {
  104. validationMessages.add(format("\"%s\" is not a valid project or module key. It cannot be empty nor contain whitespaces.", moduleDef.getKey()));
  105. }
  106. }
  107. private static void validateLegacyBranch(List<String> validationMessages, @Nullable String branch) {
  108. if (isNotEmpty(branch) && !ComponentKeys.isValidLegacyBranch(branch)) {
  109. validationMessages.add(format("\"%s\" is not a valid branch name. "
  110. + "Allowed characters are alphanumeric, '-', '_', '.' and '/'.", branch));
  111. }
  112. }
  113. private boolean isBranchFeatureAvailable() {
  114. return branchParamsValidator != null;
  115. }
  116. }