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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.List;
  24. import java.util.stream.Stream;
  25. import javax.annotation.Nullable;
  26. import org.sonar.api.batch.bootstrap.ProjectDefinition;
  27. import org.sonar.api.batch.bootstrap.ProjectReactor;
  28. import org.sonar.api.utils.MessageException;
  29. import org.sonar.core.component.ComponentKeys;
  30. import org.sonar.scanner.bootstrap.GlobalConfiguration;
  31. import org.sonar.scanner.scan.branch.BranchParamsValidator;
  32. import static java.lang.String.format;
  33. import static java.util.Objects.nonNull;
  34. import static org.apache.commons.lang.StringUtils.isNotEmpty;
  35. import static org.sonar.core.config.ScannerProperties.BRANCHES_DOC_LINK;
  36. import static org.sonar.core.config.ScannerProperties.BRANCH_NAME;
  37. import static org.sonar.core.config.ScannerProperties.BRANCH_TARGET;
  38. import static org.sonar.core.config.ScannerProperties.PULL_REQUEST_BASE;
  39. import static org.sonar.core.config.ScannerProperties.PULL_REQUEST_BRANCH;
  40. import static org.sonar.core.config.ScannerProperties.PULL_REQUEST_KEY;
  41. /**
  42. * This class aims at validating project reactor
  43. *
  44. * @since 3.6
  45. */
  46. public class ProjectReactorValidator {
  47. private final GlobalConfiguration settings;
  48. // null = branch plugin is not available
  49. @Nullable
  50. private final BranchParamsValidator branchParamsValidator;
  51. public ProjectReactorValidator(GlobalConfiguration settings, @Nullable BranchParamsValidator branchParamsValidator) {
  52. this.settings = settings;
  53. this.branchParamsValidator = branchParamsValidator;
  54. }
  55. public ProjectReactorValidator(GlobalConfiguration settings) {
  56. this(settings, null);
  57. }
  58. public void validate(ProjectReactor reactor) {
  59. List<String> validationMessages = new ArrayList<>();
  60. for (ProjectDefinition moduleDef : reactor.getProjects()) {
  61. validateModule(moduleDef, validationMessages);
  62. }
  63. if (isBranchFeatureAvailable()) {
  64. branchParamsValidator.validate(validationMessages);
  65. } else {
  66. validateBranchParamsWhenPluginAbsent(validationMessages);
  67. validatePullRequestParamsWhenPluginAbsent(validationMessages);
  68. }
  69. if (!validationMessages.isEmpty()) {
  70. throw MessageException.of("Validation of project reactor failed:\n o " +
  71. String.join("\n o ", validationMessages));
  72. }
  73. }
  74. private void validateBranchParamsWhenPluginAbsent(List<String> validationMessages) {
  75. for (String param : Arrays.asList(BRANCH_NAME, BRANCH_TARGET)) {
  76. if (isNotEmpty(settings.get(param).orElse(null))) {
  77. validationMessages.add(format("To use the property \"%s\" and analyze branches, Developer Edition or above is required. "
  78. + "See %s for more information.", param, BRANCHES_DOC_LINK));
  79. }
  80. }
  81. }
  82. private void validatePullRequestParamsWhenPluginAbsent(List<String> validationMessages) {
  83. Stream.of(PULL_REQUEST_KEY, PULL_REQUEST_BRANCH, PULL_REQUEST_BASE)
  84. .filter(param -> nonNull(settings.get(param).orElse(null)))
  85. .forEach(param -> validationMessages.add(format("To use the property \"%s\" and analyze pull requests, Developer Edition or above is required. "
  86. + "See %s for more information.", param, BRANCHES_DOC_LINK)));
  87. }
  88. private static void validateModule(ProjectDefinition moduleDef, List<String> validationMessages) {
  89. if (!ComponentKeys.isValidProjectKey(moduleDef.getKey())) {
  90. validationMessages.add(format("\"%s\" is not a valid project or module key. It cannot be empty nor contain whitespaces.", moduleDef.getKey()));
  91. }
  92. }
  93. private boolean isBranchFeatureAvailable() {
  94. return branchParamsValidator != null;
  95. }
  96. }