3 * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.step;
22 import com.google.common.base.Optional;
23 import org.sonar.api.config.Configuration;
24 import org.sonar.api.utils.log.Logger;
25 import org.sonar.api.utils.log.Loggers;
26 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
27 import org.sonar.server.computation.task.projectanalysis.component.ConfigurationRepository;
28 import org.sonar.server.computation.task.projectanalysis.qualitygate.MutableQualityGateHolder;
29 import org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGate;
30 import org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGateService;
31 import org.sonar.server.computation.task.step.ComputationStep;
32 import org.sonar.server.qualitygate.ShortLivingBranchQualityGate;
34 import static org.apache.commons.lang.StringUtils.isBlank;
37 * This step retrieves the QualityGate and stores it in
38 * {@link MutableQualityGateHolder}.
40 public class LoadQualityGateStep implements ComputationStep {
41 private static final Logger LOGGER = Loggers.get(LoadQualityGateStep.class);
43 private static final String PROPERTY_QUALITY_GATE = "sonar.qualitygate";
45 private final ConfigurationRepository configRepository;
46 private final QualityGateService qualityGateService;
47 private final MutableQualityGateHolder qualityGateHolder;
48 private final AnalysisMetadataHolder analysisMetadataHolder;
50 public LoadQualityGateStep(ConfigurationRepository settingsRepository, QualityGateService qualityGateService, MutableQualityGateHolder qualityGateHolder,
51 AnalysisMetadataHolder analysisMetadataHolder) {
52 this.configRepository = settingsRepository;
53 this.qualityGateService = qualityGateService;
54 this.qualityGateHolder = qualityGateHolder;
55 this.analysisMetadataHolder = analysisMetadataHolder;
59 public void execute() {
60 if (analysisMetadataHolder.isShortLivingBranch()) {
61 Optional<QualityGate> qualityGate = qualityGateService.findById(ShortLivingBranchQualityGate.ID);
62 if (qualityGate.isPresent()) {
63 qualityGateHolder.setQualityGate(qualityGate.get());
65 throw new IllegalStateException("Failed to retrieve hardcoded short living branch Quality Gate");
69 Configuration config = configRepository.getConfiguration();
70 String qualityGateSetting = config.get(PROPERTY_QUALITY_GATE).orElse(null);
72 if (isBlank(qualityGateSetting)) {
73 LOGGER.debug("No quality gate is configured");
74 qualityGateHolder.setNoQualityGate();
79 long qualityGateId = Long.parseLong(qualityGateSetting);
80 Optional<QualityGate> qualityGate = qualityGateService.findById(qualityGateId);
81 if (qualityGate.isPresent()) {
82 qualityGateHolder.setQualityGate(qualityGate.get());
84 qualityGateHolder.setNoQualityGate();
86 } catch (NumberFormatException e) {
87 throw new IllegalStateException(
88 String.format("Unsupported value (%s) in property %s", qualityGateSetting, PROPERTY_QUALITY_GATE),
94 public String getDescription() {
95 return "Load Quality gate";