]> source.dussan.org Git - sonarqube.git/blob
11a5e840fae7c6fbafa3f822d607dfc3daa8e064
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.computation.task.projectanalysis.step;
21
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;
41
42 import static org.apache.commons.lang.StringUtils.isEmpty;
43 import static org.apache.commons.lang.StringUtils.trimToNull;
44
45 /**
46  * Populates the {@link MutableTreeRootHolder} and {@link MutableAnalysisMetadataHolder} from the {@link BatchReportReader}
47  */
48 public class BuildComponentTreeStep implements ComputationStep {
49
50   private final DbClient dbClient;
51   private final BatchReportReader reportReader;
52   private final MutableTreeRootHolder treeRootHolder;
53   private final MutableAnalysisMetadataHolder analysisMetadataHolder;
54
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;
61   }
62
63   @Override
64   public String getDescription() {
65     return "Build tree of components";
66   }
67
68   @Override
69   public void execute() {
70     try (DbSession dbSession = dbClient.openSession(false)) {
71       ScannerReport.Component reportProject = reportReader.readComponent(analysisMetadataHolder.getRootComponentRef());
72       ComponentKeyGenerator keyGenerator = loadKeyGenerator();
73
74       // root key of branch, not necessarily of project
75       String rootKey = keyGenerator.generateKey(reportProject, null);
76
77       // loads the UUIDs from database. If they don't exist, then generate new ones
78       ComponentUuidFactory componentUuidFactory = new ComponentUuidFactory(dbClient, dbSession, rootKey);
79
80       String rootUuid = componentUuidFactory.getOrCreateForKey(rootKey);
81       SnapshotDto baseAnalysis = loadBaseAnalysis(dbSession, rootUuid);
82
83       ComponentTreeBuilder builder = new ComponentTreeBuilder(keyGenerator,
84         componentUuidFactory::getOrCreateForKey,
85         reportReader::readComponent,
86         analysisMetadataHolder.getProject(),
87         baseAnalysis);
88       Component project = builder.buildProject(reportProject);
89
90       treeRootHolder.setRoot(project);
91       analysisMetadataHolder.setBaseAnalysis(toAnalysis(baseAnalysis));
92     }
93   }
94
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))
100       .findFirst()
101       .get();
102   }
103
104   @CheckForNull
105   private SnapshotDto loadBaseAnalysis(DbSession dbSession, String rootUuid) {
106     return dbClient.snapshotDao().selectAnalysisByQuery(
107       dbSession,
108       new SnapshotQuery()
109         .setComponentUuid(rootUuid)
110         .setIsLast(true));
111   }
112
113   @CheckForNull
114   private static Analysis toAnalysis(@Nullable SnapshotDto dto) {
115     if (dto != null) {
116       return new Analysis.Builder()
117         .setId(dto.getId())
118         .setUuid(dto.getUuid())
119         .setCreatedAt(dto.getCreatedAt())
120         .build();
121     }
122     return null;
123   }
124
125   private static class DefaultKeyGenerator implements ComponentKeyGenerator {
126     @Override
127     public String generateKey(ScannerReport.Component module, @Nullable ScannerReport.Component fileOrDir) {
128       String moduleKey = module.getKey();
129       if (fileOrDir == null || isEmpty(fileOrDir.getPath())) {
130         return moduleKey;
131       }
132       return ComponentKeys.createEffectiveKey(moduleKey, trimToNull(fileOrDir.getPath()));
133     }
134   }
135 }