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.qualitygate;
22 import java.util.Collection;
23 import java.util.Objects;
24 import java.util.Optional;
25 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
26 import org.sonar.db.DbClient;
27 import org.sonar.db.DbSession;
28 import org.sonar.db.qualitygate.QualityGateConditionDto;
29 import org.sonar.db.qualitygate.QualityGateDto;
30 import org.sonar.ce.task.projectanalysis.analysis.Organization;
31 import org.sonar.server.qualitygate.ShortLivingBranchQualityGate;
33 import static org.sonar.core.util.stream.MoreCollectors.toList;
35 public class QualityGateServiceImpl implements QualityGateService {
37 private final DbClient dbClient;
38 private final MetricRepository metricRepository;
40 public QualityGateServiceImpl(DbClient dbClient, MetricRepository metricRepository) {
41 this.dbClient = dbClient;
42 this.metricRepository = metricRepository;
46 public Optional<QualityGate> findById(long id) {
47 if (id == ShortLivingBranchQualityGate.ID) {
48 return Optional.of(buildShortLivingBranchHardcodedQualityGate());
50 try (DbSession dbSession = dbClient.openSession(false)) {
51 QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectById(dbSession, id);
52 if (qualityGateDto == null) {
53 return Optional.empty();
55 return Optional.of(toQualityGate(dbSession, qualityGateDto));
60 public QualityGate findDefaultQualityGate(Organization organization) {
61 try (DbSession dbSession = dbClient.openSession(false)) {
62 QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectByOrganizationAndUuid(dbSession, organization.toDto(), organization.getDefaultQualityGateUuid());
63 if (qualityGateDto == null) {
64 throw new IllegalStateException("The default Quality gate is missing on organization " + organization.getKey());
66 return toQualityGate(dbSession, qualityGateDto);
70 private QualityGate toQualityGate(DbSession dbSession, QualityGateDto qualityGateDto) {
71 Collection<QualityGateConditionDto> dtos = dbClient.gateConditionDao().selectForQualityGate(dbSession, qualityGateDto.getId());
73 Iterable<Condition> conditions = dtos.stream()
74 .map(input -> metricRepository.getOptionalById(input.getMetricId())
75 .map(metric -> new Condition(metric, input.getOperator(), input.getErrorThreshold(), input.getWarningThreshold()))
77 .filter(Objects::nonNull)
78 .collect(toList(dtos.size()));
80 return new QualityGate(qualityGateDto.getId(), qualityGateDto.getName(), conditions);
83 private QualityGate buildShortLivingBranchHardcodedQualityGate() {
84 return new QualityGate(
85 ShortLivingBranchQualityGate.ID,
86 ShortLivingBranchQualityGate.NAME,
87 ShortLivingBranchQualityGate.CONDITIONS.stream()
88 .map(c -> new Condition(metricRepository.getByKey(c.getMetricKey()), c.getOperator(), c.getErrorThreshold(), c.getWarnThreshold()))
89 .collect(toList(ShortLivingBranchQualityGate.CONDITIONS.size())));