]> source.dussan.org Git - sonarqube.git/blob
fffb6cb5db0f784c4f9b13515bbddaeb2e847120
[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 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;
40
41 /**
42  * Populates the {@link MutableTreeRootHolder} and {@link MutableAnalysisMetadataHolder} from the {@link BatchReportReader}
43  */
44 public class BuildComponentTreeStep implements ComputationStep {
45
46   private final DbClient dbClient;
47   private final BatchReportReader reportReader;
48   private final MutableTreeRootHolder treeRootHolder;
49   private final MutableAnalysisMetadataHolder analysisMetadataHolder;
50
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;
57   }
58
59   @Override
60   public String getDescription() {
61     return "Build tree of components";
62   }
63
64   @Override
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();
70
71       // root key of branch, not necessarily of project
72       String rootKey = keyGenerator.generateKey(reportProject, null);
73
74       // loads the UUIDs from database. If they don't exist, then generate new ones
75       ComponentUuidFactory componentUuidFactory = new ComponentUuidFactory(dbClient, dbSession, rootKey);
76
77       String rootUuid = componentUuidFactory.getOrCreateForKey(rootKey);
78       SnapshotDto baseAnalysis = loadBaseAnalysis(dbSession, rootUuid);
79
80       ComponentTreeBuilder builder = new ComponentTreeBuilder(keyGenerator, publicKeyGenerator,
81         componentUuidFactory::getOrCreateForKey,
82         reportReader::readComponent,
83         analysisMetadataHolder.getProject(),
84         baseAnalysis);
85       Component project = builder.buildProject(reportProject);
86
87       treeRootHolder.setRoot(project);
88       analysisMetadataHolder.setBaseAnalysis(toAnalysis(baseAnalysis));
89     }
90   }
91
92   private ComponentKeyGenerator loadKeyGenerator() {
93     return analysisMetadataHolder.getBranch();
94   }
95
96   private ComponentKeyGenerator loadPublicKeyGenerator() {
97     Branch branch = analysisMetadataHolder.getBranch();
98
99     // for non-legacy branches, the public key is different from the DB key.
100     if (!branch.isLegacyFeature() && !branch.isMain()) {
101       return new DefaultBranchImpl();
102     }
103     return branch;
104   }
105
106   @CheckForNull
107   private SnapshotDto loadBaseAnalysis(DbSession dbSession, String rootUuid) {
108     return dbClient.snapshotDao().selectAnalysisByQuery(
109       dbSession,
110       new SnapshotQuery()
111         .setComponentUuid(rootUuid)
112         .setIsLast(true));
113   }
114
115   @CheckForNull
116   private static Analysis toAnalysis(@Nullable SnapshotDto dto) {
117     if (dto != null) {
118       return new Analysis.Builder()
119         .setId(dto.getId())
120         .setUuid(dto.getUuid())
121         .setCreatedAt(dto.getCreatedAt())
122         .build();
123     }
124     return null;
125   }
126
127 }