3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.MutableAnalysisMetadataHolder;
31 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
32 import org.sonar.server.computation.task.projectanalysis.component.Component;
33 import org.sonar.server.computation.task.projectanalysis.component.ComponentRootBuilder;
34 import org.sonar.server.computation.task.projectanalysis.component.MutableTreeRootHolder;
35 import org.sonar.server.computation.task.projectanalysis.component.UuidFactory;
36 import org.sonar.server.computation.task.step.ComputationStep;
38 import static org.sonar.core.component.ComponentKeys.createKey;
41 * Populates the {@link MutableTreeRootHolder} and {@link MutableAnalysisMetadataHolder} from the {@link BatchReportReader}
43 public class BuildComponentTreeStep implements ComputationStep {
45 private final DbClient dbClient;
46 private final BatchReportReader reportReader;
47 private final MutableTreeRootHolder treeRootHolder;
48 private final MutableAnalysisMetadataHolder analysisMetadataHolder;
50 public BuildComponentTreeStep(DbClient dbClient, BatchReportReader reportReader, MutableTreeRootHolder treeRootHolder, MutableAnalysisMetadataHolder analysisMetadataHolder) {
51 this.dbClient = dbClient;
52 this.reportReader = reportReader;
53 this.treeRootHolder = treeRootHolder;
54 this.analysisMetadataHolder = analysisMetadataHolder;
58 public String getDescription() {
59 return "Build tree of components";
63 public void execute() {
64 String branch = analysisMetadataHolder.getBranch();
65 ScannerReport.Component reportProject = reportReader.readComponent(analysisMetadataHolder.getRootComponentRef());
66 String projectKey = createKey(reportProject.getKey(), branch);
67 UuidFactory uuidFactory = new UuidFactory(dbClient, projectKey);
69 try (DbSession dbSession = dbClient.openSession(false)) {
70 ComponentRootBuilder rootBuilder = new ComponentRootBuilder(branch,
71 uuidFactory::getOrCreateForKey,
72 reportReader::readComponent,
73 () -> dbClient.componentDao().selectByKey(dbSession, projectKey));
74 Component project = rootBuilder.build(reportProject, projectKey);
75 treeRootHolder.setRoot(project);
76 setBaseAnalysis(dbSession, project.getUuid());
80 private void setBaseAnalysis(DbSession dbSession, String projectUuid) {
81 SnapshotDto snapshotDto = dbClient.snapshotDao().selectAnalysisByQuery(dbSession,
83 .setComponentUuid(projectUuid)
85 analysisMetadataHolder.setBaseAnalysis(toAnalysis(snapshotDto));
89 private static Analysis toAnalysis(@Nullable SnapshotDto snapshotDto) {
90 if (snapshotDto == null) {
93 return new Analysis.Builder()
94 .setId(snapshotDto.getId())
95 .setUuid(snapshotDto.getUuid())
96 .setCreatedAt(snapshotDto.getCreatedAt())