]> source.dussan.org Git - sonarqube.git/blob
c97670c2f1ddd934439339e5d71d9dab3d440648
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.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;
37
38 import static org.sonar.core.component.ComponentKeys.createKey;
39
40 /**
41  * Populates the {@link MutableTreeRootHolder} and {@link MutableAnalysisMetadataHolder} from the {@link BatchReportReader}
42  */
43 public class BuildComponentTreeStep implements ComputationStep {
44
45   private final DbClient dbClient;
46   private final BatchReportReader reportReader;
47   private final MutableTreeRootHolder treeRootHolder;
48   private final MutableAnalysisMetadataHolder analysisMetadataHolder;
49
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;
55   }
56
57   @Override
58   public String getDescription() {
59     return "Build tree of components";
60   }
61
62   @Override
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);
68
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());
77     }
78   }
79
80   private void setBaseAnalysis(DbSession dbSession, String projectUuid) {
81     SnapshotDto snapshotDto = dbClient.snapshotDao().selectAnalysisByQuery(dbSession,
82       new SnapshotQuery()
83         .setComponentUuid(projectUuid)
84         .setIsLast(true));
85     analysisMetadataHolder.setBaseAnalysis(toAnalysis(snapshotDto));
86   }
87
88   @CheckForNull
89   private static Analysis toAnalysis(@Nullable SnapshotDto snapshotDto) {
90     if (snapshotDto == null) {
91       return null;
92     }
93     return new Analysis.Builder()
94       .setId(snapshotDto.getId())
95       .setUuid(snapshotDto.getUuid())
96       .setCreatedAt(snapshotDto.getCreatedAt())
97       .build();
98   }
99
100 }