]> source.dussan.org Git - sonarqube.git/blob
4c5b96896aa9fe4d40418f41f0075934ee66eb9f
[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       .filter(Optional::isPresent)
97       .flatMap(x -> x.map(Stream::of).orElseGet(Stream::empty))
98       .findFirst()
99       .get();
100   }
101
102   private ComponentKeyGenerator loadPublicKeyGenerator() {
103     Optional<Branch> branch = analysisMetadataHolder.getBranch();
104     if (!branch.isPresent()) {
105       // Used for pull request
106       return new DefaultBranchImpl();
107     }
108     return branch.filter(Branch::isLegacyFeature)
109       .map(b -> new DefaultBranchImpl(b.getName()))
110       .orElseGet(DefaultBranchImpl::new);
111   }
112
113   @CheckForNull
114   private SnapshotDto loadBaseAnalysis(DbSession dbSession, String rootUuid) {
115     return dbClient.snapshotDao().selectAnalysisByQuery(
116       dbSession,
117       new SnapshotQuery()
118         .setComponentUuid(rootUuid)
119         .setIsLast(true));
120   }
121
122   @CheckForNull
123   private static Analysis toAnalysis(@Nullable SnapshotDto dto) {
124     if (dto != null) {
125       return new Analysis.Builder()
126         .setId(dto.getId())
127         .setUuid(dto.getUuid())
128         .setCreatedAt(dto.getCreatedAt())
129         .build();
130     }
131     return null;
132   }
133
134 }