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.

BuildComponentTreeStep.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.ce.task.projectanalysis.step;
  21. import java.util.Optional;
  22. import java.util.function.Function;
  23. import javax.annotation.Nullable;
  24. import org.sonar.ce.task.projectanalysis.analysis.Analysis;
  25. import org.sonar.ce.task.projectanalysis.analysis.Branch;
  26. import org.sonar.ce.task.projectanalysis.analysis.MutableAnalysisMetadataHolder;
  27. import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
  28. import org.sonar.ce.task.projectanalysis.component.Component;
  29. import org.sonar.ce.task.projectanalysis.component.ComponentKeyGenerator;
  30. import org.sonar.ce.task.projectanalysis.component.ComponentTreeBuilder;
  31. import org.sonar.ce.task.projectanalysis.component.ComponentUuidFactoryWithMigration;
  32. import org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl;
  33. import org.sonar.ce.task.projectanalysis.component.MutableTreeRootHolder;
  34. import org.sonar.ce.task.projectanalysis.component.ProjectAttributes;
  35. import org.sonar.ce.task.projectanalysis.component.ReportModulesPath;
  36. import org.sonar.ce.task.step.ComputationStep;
  37. import org.sonar.db.DbClient;
  38. import org.sonar.db.DbSession;
  39. import org.sonar.db.component.SnapshotDto;
  40. import org.sonar.scanner.protocol.output.ScannerReport;
  41. import static com.google.common.base.MoreObjects.firstNonNull;
  42. import static org.apache.commons.lang.StringUtils.trimToNull;
  43. /**
  44. * Populates the {@link MutableTreeRootHolder} and {@link MutableAnalysisMetadataHolder} from the {@link BatchReportReader}
  45. */
  46. public class BuildComponentTreeStep implements ComputationStep {
  47. private static final String DEFAULT_PROJECT_VERSION = "not provided";
  48. private final DbClient dbClient;
  49. private final BatchReportReader reportReader;
  50. private final MutableTreeRootHolder treeRootHolder;
  51. private final MutableAnalysisMetadataHolder analysisMetadataHolder;
  52. private final ReportModulesPath reportModulesPath;
  53. public BuildComponentTreeStep(DbClient dbClient, BatchReportReader reportReader, MutableTreeRootHolder treeRootHolder,
  54. MutableAnalysisMetadataHolder analysisMetadataHolder, ReportModulesPath reportModulesPath) {
  55. this.dbClient = dbClient;
  56. this.reportReader = reportReader;
  57. this.treeRootHolder = treeRootHolder;
  58. this.analysisMetadataHolder = analysisMetadataHolder;
  59. this.reportModulesPath = reportModulesPath;
  60. }
  61. @Override
  62. public String getDescription() {
  63. return "Build tree of components";
  64. }
  65. @Override
  66. public void execute(ComputationStep.Context context) {
  67. try (DbSession dbSession = dbClient.openSession(false)) {
  68. ScannerReport.Component reportProject = reportReader.readComponent(analysisMetadataHolder.getRootComponentRef());
  69. ComponentKeyGenerator keyGenerator = loadKeyGenerator();
  70. ComponentKeyGenerator publicKeyGenerator = loadPublicKeyGenerator();
  71. ScannerReport.Metadata metadata = reportReader.readMetadata();
  72. // root key of branch, not necessarily of project
  73. String rootKey = keyGenerator.generateKey(reportProject.getKey(), null);
  74. Function<String, String> pathToKey = path -> keyGenerator.generateKey(reportProject.getKey(), path);
  75. // loads the UUIDs from database. If they don't exist, then generate new ones
  76. ComponentUuidFactoryWithMigration componentUuidFactoryWithMigration = new ComponentUuidFactoryWithMigration(dbClient, dbSession, rootKey, pathToKey, reportModulesPath.get());
  77. String rootUuid = componentUuidFactoryWithMigration.getOrCreateForKey(rootKey);
  78. Optional<SnapshotDto> baseAnalysis = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, rootUuid);
  79. ComponentTreeBuilder builder = new ComponentTreeBuilder(keyGenerator, publicKeyGenerator,
  80. componentUuidFactoryWithMigration::getOrCreateForKey,
  81. reportReader::readComponent,
  82. analysisMetadataHolder.getProject(),
  83. analysisMetadataHolder.getBranch(),
  84. createProjectAttributes(metadata, baseAnalysis.orElse(null)));
  85. String relativePathFromScmRoot = metadata.getRelativePathFromScmRoot();
  86. Component reportTreeRoot = builder.buildProject(reportProject, relativePathFromScmRoot);
  87. if (analysisMetadataHolder.isSLBorPR()) {
  88. Component changedComponentTreeRoot = builder.buildChangedComponentTreeRoot(reportTreeRoot);
  89. treeRootHolder.setRoots(changedComponentTreeRoot, reportTreeRoot);
  90. } else {
  91. treeRootHolder.setRoots(reportTreeRoot, reportTreeRoot);
  92. }
  93. analysisMetadataHolder.setBaseAnalysis(baseAnalysis.map(BuildComponentTreeStep::toAnalysis).orElse(null));
  94. context.getStatistics().add("components", treeRootHolder.getSize());
  95. }
  96. }
  97. private static ProjectAttributes createProjectAttributes(ScannerReport.Metadata metadata, @Nullable SnapshotDto baseAnalysis) {
  98. String projectVersion = computeProjectVersion(trimToNull(metadata.getProjectVersion()), baseAnalysis);
  99. String buildString = trimToNull(metadata.getBuildString());
  100. String scmRevisionId = trimToNull(metadata.getScmRevisionId());
  101. return new ProjectAttributes(projectVersion, buildString, scmRevisionId);
  102. }
  103. private static String computeProjectVersion(@Nullable String projectVersion, @Nullable SnapshotDto baseAnalysis) {
  104. if (projectVersion != null) {
  105. return projectVersion;
  106. }
  107. if (baseAnalysis != null) {
  108. return firstNonNull(baseAnalysis.getProjectVersion(), DEFAULT_PROJECT_VERSION);
  109. }
  110. return DEFAULT_PROJECT_VERSION;
  111. }
  112. private ComponentKeyGenerator loadKeyGenerator() {
  113. return analysisMetadataHolder.getBranch();
  114. }
  115. private ComponentKeyGenerator loadPublicKeyGenerator() {
  116. Branch branch = analysisMetadataHolder.getBranch();
  117. // for non-legacy branches, the public key is different from the DB key.
  118. if (!branch.isMain()) {
  119. return new DefaultBranchImpl();
  120. }
  121. return branch;
  122. }
  123. private static Analysis toAnalysis(SnapshotDto dto) {
  124. return new Analysis.Builder()
  125. .setId(dto.getId())
  126. .setUuid(dto.getUuid())
  127. .setCreatedAt(dto.getCreatedAt())
  128. .build();
  129. }
  130. }