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.core.component.ComponentKeys;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.component.SnapshotDto;
30 import org.sonar.db.component.SnapshotQuery;
31 import org.sonar.scanner.protocol.output.ScannerReport;
32 import org.sonar.server.computation.task.projectanalysis.analysis.Analysis;
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.MutableTreeRootHolder;
40 import org.sonar.server.computation.task.step.ComputationStep;
42 import static org.apache.commons.lang.StringUtils.isEmpty;
43 import static org.apache.commons.lang.StringUtils.trimToNull;
46 * Populates the {@link MutableTreeRootHolder} and {@link MutableAnalysisMetadataHolder} from the {@link BatchReportReader}
48 public class BuildComponentTreeStep implements ComputationStep {
50 private final DbClient dbClient;
51 private final BatchReportReader reportReader;
52 private final MutableTreeRootHolder treeRootHolder;
53 private final MutableAnalysisMetadataHolder analysisMetadataHolder;
55 public BuildComponentTreeStep(DbClient dbClient, BatchReportReader reportReader,
56 MutableTreeRootHolder treeRootHolder, MutableAnalysisMetadataHolder analysisMetadataHolder) {
57 this.dbClient = dbClient;
58 this.reportReader = reportReader;
59 this.treeRootHolder = treeRootHolder;
60 this.analysisMetadataHolder = analysisMetadataHolder;
64 public String getDescription() {
65 return "Build tree of components";
69 public void execute() {
70 try (DbSession dbSession = dbClient.openSession(false)) {
71 ScannerReport.Component reportProject = reportReader.readComponent(analysisMetadataHolder.getRootComponentRef());
72 ComponentKeyGenerator keyGenerator = loadKeyGenerator();
74 // root key of branch, not necessarily of project
75 String rootKey = keyGenerator.generateKey(reportProject, null);
77 // loads the UUIDs from database. If they don't exist, then generate new ones
78 ComponentUuidFactory componentUuidFactory = new ComponentUuidFactory(dbClient, dbSession, rootKey);
80 String rootUuid = componentUuidFactory.getOrCreateForKey(rootKey);
81 SnapshotDto baseAnalysis = loadBaseAnalysis(dbSession, rootUuid);
83 ComponentTreeBuilder builder = new ComponentTreeBuilder(keyGenerator,
84 componentUuidFactory::getOrCreateForKey,
85 reportReader::readComponent,
86 analysisMetadataHolder.getProject(),
88 Component project = builder.buildProject(reportProject);
90 treeRootHolder.setRoot(project);
91 analysisMetadataHolder.setBaseAnalysis(toAnalysis(baseAnalysis));
95 private ComponentKeyGenerator loadKeyGenerator() {
96 return Stream.of(analysisMetadataHolder.getBranch(), Optional.of(new DefaultKeyGenerator()))
97 // TODO pull request generator will be added here
98 .filter(Optional::isPresent)
99 .flatMap(x -> x.map(Stream::of).orElseGet(Stream::empty))
105 private SnapshotDto loadBaseAnalysis(DbSession dbSession, String rootUuid) {
106 return dbClient.snapshotDao().selectAnalysisByQuery(
109 .setComponentUuid(rootUuid)
114 private static Analysis toAnalysis(@Nullable SnapshotDto dto) {
116 return new Analysis.Builder()
118 .setUuid(dto.getUuid())
119 .setCreatedAt(dto.getCreatedAt())
125 private static class DefaultKeyGenerator implements ComponentKeyGenerator {
127 public String generateKey(ScannerReport.Component module, @Nullable ScannerReport.Component fileOrDir) {
128 String moduleKey = module.getKey();
129 if (fileOrDir == null || isEmpty(fileOrDir.getPath())) {
132 return ComponentKeys.createEffectiveKey(moduleKey, trimToNull(fileOrDir.getPath()));