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 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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 java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.List;
  24. import java.util.Set;
  25. import java.util.stream.Stream;
  26. import javax.annotation.Nullable;
  27. import org.sonar.api.batch.bootstrap.ProjectDefinition;
  28. import org.sonar.api.batch.bootstrap.ProjectReactor;
  29. import org.sonar.api.utils.MessageException;
  30. import org.sonar.core.component.ComponentKeys;
  31. import org.sonar.scanner.bootstrap.GlobalConfiguration;
  32. import org.sonar.scanner.scan.branch.BranchParamsValidator;
  33. import org.springframework.beans.factory.annotation.Autowired;
  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.component.ComponentKeys.ALLOWED_CHARACTERS_MESSAGE;
  38. import static org.sonar.core.config.ScannerProperties.BRANCHES_DOC_LINK;
  39. import static org.sonar.core.config.ScannerProperties.BRANCH_NAME;
  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. *
  46. * @since 3.6
  47. */
  48. public class ProjectReactorValidator {
  49. private final GlobalConfiguration settings;
  50. // null = branch plugin is not available
  51. @Nullable
  52. private final BranchParamsValidator branchParamsValidator;
  53. @Autowired(required = false)
  54. public ProjectReactorValidator(GlobalConfiguration settings, @Nullable BranchParamsValidator branchParamsValidator) {
  55. this.settings = settings;
  56. this.branchParamsValidator = branchParamsValidator;
  57. }
  58. @Autowired(required = false)
  59. public ProjectReactorValidator(GlobalConfiguration settings) {
  60. this(settings, null);
  61. }
  62. public void validate(ProjectReactor reactor) {
  63. List<String> validationMessages = new ArrayList<>();
  64. for (ProjectDefinition moduleDef : reactor.getProjects()) {
  65. validateModule(moduleDef, validationMessages);
  66. }
  67. if (isBranchFeatureAvailable()) {
  68. branchParamsValidator.validate(validationMessages);
  69. } else {
  70. validateBranchParamsWhenPluginAbsent(validationMessages);
  71. validatePullRequestParamsWhenPluginAbsent(validationMessages);
  72. }
  73. if (!validationMessages.isEmpty()) {
  74. throw MessageException.of("Validation of project failed:\n o " +
  75. String.join("\n o ", validationMessages));
  76. }
  77. }
  78. private void validateBranchParamsWhenPluginAbsent(List<String> validationMessages) {
  79. if (isNotEmpty(settings.get(BRANCH_NAME).orElse(null))) {
  80. validationMessages.add(format("To use the property \"%s\" and analyze branches, Developer Edition or above is required. "
  81. + "See %s for more information.", BRANCH_NAME, BRANCHES_DOC_LINK));
  82. }
  83. }
  84. private void validatePullRequestParamsWhenPluginAbsent(List<String> validationMessages) {
  85. Stream.of(PULL_REQUEST_KEY, PULL_REQUEST_BRANCH, PULL_REQUEST_BASE)
  86. .filter(param -> nonNull(settings.get(param).orElse(null)))
  87. .forEach(param -> validationMessages.add(format("To use the property \"%s\" and analyze pull requests, Developer Edition or above is required. "
  88. + "See %s for more information.", param, BRANCHES_DOC_LINK)));
  89. }
  90. private static void validateModule(ProjectDefinition projectDefinition, List<String> validationMessages) {
  91. if (!ComponentKeys.isValidProjectKey(projectDefinition.getKey())) {
  92. validationMessages.add(format("\"%s\" is not a valid project key. %s.", projectDefinition.getKey(), ALLOWED_CHARACTERS_MESSAGE));
  93. }
  94. Set<String> forbiddenNamePhrases = Set.of(":BRANCH:", ":PULLREQUEST:");
  95. if (forbiddenNamePhrases.stream().anyMatch(projectDefinition.getKey()::contains)) {
  96. validationMessages.add(format("\"%s\" is not a valid project key. Project key must not contain following phrases [%s]", projectDefinition.getKey(),
  97. String.join(", ", forbiddenNamePhrases)));
  98. }
  99. }
  100. private boolean isBranchFeatureAvailable() {
  101. return branchParamsValidator != null;
  102. }
  103. }