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.Optional;
23 import org.sonar.api.config.Configuration;
24 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
25 import org.sonar.ce.task.projectanalysis.component.ConfigurationRepository;
26 import org.sonar.ce.task.projectanalysis.qualitygate.MutableQualityGateHolder;
27 import org.sonar.ce.task.projectanalysis.qualitygate.QualityGate;
28 import org.sonar.ce.task.projectanalysis.qualitygate.QualityGateService;
29 import org.sonar.ce.task.step.ComputationStep;
30 import org.sonar.server.qualitygate.ShortLivingBranchQualityGate;
32 import static org.apache.commons.lang.StringUtils.isBlank;
35 * This step retrieves the QualityGate and stores it in
36 * {@link MutableQualityGateHolder}.
38 public class LoadQualityGateStep implements ComputationStep {
39 private static final String PROPERTY_PROJECT_QUALITY_GATE = "sonar.qualitygate";
41 private final ConfigurationRepository configRepository;
42 private final QualityGateService qualityGateService;
43 private final MutableQualityGateHolder qualityGateHolder;
44 private final AnalysisMetadataHolder analysisMetadataHolder;
46 public LoadQualityGateStep(ConfigurationRepository settingsRepository, QualityGateService qualityGateService, MutableQualityGateHolder qualityGateHolder,
47 AnalysisMetadataHolder analysisMetadataHolder) {
48 this.configRepository = settingsRepository;
49 this.qualityGateService = qualityGateService;
50 this.qualityGateHolder = qualityGateHolder;
51 this.analysisMetadataHolder = analysisMetadataHolder;
55 public void execute(ComputationStep.Context context) {
56 Optional<QualityGate> qualityGate = getShortLivingBranchQualityGate();
57 if (!qualityGate.isPresent()) {
58 // Not on a short living branch, let's retrieve the QG of the project
59 qualityGate = getProjectQualityGate();
60 if (!qualityGate.isPresent()) {
61 // No QG defined for the project, let's retrieve the QG on the organization
62 qualityGate = Optional.of(getOrganizationDefaultQualityGate());
66 qualityGateHolder.setQualityGate(qualityGate.orElseThrow(() -> new IllegalStateException("Quality gate not present")));
69 private Optional<QualityGate> getShortLivingBranchQualityGate() {
70 if (analysisMetadataHolder.isShortLivingBranch() || analysisMetadataHolder.isPullRequest()) {
71 Optional<QualityGate> qualityGate = qualityGateService.findById(ShortLivingBranchQualityGate.ID);
72 if (qualityGate.isPresent()) {
75 throw new IllegalStateException("Failed to retrieve hardcoded short living branch Quality Gate");
78 return Optional.empty();
82 private Optional<QualityGate> getProjectQualityGate() {
83 Configuration config = configRepository.getConfiguration();
84 String qualityGateSetting = config.get(PROPERTY_PROJECT_QUALITY_GATE).orElse(null);
86 if (isBlank(qualityGateSetting)) {
87 return Optional.empty();
91 long qualityGateId = Long.parseLong(qualityGateSetting);
92 return qualityGateService.findById(qualityGateId);
93 } catch (NumberFormatException e) {
94 throw new IllegalStateException(
95 String.format("Unsupported value (%s) in property %s", qualityGateSetting, PROPERTY_PROJECT_QUALITY_GATE), e);
99 private QualityGate getOrganizationDefaultQualityGate() {
100 return qualityGateService.findDefaultQualityGate(analysisMetadataHolder.getOrganization());
104 public String getDescription() {
105 return "Load Quality gate";