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.

ValidateProjectStep.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.ce.task.projectanalysis.step;
  21. import com.google.common.base.Joiner;
  22. import java.util.ArrayList;
  23. import java.util.Date;
  24. import java.util.List;
  25. import java.util.Optional;
  26. import org.sonar.api.utils.MessageException;
  27. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
  28. import org.sonar.ce.task.projectanalysis.component.Component;
  29. import org.sonar.ce.task.projectanalysis.component.ComponentVisitor;
  30. import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
  31. import org.sonar.ce.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
  32. import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
  33. import org.sonar.ce.task.projectanalysis.component.TypeAwareVisitorAdapter;
  34. import org.sonar.ce.task.step.ComputationStep;
  35. import org.sonar.db.DbClient;
  36. import org.sonar.db.DbSession;
  37. import org.sonar.db.component.BranchDto;
  38. import org.sonar.db.component.ComponentDao;
  39. import org.sonar.db.component.ComponentDto;
  40. import org.sonar.db.component.SnapshotDto;
  41. import static com.google.common.base.Preconditions.checkState;
  42. import static java.lang.String.format;
  43. import static org.sonar.api.utils.DateUtils.formatDateTime;
  44. import static org.sonar.core.component.ComponentKeys.ALLOWED_CHARACTERS_MESSAGE;
  45. import static org.sonar.core.component.ComponentKeys.isValidProjectKey;
  46. public class ValidateProjectStep implements ComputationStep {
  47. private static final Joiner MESSAGES_JOINER = Joiner.on("\n o ");
  48. private final DbClient dbClient;
  49. private final TreeRootHolder treeRootHolder;
  50. private final AnalysisMetadataHolder analysisMetadataHolder;
  51. public ValidateProjectStep(DbClient dbClient, TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder) {
  52. this.dbClient = dbClient;
  53. this.treeRootHolder = treeRootHolder;
  54. this.analysisMetadataHolder = analysisMetadataHolder;
  55. }
  56. @Override
  57. public void execute(ComputationStep.Context context) {
  58. try (DbSession dbSession = dbClient.openSession(false)) {
  59. Component root = treeRootHolder.getRoot();
  60. String branchKey = analysisMetadataHolder.isBranch() ? analysisMetadataHolder.getBranch().getName() : null;
  61. String prKey = analysisMetadataHolder.isPullRequest() ? analysisMetadataHolder.getBranch().getPullRequestKey() : null;
  62. ValidateProjectsVisitor visitor = new ValidateProjectsVisitor(dbSession, dbClient.componentDao());
  63. new DepthTraversalTypeAwareCrawler(visitor).visit(root);
  64. if (!visitor.validationMessages.isEmpty()) {
  65. throw MessageException.of("Validation of project failed:\n o " + MESSAGES_JOINER.join(visitor.validationMessages));
  66. }
  67. }
  68. }
  69. @Override
  70. public String getDescription() {
  71. return "Validate project";
  72. }
  73. private class ValidateProjectsVisitor extends TypeAwareVisitorAdapter {
  74. private final DbSession session;
  75. private final ComponentDao componentDao;
  76. private final List<String> validationMessages = new ArrayList<>();
  77. public ValidateProjectsVisitor(DbSession session, ComponentDao componentDao) {
  78. super(CrawlerDepthLimit.PROJECT, ComponentVisitor.Order.PRE_ORDER);
  79. this.session = session;
  80. this.componentDao = componentDao;
  81. }
  82. @Override
  83. public void visitProject(Component rawProject) {
  84. String rawProjectKey = rawProject.getKey();
  85. Optional<ComponentDto> baseProjectOpt = loadBaseComponent(rawProjectKey);
  86. if (baseProjectOpt.isPresent()) {
  87. ComponentDto baseProject = baseProjectOpt.get();
  88. validateAnalysisDate(baseProject);
  89. validateProjectKey(baseProject);
  90. }
  91. }
  92. private void validateProjectKey(ComponentDto baseProject) {
  93. if (!isValidProjectKey(baseProject.getKey())) {
  94. validationMessages.add(format("The project key ‘%s’ contains invalid characters. %s. You should update the project key with the expected format.", baseProject.getKey(),
  95. ALLOWED_CHARACTERS_MESSAGE));
  96. }
  97. }
  98. private void validateAnalysisDate(ComponentDto baseProject) {
  99. Optional<SnapshotDto> snapshotDto = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(session, baseProject.uuid());
  100. long currentAnalysisDate = analysisMetadataHolder.getAnalysisDate();
  101. Long lastAnalysisDate = snapshotDto.map(SnapshotDto::getCreatedAt).orElse(null);
  102. if (lastAnalysisDate != null && currentAnalysisDate <= lastAnalysisDate) {
  103. validationMessages.add(format("Date of analysis cannot be older than the date of the last known analysis on this project. Value: \"%s\". " +
  104. "Latest analysis: \"%s\". It's only possible to rebuild the past in a chronological order.",
  105. formatDateTime(new Date(currentAnalysisDate)), formatDateTime(new Date(lastAnalysisDate))));
  106. }
  107. }
  108. private Optional<ComponentDto> loadBaseComponent(String rawComponentKey) {
  109. // Load component from key to be able to detect issue (try to analyze a module, etc.)
  110. if (analysisMetadataHolder.isBranch()) {
  111. return componentDao.selectByKeyAndBranch(session, rawComponentKey, analysisMetadataHolder.getBranch().getName());
  112. } else {
  113. return componentDao.selectByKeyAndPullRequest(session, rawComponentKey, analysisMetadataHolder.getBranch().getPullRequestKey());
  114. }
  115. }
  116. }
  117. }