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 javax.annotation.CheckForNull;
23 import javax.annotation.Nullable;
24 import org.sonar.db.DbClient;
25 import org.sonar.db.DbSession;
26 import org.sonar.db.component.SnapshotDto;
27 import org.sonar.db.component.SnapshotQuery;
28 import org.sonar.scanner.protocol.output.ScannerReport;
29 import org.sonar.server.computation.task.projectanalysis.analysis.Analysis;
30 import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
31 import org.sonar.server.computation.task.projectanalysis.analysis.MutableAnalysisMetadataHolder;
32 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
33 import org.sonar.server.computation.task.projectanalysis.component.Component;
34 import org.sonar.server.computation.task.projectanalysis.component.ComponentKeyGenerator;
35 import org.sonar.server.computation.task.projectanalysis.component.ComponentTreeBuilder;
36 import org.sonar.server.computation.task.projectanalysis.component.ComponentUuidFactory;
37 import org.sonar.server.computation.task.projectanalysis.component.DefaultBranchImpl;
38 import org.sonar.server.computation.task.projectanalysis.component.MutableTreeRootHolder;
39 import org.sonar.server.computation.task.step.ComputationStep;
42 * Populates the {@link MutableTreeRootHolder} and {@link MutableAnalysisMetadataHolder} from the {@link BatchReportReader}
44 public class BuildComponentTreeStep implements ComputationStep {
46 private final DbClient dbClient;
47 private final BatchReportReader reportReader;
48 private final MutableTreeRootHolder treeRootHolder;
49 private final MutableAnalysisMetadataHolder analysisMetadataHolder;
51 public BuildComponentTreeStep(DbClient dbClient, BatchReportReader reportReader,
52 MutableTreeRootHolder treeRootHolder, MutableAnalysisMetadataHolder analysisMetadataHolder) {
53 this.dbClient = dbClient;
54 this.reportReader = reportReader;
55 this.treeRootHolder = treeRootHolder;
56 this.analysisMetadataHolder = analysisMetadataHolder;
60 public String getDescription() {
61 return "Build tree of components";
65 public void execute() {
66 try (DbSession dbSession = dbClient.openSession(false)) {
67 ScannerReport.Component reportProject = reportReader.readComponent(analysisMetadataHolder.getRootComponentRef());
68 ComponentKeyGenerator keyGenerator = loadKeyGenerator();
69 ComponentKeyGenerator publicKeyGenerator = loadPublicKeyGenerator();
71 // root key of branch, not necessarily of project
72 String rootKey = keyGenerator.generateKey(reportProject, null);
74 // loads the UUIDs from database. If they don't exist, then generate new ones
75 ComponentUuidFactory componentUuidFactory = new ComponentUuidFactory(dbClient, dbSession, rootKey);
77 String rootUuid = componentUuidFactory.getOrCreateForKey(rootKey);
78 SnapshotDto baseAnalysis = loadBaseAnalysis(dbSession, rootUuid);
80 ComponentTreeBuilder builder = new ComponentTreeBuilder(keyGenerator, publicKeyGenerator,
81 componentUuidFactory::getOrCreateForKey,
82 reportReader::readComponent,
83 analysisMetadataHolder.getProject(),
85 Component project = builder.buildProject(reportProject);
87 treeRootHolder.setRoot(project);
88 analysisMetadataHolder.setBaseAnalysis(toAnalysis(baseAnalysis));
92 private ComponentKeyGenerator loadKeyGenerator() {
93 return analysisMetadataHolder.getBranch();
96 private ComponentKeyGenerator loadPublicKeyGenerator() {
97 Branch branch = analysisMetadataHolder.getBranch();
99 // for non-legacy branches, the public key is different from the DB key.
100 if (!branch.isLegacyFeature() && !branch.isMain()) {
101 return new DefaultBranchImpl();
107 private SnapshotDto loadBaseAnalysis(DbSession dbSession, String rootUuid) {
108 return dbClient.snapshotDao().selectAnalysisByQuery(
111 .setComponentUuid(rootUuid)
116 private static Analysis toAnalysis(@Nullable SnapshotDto dto) {
118 return new Analysis.Builder()
120 .setUuid(dto.getUuid())
121 .setCreatedAt(dto.getCreatedAt())