]> source.dussan.org Git - sonarqube.git/blob
68abc8c8b62d46ea85b5f6c08821a7d216339d2a
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.step;
21
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;
33
34 import static org.apache.commons.lang.StringUtils.isBlank;
35
36 /**
37  * This step retrieves the QualityGate and stores it in
38  * {@link MutableQualityGateHolder}.
39  */
40 public class LoadQualityGateStep implements ComputationStep {
41   private static final Logger LOGGER = Loggers.get(LoadQualityGateStep.class);
42
43   private static final String PROPERTY_QUALITY_GATE = "sonar.qualitygate";
44
45   private final ConfigurationRepository configRepository;
46   private final QualityGateService qualityGateService;
47   private final MutableQualityGateHolder qualityGateHolder;
48   private final AnalysisMetadataHolder analysisMetadataHolder;
49
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;
56   }
57
58   @Override
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());
64       } else {
65         throw new IllegalStateException("Failed to retrieve hardcoded short living branch Quality Gate");
66       }
67       return;
68     }
69     Configuration config = configRepository.getConfiguration();
70     String qualityGateSetting = config.get(PROPERTY_QUALITY_GATE).orElse(null);
71
72     if (isBlank(qualityGateSetting)) {
73       LOGGER.debug("No quality gate is configured");
74       qualityGateHolder.setNoQualityGate();
75       return;
76     }
77
78     try {
79       long qualityGateId = Long.parseLong(qualityGateSetting);
80       Optional<QualityGate> qualityGate = qualityGateService.findById(qualityGateId);
81       if (qualityGate.isPresent()) {
82         qualityGateHolder.setQualityGate(qualityGate.get());
83       } else {
84         qualityGateHolder.setNoQualityGate();
85       }
86     } catch (NumberFormatException e) {
87       throw new IllegalStateException(
88         String.format("Unsupported value (%s) in property %s", qualityGateSetting, PROPERTY_QUALITY_GATE),
89         e);
90     }
91   }
92
93   @Override
94   public String getDescription() {
95     return "Load Quality gate";
96   }
97 }