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