3 * Copyright (C) 2009-2019 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.UuidFactory;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.component.AnalysisPropertyDto;
32 import static org.sonar.core.config.CorePropertyDefinitions.SONAR_ANALYSIS;
35 * Persist analysis properties
36 * Only properties starting with "sonar.analysis" or "sonar.pullrequest" will be persisted in database
38 public class PersistAnalysisPropertiesStep implements ComputationStep {
40 private static final String SONAR_PULL_REQUEST = "sonar.pullrequest.";
42 private final DbClient dbClient;
43 private final AnalysisMetadataHolder analysisMetadataHolder;
44 private final BatchReportReader reportReader;
45 private final UuidFactory uuidFactory;
47 public PersistAnalysisPropertiesStep(DbClient dbClient, AnalysisMetadataHolder analysisMetadataHolder,
48 BatchReportReader reportReader, UuidFactory uuidFactory) {
49 this.dbClient = dbClient;
50 this.analysisMetadataHolder = analysisMetadataHolder;
51 this.reportReader = reportReader;
52 this.uuidFactory = uuidFactory;
56 public void execute(ComputationStep.Context context) {
57 final List<AnalysisPropertyDto> analysisPropertyDtos = new ArrayList<>();
58 reportReader.readContextProperties().forEachRemaining(
60 String propertyKey = contextProperty.getKey();
61 if (propertyKey.startsWith(SONAR_ANALYSIS) || propertyKey.startsWith(SONAR_PULL_REQUEST)) {
62 analysisPropertyDtos.add(new AnalysisPropertyDto()
63 .setUuid(uuidFactory.create())
65 .setValue(contextProperty.getValue())
66 .setSnapshotUuid(analysisMetadataHolder.getUuid()));
70 if (analysisPropertyDtos.isEmpty()) {
74 try (DbSession dbSession = dbClient.openSession(false)) {
75 dbClient.analysisPropertiesDao().insert(dbSession, analysisPropertyDtos);
81 public String getDescription() {
82 return "Persist analysis properties";