]> source.dussan.org Git - sonarqube.git/blob
1417de24f8bc8d67a5286a5d626afc740e3494cf
[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.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;
37
38 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
39
40 /**
41  * This step retrieves the QualityGate and stores it in
42  * {@link MutableQualityGateHolder}.
43  */
44 public class LoadQualityGateStep implements ComputationStep {
45   private static final Logger LOGGER = Loggers.get(LoadQualityGateStep.class);
46
47   private static final String PROPERTY_QUALITY_GATE = "sonar.qualitygate";
48
49   private final TreeRootHolder treeRootHolder;
50   private final SettingsRepository settingsRepository;
51   private final QualityGateService qualityGateService;
52   private final MutableQualityGateHolder qualityGateHolder;
53
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;
60   }
61
62   @Override
63   public void execute() {
64     new DepthTraversalTypeAwareCrawler(
65       new TypeAwareVisitorAdapter(CrawlerDepthLimit.PROJECT, PRE_ORDER) {
66         @Override
67         public void visitProject(Component project) {
68           executeForProject(project);
69         }
70       }).visit(treeRootHolder.getRoot());
71   }
72
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);
77
78     if (qualityGateSetting == null || StringUtils.isBlank(qualityGateSetting)) {
79       LOGGER.debug("No quality gate is configured for project " + projectKey);
80       qualityGateHolder.setNoQualityGate();
81       return;
82     }
83
84     try {
85       long qualityGateId = Long.parseLong(qualityGateSetting);
86       Optional<QualityGate> qualityGate = qualityGateService.findById(qualityGateId);
87       if (qualityGate.isPresent()) {
88         qualityGateHolder.setQualityGate(qualityGate.get());
89       } else {
90         qualityGateHolder.setNoQualityGate();
91       }
92     } catch (NumberFormatException e) {
93       throw new IllegalStateException(
94         String.format("Unsupported value (%s) in property %s", qualityGateSetting, PROPERTY_QUALITY_GATE),
95         e);
96     }
97   }
98
99   @Override
100   public String getDescription() {
101     return "Load Quality gate";
102   }
103 }