3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.step;
22 import java.util.Optional;
23 import java.util.function.Function;
24 import javax.annotation.Nullable;
25 import org.sonar.ce.task.projectanalysis.analysis.Analysis;
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.MutableTreeRootHolder;
33 import org.sonar.ce.task.projectanalysis.component.ProjectAttributes;
34 import org.sonar.ce.task.projectanalysis.component.ReportModulesPath;
35 import org.sonar.ce.task.step.ComputationStep;
36 import org.sonar.db.DbClient;
37 import org.sonar.db.DbSession;
38 import org.sonar.db.component.SnapshotDto;
39 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;
45 * Populates the {@link MutableTreeRootHolder} and {@link MutableAnalysisMetadataHolder} from the {@link BatchReportReader}
47 public class BuildComponentTreeStep implements ComputationStep {
49 private static final String DEFAULT_PROJECT_VERSION = "not provided";
51 private final DbClient dbClient;
52 private final BatchReportReader reportReader;
53 private final MutableTreeRootHolder treeRootHolder;
54 private final MutableAnalysisMetadataHolder analysisMetadataHolder;
55 private final ReportModulesPath reportModulesPath;
57 public BuildComponentTreeStep(DbClient dbClient, BatchReportReader reportReader, MutableTreeRootHolder treeRootHolder,
58 MutableAnalysisMetadataHolder analysisMetadataHolder, ReportModulesPath reportModulesPath) {
59 this.dbClient = dbClient;
60 this.reportReader = reportReader;
61 this.treeRootHolder = treeRootHolder;
62 this.analysisMetadataHolder = analysisMetadataHolder;
63 this.reportModulesPath = reportModulesPath;
67 public String getDescription() {
68 return "Build tree of components";
72 public void execute(ComputationStep.Context context) {
73 try (DbSession dbSession = dbClient.openSession(false)) {
74 ScannerReport.Component reportProject = reportReader.readComponent(analysisMetadataHolder.getRootComponentRef());
75 ComponentKeyGenerator keyGenerator = loadKeyGenerator();
76 ScannerReport.Metadata metadata = reportReader.readMetadata();
78 // root key of branch, not necessarily of project
79 String rootKey = keyGenerator.generateKey(reportProject.getKey(), null);
80 Function<String, String> pathToKey = path -> keyGenerator.generateKey(reportProject.getKey(), path);
81 // loads the UUIDs from database. If they don't exist, then generate new ones
82 ComponentUuidFactoryWithMigration componentUuidFactoryWithMigration = new ComponentUuidFactoryWithMigration(dbClient, dbSession, rootKey, pathToKey, reportModulesPath.get());
84 String rootUuid = componentUuidFactoryWithMigration.getOrCreateForKey(rootKey);
85 Optional<SnapshotDto> baseAnalysis = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, rootUuid);
87 ComponentTreeBuilder builder = new ComponentTreeBuilder(keyGenerator,
88 componentUuidFactoryWithMigration::getOrCreateForKey,
89 reportReader::readComponent,
90 analysisMetadataHolder.getProject(),
91 analysisMetadataHolder.getBranch(),
92 createProjectAttributes(metadata, baseAnalysis.orElse(null)));
93 String relativePathFromScmRoot = metadata.getRelativePathFromScmRoot();
95 Component reportTreeRoot = builder.buildProject(reportProject, relativePathFromScmRoot);
97 if (analysisMetadataHolder.isPullRequest()) {
98 Component changedComponentTreeRoot = builder.buildChangedComponentTreeRoot(reportTreeRoot);
99 treeRootHolder.setRoots(changedComponentTreeRoot, reportTreeRoot);
101 treeRootHolder.setRoots(reportTreeRoot, reportTreeRoot);
104 analysisMetadataHolder.setBaseAnalysis(baseAnalysis.map(BuildComponentTreeStep::toAnalysis).orElse(null));
106 context.getStatistics().add("components", treeRootHolder.getSize());
110 private static ProjectAttributes createProjectAttributes(ScannerReport.Metadata metadata, @Nullable SnapshotDto baseAnalysis) {
111 String projectVersion = computeProjectVersion(trimToNull(metadata.getProjectVersion()), baseAnalysis);
112 String buildString = trimToNull(metadata.getBuildString());
113 String scmRevisionId = trimToNull(metadata.getScmRevisionId());
114 return new ProjectAttributes(projectVersion, buildString, scmRevisionId);
117 private static String computeProjectVersion(@Nullable String projectVersion, @Nullable SnapshotDto baseAnalysis) {
118 if (projectVersion != null) {
119 return projectVersion;
121 if (baseAnalysis != null) {
122 return firstNonNull(baseAnalysis.getProjectVersion(), DEFAULT_PROJECT_VERSION);
124 return DEFAULT_PROJECT_VERSION;
127 private ComponentKeyGenerator loadKeyGenerator() {
128 return analysisMetadataHolder.getBranch();
131 private static Analysis toAnalysis(SnapshotDto dto) {
132 return new Analysis.Builder()
133 .setUuid(dto.getUuid())
134 .setCreatedAt(dto.getCreatedAt())