3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info 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.ce.task.projectanalysis.step;
22 import java.util.ArrayList;
23 import java.util.List;
24 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
25 import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
26 import org.sonar.ce.task.step.ComputationStep;
27 import org.sonar.core.util.CloseableIterator;
28 import org.sonar.core.util.UuidFactory;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbSession;
31 import org.sonar.db.component.AnalysisPropertyDto;
32 import org.sonar.scanner.protocol.output.ScannerReport;
34 import static org.sonar.core.config.CorePropertyDefinitions.SONAR_ANALYSIS;
35 import static org.sonar.core.config.CorePropertyDefinitions.SONAR_ANALYSIS_DETECTEDSCM;
38 * Persist analysis properties
40 public class PersistAnalysisPropertiesStep implements ComputationStep {
42 private static final String SONAR_PULL_REQUEST = "sonar.pullrequest.";
44 private final DbClient dbClient;
45 private final AnalysisMetadataHolder analysisMetadataHolder;
46 private final BatchReportReader reportReader;
47 private final UuidFactory uuidFactory;
49 public PersistAnalysisPropertiesStep(DbClient dbClient, AnalysisMetadataHolder analysisMetadataHolder,
50 BatchReportReader reportReader, UuidFactory uuidFactory) {
51 this.dbClient = dbClient;
52 this.analysisMetadataHolder = analysisMetadataHolder;
53 this.reportReader = reportReader;
54 this.uuidFactory = uuidFactory;
58 public void execute(ComputationStep.Context context) {
59 List<AnalysisPropertyDto> analysisPropertyDtos = new ArrayList<>();
60 try (CloseableIterator<ScannerReport.ContextProperty> it = reportReader.readContextProperties()) {
63 String propertyKey = contextProperty.getKey();
64 if (propertyKey.startsWith(SONAR_ANALYSIS) || propertyKey.startsWith(SONAR_PULL_REQUEST) || SONAR_ANALYSIS_DETECTEDSCM.equals(propertyKey)) {
65 analysisPropertyDtos.add(new AnalysisPropertyDto()
66 .setUuid(uuidFactory.create())
68 .setValue(contextProperty.getValue())
69 .setAnalysisUuid(analysisMetadataHolder.getUuid()));
74 if (analysisPropertyDtos.isEmpty()) {
78 try (DbSession dbSession = dbClient.openSession(false)) {
79 dbClient.analysisPropertiesDao().insert(dbSession, analysisPropertyDtos);
85 public String getDescription() {
86 return "Persist analysis properties";