3 * Copyright (C) 2009-2019 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.isSLBorPR()) {
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 = trimToNull(metadata.getProjectVersion());
115 String codePeriodVersion = computeCodePeriodVersion(metadata.getCodePeriodVersion(), projectVersion, baseAnalysis);
116 String buildString = trimToNull(metadata.getBuildString());
117 return new ProjectAttributes(projectVersion, codePeriodVersion, buildString);
120 private static String computeCodePeriodVersion(String rawCodePeriodVersion, @Nullable String projectVersion, @Nullable SnapshotDto baseAnalysis) {
121 String codePeriodVersion = trimToNull(rawCodePeriodVersion);
122 if (codePeriodVersion != null) {
123 return codePeriodVersion;
125 // support case (legacy but not forbidden) where only projectVersion is set
126 if (projectVersion != null) {
127 return projectVersion;
129 if (baseAnalysis != null) {
130 return firstNonNull(baseAnalysis.getCodePeriodVersion(), DEFAULT_PROJECT_VERSION);
132 return DEFAULT_PROJECT_VERSION;
135 private ComponentKeyGenerator loadKeyGenerator() {
136 return analysisMetadataHolder.getBranch();
139 private ComponentKeyGenerator loadPublicKeyGenerator() {
140 Branch branch = analysisMetadataHolder.getBranch();
142 // for non-legacy branches, the public key is different from the DB key.
143 if (!branch.isLegacyFeature() && !branch.isMain()) {
144 return new DefaultBranchImpl();
149 private static Analysis toAnalysis(SnapshotDto dto) {
150 return new Analysis.Builder()
152 .setUuid(dto.getUuid())
153 .setCreatedAt(dto.getCreatedAt())