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