3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.computation.task.projectanalysis.step;
22 import com.google.common.base.Function;
23 import java.util.Date;
24 import org.sonar.api.utils.MessageException;
25 import org.sonar.ce.queue.CeTask;
26 import org.sonar.scanner.protocol.output.ScannerReport;
27 import org.sonar.scanner.protocol.output.ScannerReport.Metadata.QProfile;
28 import org.sonar.server.computation.task.projectanalysis.analysis.MutableAnalysisMetadataHolder;
29 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
30 import org.sonar.server.computation.task.projectanalysis.qualityprofile.QualityProfile;
31 import org.sonar.server.computation.task.step.ComputationStep;
33 import static com.google.common.collect.Maps.transformValues;
34 import static java.lang.String.format;
35 import static org.apache.commons.lang.StringUtils.isNotEmpty;
38 * Feed analysis metadata holder with metadata from the analysis report.
40 public class LoadReportAnalysisMetadataHolderStep implements ComputationStep {
42 private static final ToComputeQProfile TO_COMPUTE_QPROFILE = new ToComputeQProfile();
44 private static final class ToComputeQProfile implements Function<QProfile, QualityProfile> {
46 public QualityProfile apply(QProfile input) {
47 return new QualityProfile(input.getKey(), input.getName(), input.getLanguage(), new Date(input.getRulesUpdatedAt()));
51 private final CeTask ceTask;
52 private final BatchReportReader reportReader;
53 private final MutableAnalysisMetadataHolder mutableAnalysisMetadataHolder;
55 public LoadReportAnalysisMetadataHolderStep(CeTask ceTask, BatchReportReader reportReader, MutableAnalysisMetadataHolder mutableAnalysisMetadataHolder) {
57 this.reportReader = reportReader;
58 this.mutableAnalysisMetadataHolder = mutableAnalysisMetadataHolder;
62 public void execute() {
63 ScannerReport.Metadata reportMetadata = reportReader.readMetadata();
65 checkProjectKeyConsistency(reportMetadata);
67 mutableAnalysisMetadataHolder.setRootComponentRef(reportMetadata.getRootComponentRef());
68 mutableAnalysisMetadataHolder.setBranch(isNotEmpty(reportMetadata.getBranch()) ? reportMetadata.getBranch() : null);
69 mutableAnalysisMetadataHolder.setAnalysisDate(reportMetadata.getAnalysisDate());
70 mutableAnalysisMetadataHolder.setCrossProjectDuplicationEnabled(reportMetadata.getCrossProjectDuplicationActivated());
71 mutableAnalysisMetadataHolder.setQProfilesByLanguage(transformValues(reportMetadata.getQprofilesPerLanguage(), TO_COMPUTE_QPROFILE));
74 private void checkProjectKeyConsistency(ScannerReport.Metadata reportMetadata) {
75 String reportProjectKey = projectKeyFromReport(reportMetadata);
76 String componentKey = ceTask.getComponentKey();
77 if (componentKey == null) {
78 throw MessageException.of(format(
79 "Compute Engine task component key is null. Project with UUID %s must have been deleted since report was uploaded. Can not proceed.",
80 ceTask.getComponentUuid()));
82 if (!componentKey.equals(reportProjectKey)) {
83 throw MessageException.of(format(
84 "ProjectKey in report (%s) is not consistent with projectKey under which the report as been submitted (%s)",
90 private static String projectKeyFromReport(ScannerReport.Metadata reportMetadata) {
91 if (isNotEmpty(reportMetadata.getBranch())) {
92 return reportMetadata.getProjectKey() + ":" + reportMetadata.getBranch();
94 return reportMetadata.getProjectKey();
98 public String getDescription() {
99 return "Load analysis metadata";