]> source.dussan.org Git - sonarqube.git/blob
15b3dc5a7926b3ccc1b61a49ac2d767f42b7077e
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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.ce.task.projectanalysis.step;
21
22 import java.util.Optional;
23 import java.util.function.Function;
24 import javax.annotation.Nullable;
25 import org.sonar.ce.task.projectanalysis.analysis.Analysis;
26 import org.sonar.ce.task.projectanalysis.analysis.Branch;
27 import org.sonar.ce.task.projectanalysis.analysis.MutableAnalysisMetadataHolder;
28 import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
29 import org.sonar.ce.task.projectanalysis.component.Component;
30 import org.sonar.ce.task.projectanalysis.component.ComponentKeyGenerator;
31 import org.sonar.ce.task.projectanalysis.component.ComponentTreeBuilder;
32 import org.sonar.ce.task.projectanalysis.component.ComponentUuidFactoryWithMigration;
33 import org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl;
34 import org.sonar.ce.task.projectanalysis.component.MutableTreeRootHolder;
35 import org.sonar.ce.task.projectanalysis.component.ProjectAttributes;
36 import org.sonar.ce.task.projectanalysis.component.ReportModulesPath;
37 import org.sonar.ce.task.step.ComputationStep;
38 import org.sonar.db.DbClient;
39 import org.sonar.db.DbSession;
40 import org.sonar.db.component.SnapshotDto;
41 import org.sonar.scanner.protocol.output.ScannerReport;
42
43 import static com.google.common.base.MoreObjects.firstNonNull;
44 import static org.apache.commons.lang.StringUtils.trimToNull;
45
46 /**
47  * Populates the {@link MutableTreeRootHolder} and {@link MutableAnalysisMetadataHolder} from the {@link BatchReportReader}
48  */
49 public class BuildComponentTreeStep implements ComputationStep {
50
51   private static final String DEFAULT_PROJECT_VERSION = "not provided";
52
53   private final DbClient dbClient;
54   private final BatchReportReader reportReader;
55   private final MutableTreeRootHolder treeRootHolder;
56   private final MutableAnalysisMetadataHolder analysisMetadataHolder;
57   private final ReportModulesPath reportModulesPath;
58
59   public BuildComponentTreeStep(DbClient dbClient, BatchReportReader reportReader, MutableTreeRootHolder treeRootHolder,
60     MutableAnalysisMetadataHolder analysisMetadataHolder, ReportModulesPath reportModulesPath) {
61     this.dbClient = dbClient;
62     this.reportReader = reportReader;
63     this.treeRootHolder = treeRootHolder;
64     this.analysisMetadataHolder = analysisMetadataHolder;
65     this.reportModulesPath = reportModulesPath;
66   }
67
68   @Override
69   public String getDescription() {
70     return "Build tree of components";
71   }
72
73   @Override
74   public void execute(ComputationStep.Context context) {
75     try (DbSession dbSession = dbClient.openSession(false)) {
76       ScannerReport.Component reportProject = reportReader.readComponent(analysisMetadataHolder.getRootComponentRef());
77       ComponentKeyGenerator keyGenerator = loadKeyGenerator();
78       ComponentKeyGenerator publicKeyGenerator = loadPublicKeyGenerator();
79       ScannerReport.Metadata metadata = reportReader.readMetadata();
80
81       // root key of branch, not necessarily of project
82       String rootKey = keyGenerator.generateKey(reportProject.getKey(), null);
83       Function<String, String> pathToKey = path -> keyGenerator.generateKey(reportProject.getKey(), path);
84       // loads the UUIDs from database. If they don't exist, then generate new ones
85       ComponentUuidFactoryWithMigration componentUuidFactoryWithMigration = new ComponentUuidFactoryWithMigration(dbClient, dbSession, rootKey, pathToKey, reportModulesPath.get());
86
87       String rootUuid = componentUuidFactoryWithMigration.getOrCreateForKey(rootKey);
88       Optional<SnapshotDto> baseAnalysis = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, rootUuid);
89
90       ComponentTreeBuilder builder = new ComponentTreeBuilder(keyGenerator, publicKeyGenerator,
91         componentUuidFactoryWithMigration::getOrCreateForKey,
92         reportReader::readComponent,
93         analysisMetadataHolder.getProject(),
94         analysisMetadataHolder.getBranch(),
95         createProjectAttributes(metadata, baseAnalysis.orElse(null)));
96       String relativePathFromScmRoot = metadata.getRelativePathFromScmRoot();
97
98       Component reportTreeRoot = builder.buildProject(reportProject, relativePathFromScmRoot);
99
100       if (analysisMetadataHolder.isPullRequest()) {
101         Component changedComponentTreeRoot = builder.buildChangedComponentTreeRoot(reportTreeRoot);
102         treeRootHolder.setRoots(changedComponentTreeRoot, reportTreeRoot);
103       } else {
104         treeRootHolder.setRoots(reportTreeRoot, reportTreeRoot);
105       }
106
107       analysisMetadataHolder.setBaseAnalysis(baseAnalysis.map(BuildComponentTreeStep::toAnalysis).orElse(null));
108
109       context.getStatistics().add("components", treeRootHolder.getSize());
110     }
111   }
112
113   private static ProjectAttributes createProjectAttributes(ScannerReport.Metadata metadata, @Nullable SnapshotDto baseAnalysis) {
114     String projectVersion = computeProjectVersion(trimToNull(metadata.getProjectVersion()), baseAnalysis);
115     String buildString = trimToNull(metadata.getBuildString());
116     String scmRevisionId = trimToNull(metadata.getScmRevisionId());
117     return new ProjectAttributes(projectVersion, buildString, scmRevisionId);
118   }
119
120   private static String computeProjectVersion(@Nullable String projectVersion, @Nullable SnapshotDto baseAnalysis) {
121     if (projectVersion != null) {
122       return projectVersion;
123     }
124     if (baseAnalysis != null) {
125       return firstNonNull(baseAnalysis.getProjectVersion(), DEFAULT_PROJECT_VERSION);
126     }
127     return DEFAULT_PROJECT_VERSION;
128   }
129
130   private ComponentKeyGenerator loadKeyGenerator() {
131     return analysisMetadataHolder.getBranch();
132   }
133
134   private ComponentKeyGenerator loadPublicKeyGenerator() {
135     Branch branch = analysisMetadataHolder.getBranch();
136
137     // for non-legacy branches, the public key is different from the DB key.
138     if (!branch.isMain()) {
139       return new DefaultBranchImpl();
140     }
141     return branch;
142   }
143
144   private static Analysis toAnalysis(SnapshotDto dto) {
145     return new Analysis.Builder()
146       .setUuid(dto.getUuid())
147       .setCreatedAt(dto.getCreatedAt())
148       .build();
149   }
150
151 }