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.apache.commons.lang.StringUtils;
24 import org.sonar.api.config.Settings;
25 import org.sonar.api.utils.log.Logger;
26 import org.sonar.api.utils.log.Loggers;
27 import org.sonar.server.computation.task.projectanalysis.component.Component;
28 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
29 import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
30 import org.sonar.server.computation.task.projectanalysis.component.SettingsRepository;
31 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
32 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
33 import org.sonar.server.computation.task.projectanalysis.qualitygate.MutableQualityGateHolder;
34 import org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGate;
35 import org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGateService;
36 import org.sonar.server.computation.task.step.ComputationStep;
38 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
41 * This step retrieves the QualityGate and stores it in
42 * {@link MutableQualityGateHolder}.
44 public class LoadQualityGateStep implements ComputationStep {
45 private static final Logger LOGGER = Loggers.get(LoadQualityGateStep.class);
47 private static final String PROPERTY_QUALITY_GATE = "sonar.qualitygate";
49 private final TreeRootHolder treeRootHolder;
50 private final SettingsRepository settingsRepository;
51 private final QualityGateService qualityGateService;
52 private final MutableQualityGateHolder qualityGateHolder;
54 public LoadQualityGateStep(TreeRootHolder treeRootHolder, SettingsRepository settingsRepository,
55 QualityGateService qualityGateService, MutableQualityGateHolder qualityGateHolder) {
56 this.treeRootHolder = treeRootHolder;
57 this.settingsRepository = settingsRepository;
58 this.qualityGateService = qualityGateService;
59 this.qualityGateHolder = qualityGateHolder;
63 public void execute() {
64 new DepthTraversalTypeAwareCrawler(
65 new TypeAwareVisitorAdapter(CrawlerDepthLimit.PROJECT, PRE_ORDER) {
67 public void visitProject(Component project) {
68 executeForProject(project);
70 }).visit(treeRootHolder.getRoot());
73 private void executeForProject(Component project) {
74 String projectKey = project.getKey();
75 Settings settings = settingsRepository.getSettings(project);
76 String qualityGateSetting = settings.getString(PROPERTY_QUALITY_GATE);
78 if (qualityGateSetting == null || StringUtils.isBlank(qualityGateSetting)) {
79 LOGGER.debug("No quality gate is configured for project " + projectKey);
80 qualityGateHolder.setNoQualityGate();
85 long qualityGateId = Long.parseLong(qualityGateSetting);
86 Optional<QualityGate> qualityGate = qualityGateService.findById(qualityGateId);
87 if (qualityGate.isPresent()) {
88 qualityGateHolder.setQualityGate(qualityGate.get());
90 qualityGateHolder.setNoQualityGate();
92 } catch (NumberFormatException e) {
93 throw new IllegalStateException(
94 String.format("Unsupported value (%s) in property %s", qualityGateSetting, PROPERTY_QUALITY_GATE),
100 public String getDescription() {
101 return "Load Quality gate";