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