]> source.dussan.org Git - sonarqube.git/blob
6a029818e4f6645c94c2ca0892a7898ad9d4e490
[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.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.Analysis;
32 import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
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.DefaultBranchImpl;
40 import org.sonar.server.computation.task.projectanalysis.component.MutableTreeRootHolder;
41 import org.sonar.server.computation.task.step.ComputationStep;
42
43 /**
44  * Populates the {@link MutableTreeRootHolder} and {@link MutableAnalysisMetadataHolder} from the {@link BatchReportReader}
45  */
46 public class BuildComponentTreeStep implements ComputationStep {
47
48   private final DbClient dbClient;
49   private final BatchReportReader reportReader;
50   private final MutableTreeRootHolder treeRootHolder;
51   private final MutableAnalysisMetadataHolder analysisMetadataHolder;
52
53   public BuildComponentTreeStep(DbClient dbClient, BatchReportReader reportReader,
54     MutableTreeRootHolder treeRootHolder, MutableAnalysisMetadataHolder analysisMetadataHolder) {
55     this.dbClient = dbClient;
56     this.reportReader = reportReader;
57     this.treeRootHolder = treeRootHolder;
58     this.analysisMetadataHolder = analysisMetadataHolder;
59   }
60
61   @Override
62   public String getDescription() {
63     return "Build tree of components";
64   }
65
66   @Override
67   public void execute() {
68     try (DbSession dbSession = dbClient.openSession(false)) {
69       ScannerReport.Component reportProject = reportReader.readComponent(analysisMetadataHolder.getRootComponentRef());
70       ComponentKeyGenerator keyGenerator = loadKeyGenerator();
71       ComponentKeyGenerator publicKeyGenerator = loadPublicKeyGenerator();
72
73       // root key of branch, not necessarily of project
74       String rootKey = keyGenerator.generateKey(reportProject, null);
75
76       // loads the UUIDs from database. If they don't exist, then generate new ones
77       ComponentUuidFactory componentUuidFactory = new ComponentUuidFactory(dbClient, dbSession, rootKey);
78
79       String rootUuid = componentUuidFactory.getOrCreateForKey(rootKey);
80       SnapshotDto baseAnalysis = loadBaseAnalysis(dbSession, rootUuid);
81
82       ComponentTreeBuilder builder = new ComponentTreeBuilder(keyGenerator, publicKeyGenerator,
83         componentUuidFactory::getOrCreateForKey,
84         reportReader::readComponent,
85         analysisMetadataHolder.getProject(),
86         baseAnalysis);
87       Component project = builder.buildProject(reportProject);
88
89       treeRootHolder.setRoot(project);
90       analysisMetadataHolder.setBaseAnalysis(toAnalysis(baseAnalysis));
91     }
92   }
93
94   private ComponentKeyGenerator loadKeyGenerator() {
95     return Stream.of(analysisMetadataHolder.getBranch(), Optional.of(new DefaultBranchImpl()))
96       // TODO pull request generator will be added here
97       .filter(Optional::isPresent)
98       .flatMap(x -> x.map(Stream::of).orElseGet(Stream::empty))
99       .findFirst()
100       .get();
101   }
102
103   private ComponentKeyGenerator loadPublicKeyGenerator() {
104     Optional<Branch> branch = analysisMetadataHolder.getBranch();
105     if (!branch.isPresent()) {
106       // Used for pull request
107       return new DefaultBranchImpl();
108     }
109     return branch.filter(Branch::isLegacyFeature)
110       .map(b -> new DefaultBranchImpl(b.getName().orElse(null)))
111       .orElseGet(DefaultBranchImpl::new);
112   }
113
114   @CheckForNull
115   private SnapshotDto loadBaseAnalysis(DbSession dbSession, String rootUuid) {
116     return dbClient.snapshotDao().selectAnalysisByQuery(
117       dbSession,
118       new SnapshotQuery()
119         .setComponentUuid(rootUuid)
120         .setIsLast(true));
121   }
122
123   @CheckForNull
124   private static Analysis toAnalysis(@Nullable SnapshotDto dto) {
125     if (dto != null) {
126       return new Analysis.Builder()
127         .setId(dto.getId())
128         .setUuid(dto.getUuid())
129         .setCreatedAt(dto.getCreatedAt())
130         .build();
131     }
132     return null;
133   }
134
135 }