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 com.google.common.collect.Iterables;
23 import javax.annotation.CheckForNull;
24 import javax.annotation.Nullable;
25 import org.sonar.core.component.ComponentKeys;
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.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.ComponentImpl;
35 import org.sonar.server.computation.task.projectanalysis.component.MutableTreeRootHolder;
36 import org.sonar.server.computation.task.projectanalysis.component.UuidFactory;
37 import org.sonar.server.computation.task.projectanalysis.analysis.Analysis;
38 import org.sonar.server.computation.task.step.ComputationStep;
40 import static com.google.common.collect.Iterables.toArray;
41 import static org.sonar.server.computation.task.projectanalysis.component.ComponentImpl.builder;
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, MutableTreeRootHolder treeRootHolder, MutableAnalysisMetadataHolder analysisMetadataHolder) {
54 this.dbClient = dbClient;
55 this.reportReader = reportReader;
56 this.treeRootHolder = treeRootHolder;
57 this.analysisMetadataHolder = analysisMetadataHolder;
61 public void execute() {
62 String branch = analysisMetadataHolder.getBranch();
63 ScannerReport.Component reportProject = reportReader.readComponent(analysisMetadataHolder.getRootComponentRef());
64 UuidFactory uuidFactory = new UuidFactory(dbClient, moduleKey(reportProject, branch));
65 Component project = new ComponentRootBuilder(reportProject, uuidFactory, branch).build();
66 treeRootHolder.setRoot(project);
67 setBaseAnalysis(project.getUuid());
70 private void setBaseAnalysis(String projectUuid) {
71 DbSession dbSession = dbClient.openSession(false);
73 SnapshotDto snapshotDto = dbClient.snapshotDao().selectAnalysisByQuery(dbSession,
75 .setComponentUuid(projectUuid)
77 analysisMetadataHolder.setBaseAnalysis(toAnalysis(snapshotDto));
79 dbClient.closeSession(dbSession);
84 private static Analysis toAnalysis(@Nullable SnapshotDto snapshotDto) {
85 return snapshotDto == null ? null : new Analysis.Builder()
86 .setId(snapshotDto.getId())
87 .setUuid(snapshotDto.getUuid())
88 .setCreatedAt(snapshotDto.getCreatedAt())
92 private class ComponentRootBuilder {
94 private final ScannerReport.Component reportProject;
96 private final UuidFactory uuidFactory;
99 private final String branch;
101 public ComponentRootBuilder(ScannerReport.Component reportProject, UuidFactory uuidFactory, @Nullable String branch) {
102 this.reportProject = reportProject;
103 this.uuidFactory = uuidFactory;
104 this.branch = branch;
107 private Component build() {
108 return buildComponent(reportProject, moduleKey(reportProject, branch));
111 private ComponentImpl buildComponent(ScannerReport.Component reportComponent, String latestModuleKey) {
112 switch (reportComponent.getType()) {
115 String moduleKey = moduleKey(reportComponent, branch);
116 return buildComponent(reportComponent, moduleKey, moduleKey);
119 return buildComponent(reportComponent, ComponentKeys.createEffectiveKey(latestModuleKey, reportComponent.getPath()), latestModuleKey);
121 throw new IllegalStateException(String.format("Unsupported component type '%s'", reportComponent.getType()));
125 private ComponentImpl buildComponent(ScannerReport.Component reportComponent, String componentKey, String latestModuleKey) {
126 return builder(reportComponent)
127 .addChildren(toArray(buildChildren(reportComponent, latestModuleKey), Component.class))
128 .setKey(componentKey)
129 .setUuid(uuidFactory.getOrCreateForKey(componentKey))
133 private Iterable<Component> buildChildren(ScannerReport.Component component, final String latestModuleKey) {
134 return Iterables.transform(
135 component.getChildRefList(),
136 componentRef -> buildComponent(reportReader.readComponent(componentRef), latestModuleKey));
140 private static String moduleKey(ScannerReport.Component reportComponent, @Nullable String branch) {
141 return ComponentKeys.createKey(reportComponent.getKey(), branch);
145 public String getDescription() {
146 return "Build tree of components";