3 * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.step;
22 import java.util.Optional;
23 import java.util.stream.Stream;
24 import javax.annotation.CheckForNull;
25 import javax.annotation.Nullable;
26 import org.sonar.db.DbClient;
27 import org.sonar.db.DbSession;
28 import org.sonar.db.component.SnapshotDto;
29 import org.sonar.db.component.SnapshotQuery;
30 import org.sonar.scanner.protocol.output.ScannerReport;
31 import org.sonar.server.computation.task.projectanalysis.analysis.Analysis;
32 import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
33 import org.sonar.server.computation.task.projectanalysis.analysis.MutableAnalysisMetadataHolder;
34 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
35 import org.sonar.server.computation.task.projectanalysis.component.Component;
36 import org.sonar.server.computation.task.projectanalysis.component.ComponentKeyGenerator;
37 import org.sonar.server.computation.task.projectanalysis.component.ComponentTreeBuilder;
38 import org.sonar.server.computation.task.projectanalysis.component.ComponentUuidFactory;
39 import org.sonar.server.computation.task.projectanalysis.component.DefaultBranchImpl;
40 import org.sonar.server.computation.task.projectanalysis.component.MutableTreeRootHolder;
41 import org.sonar.server.computation.task.step.ComputationStep;
44 * Populates the {@link MutableTreeRootHolder} and {@link MutableAnalysisMetadataHolder} from the {@link BatchReportReader}
46 public class BuildComponentTreeStep implements ComputationStep {
48 private final DbClient dbClient;
49 private final BatchReportReader reportReader;
50 private final MutableTreeRootHolder treeRootHolder;
51 private final MutableAnalysisMetadataHolder analysisMetadataHolder;
53 public BuildComponentTreeStep(DbClient dbClient, BatchReportReader reportReader,
54 MutableTreeRootHolder treeRootHolder, MutableAnalysisMetadataHolder analysisMetadataHolder) {
55 this.dbClient = dbClient;
56 this.reportReader = reportReader;
57 this.treeRootHolder = treeRootHolder;
58 this.analysisMetadataHolder = analysisMetadataHolder;
62 public String getDescription() {
63 return "Build tree of components";
67 public void execute() {
68 try (DbSession dbSession = dbClient.openSession(false)) {
69 ScannerReport.Component reportProject = reportReader.readComponent(analysisMetadataHolder.getRootComponentRef());
70 ComponentKeyGenerator keyGenerator = loadKeyGenerator();
71 ComponentKeyGenerator publicKeyGenerator = loadPublicKeyGenerator();
73 // root key of branch, not necessarily of project
74 String rootKey = keyGenerator.generateKey(reportProject, null);
76 // loads the UUIDs from database. If they don't exist, then generate new ones
77 ComponentUuidFactory componentUuidFactory = new ComponentUuidFactory(dbClient, dbSession, rootKey);
79 String rootUuid = componentUuidFactory.getOrCreateForKey(rootKey);
80 SnapshotDto baseAnalysis = loadBaseAnalysis(dbSession, rootUuid);
82 ComponentTreeBuilder builder = new ComponentTreeBuilder(keyGenerator, publicKeyGenerator,
83 componentUuidFactory::getOrCreateForKey,
84 reportReader::readComponent,
85 analysisMetadataHolder.getProject(),
87 Component project = builder.buildProject(reportProject);
89 treeRootHolder.setRoot(project);
90 analysisMetadataHolder.setBaseAnalysis(toAnalysis(baseAnalysis));
94 private ComponentKeyGenerator loadKeyGenerator() {
95 return Stream.of(analysisMetadataHolder.getBranch(), Optional.of(new DefaultBranchImpl()))
96 .filter(Optional::isPresent)
97 .flatMap(x -> x.map(Stream::of).orElseGet(Stream::empty))
102 private ComponentKeyGenerator loadPublicKeyGenerator() {
103 Optional<Branch> branch = analysisMetadataHolder.getBranch();
104 if (!branch.isPresent()) {
105 // Used for pull request
106 return new DefaultBranchImpl();
108 return branch.filter(Branch::isLegacyFeature)
109 .map(b -> new DefaultBranchImpl(b.getName()))
110 .orElseGet(DefaultBranchImpl::new);
114 private SnapshotDto loadBaseAnalysis(DbSession dbSession, String rootUuid) {
115 return dbClient.snapshotDao().selectAnalysisByQuery(
118 .setComponentUuid(rootUuid)
123 private static Analysis toAnalysis(@Nullable SnapshotDto dto) {
125 return new Analysis.Builder()
127 .setUuid(dto.getUuid())
128 .setCreatedAt(dto.getCreatedAt())