-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
LoadReportAnalysisMetadataHolderStep.java
101 lines (88 loc) · 4.38 KB
/
LoadReportAnalysisMetadataHolderStep.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.computation.task.projectanalysis.step;
import com.google.common.base.Function;
import java.util.Date;
import org.sonar.api.utils.MessageException;
import org.sonar.ce.queue.CeTask;
import org.sonar.scanner.protocol.output.ScannerReport;
import org.sonar.scanner.protocol.output.ScannerReport.Metadata.QProfile;
import org.sonar.server.computation.task.projectanalysis.analysis.MutableAnalysisMetadataHolder;
import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
import org.sonar.server.computation.task.projectanalysis.qualityprofile.QualityProfile;
import org.sonar.server.computation.task.step.ComputationStep;
import static com.google.common.collect.Maps.transformValues;
import static java.lang.String.format;
import static org.apache.commons.lang.StringUtils.isNotEmpty;
/**
* Feed analysis metadata holder with metadata from the analysis report.
*/
public class LoadReportAnalysisMetadataHolderStep implements ComputationStep {
private static final ToComputeQProfile TO_COMPUTE_QPROFILE = new ToComputeQProfile();
private static final class ToComputeQProfile implements Function<QProfile, QualityProfile> {
@Override
public QualityProfile apply(QProfile input) {
return new QualityProfile(input.getKey(), input.getName(), input.getLanguage(), new Date(input.getRulesUpdatedAt()));
}
}
private final CeTask ceTask;
private final BatchReportReader reportReader;
private final MutableAnalysisMetadataHolder mutableAnalysisMetadataHolder;
public LoadReportAnalysisMetadataHolderStep(CeTask ceTask, BatchReportReader reportReader, MutableAnalysisMetadataHolder mutableAnalysisMetadataHolder) {
this.ceTask = ceTask;
this.reportReader = reportReader;
this.mutableAnalysisMetadataHolder = mutableAnalysisMetadataHolder;
}
@Override
public void execute() {
ScannerReport.Metadata reportMetadata = reportReader.readMetadata();
mutableAnalysisMetadataHolder.setAnalysisDate(reportMetadata.getAnalysisDate());
checkProjectKeyConsistency(reportMetadata);
mutableAnalysisMetadataHolder.setRootComponentRef(reportMetadata.getRootComponentRef());
mutableAnalysisMetadataHolder.setBranch(isNotEmpty(reportMetadata.getBranch()) ? reportMetadata.getBranch() : null);
mutableAnalysisMetadataHolder.setCrossProjectDuplicationEnabled(reportMetadata.getCrossProjectDuplicationActivated());
mutableAnalysisMetadataHolder.setQProfilesByLanguage(transformValues(reportMetadata.getQprofilesPerLanguage(), TO_COMPUTE_QPROFILE));
}
private void checkProjectKeyConsistency(ScannerReport.Metadata reportMetadata) {
String reportProjectKey = projectKeyFromReport(reportMetadata);
String componentKey = ceTask.getComponentKey();
if (componentKey == null) {
throw MessageException.of(format(
"Compute Engine task component key is null. Project with UUID %s must have been deleted since report was uploaded. Can not proceed.",
ceTask.getComponentUuid()));
}
if (!componentKey.equals(reportProjectKey)) {
throw MessageException.of(format(
"ProjectKey in report (%s) is not consistent with projectKey under which the report as been submitted (%s)",
reportProjectKey,
componentKey));
}
}
private static String projectKeyFromReport(ScannerReport.Metadata reportMetadata) {
if (isNotEmpty(reportMetadata.getBranch())) {
return reportMetadata.getProjectKey() + ":" + reportMetadata.getBranch();
}
return reportMetadata.getProjectKey();
}
@Override
public String getDescription() {
return "Load analysis metadata";
}
}