]> source.dussan.org Git - sonarqube.git/blob
b9cadec700ebbb9fe6984d544720abe02fed0785
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.ce.task.projectanalysis.step;
21
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;
31
32 import static org.apache.commons.lang.StringUtils.isBlank;
33
34 /**
35  * This step retrieves the QualityGate and stores it in
36  * {@link MutableQualityGateHolder}.
37  */
38 public class LoadQualityGateStep implements ComputationStep {
39   private static final String PROPERTY_PROJECT_QUALITY_GATE = "sonar.qualitygate";
40
41   private final ConfigurationRepository configRepository;
42   private final QualityGateService qualityGateService;
43   private final MutableQualityGateHolder qualityGateHolder;
44   private final AnalysisMetadataHolder analysisMetadataHolder;
45
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;
52   }
53
54   @Override
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());
63       }
64     }
65
66     qualityGateHolder.setQualityGate(qualityGate.orElseThrow(() -> new IllegalStateException("Quality gate not present")));
67   }
68
69   private Optional<QualityGate> getShortLivingBranchQualityGate() {
70     if (analysisMetadataHolder.isShortLivingBranch() || analysisMetadataHolder.isPullRequest()) {
71       Optional<QualityGate> qualityGate = qualityGateService.findById(ShortLivingBranchQualityGate.ID);
72       if (qualityGate.isPresent()) {
73         return qualityGate;
74       } else {
75         throw new IllegalStateException("Failed to retrieve hardcoded short living branch Quality Gate");
76       }
77     } else {
78       return Optional.empty();
79     }
80   }
81
82   private Optional<QualityGate> getProjectQualityGate() {
83     Configuration config = configRepository.getConfiguration();
84     String qualityGateSetting = config.get(PROPERTY_PROJECT_QUALITY_GATE).orElse(null);
85
86     if (isBlank(qualityGateSetting)) {
87       return Optional.empty();
88     }
89
90     try {
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);
96     }
97   }
98
99   private QualityGate getOrganizationDefaultQualityGate() {
100     return qualityGateService.findDefaultQualityGate(analysisMetadataHolder.getOrganization());
101   }
102
103   @Override
104   public String getDescription() {
105     return "Load Quality gate";
106   }
107 }