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.qualitygate;
22 import com.google.common.base.Optional;
23 import java.util.Collection;
24 import java.util.Objects;
25 import org.sonar.db.DbClient;
26 import org.sonar.db.DbSession;
27 import org.sonar.db.qualitygate.QualityGateConditionDto;
28 import org.sonar.db.qualitygate.QualityGateDto;
29 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepository;
30 import org.sonar.server.qualitygate.ShortLivingBranchQualityGate;
32 import static org.sonar.core.util.stream.MoreCollectors.toList;
34 public class QualityGateServiceImpl implements QualityGateService {
36 private final DbClient dbClient;
37 private final MetricRepository metricRepository;
39 public QualityGateServiceImpl(DbClient dbClient, MetricRepository metricRepository) {
40 this.dbClient = dbClient;
41 this.metricRepository = metricRepository;
45 public Optional<QualityGate> findById(long id) {
46 if (id == ShortLivingBranchQualityGate.ID) {
47 return Optional.of(buildShortLivingBranchHardcodedQualityGate());
49 try (DbSession dbSession = dbClient.openSession(false)) {
50 QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectById(dbSession, id);
51 if (qualityGateDto == null) {
52 return Optional.absent();
54 return Optional.of(toQualityGate(dbSession, qualityGateDto));
58 private QualityGate toQualityGate(DbSession dbSession, QualityGateDto qualityGateDto) {
59 Collection<QualityGateConditionDto> dtos = dbClient.gateConditionDao().selectForQualityGate(dbSession, qualityGateDto.getId());
61 Iterable<Condition> conditions = dtos.stream()
62 .map(input -> metricRepository.getOptionalById(input.getMetricId())
63 .map(metric -> new Condition(metric, input.getOperator(), input.getErrorThreshold(), input.getWarningThreshold(), input.getPeriod() != null))
65 .filter(Objects::nonNull)
66 .collect(toList(dtos.size()));
68 return new QualityGate(qualityGateDto.getId(), qualityGateDto.getName(), conditions);
71 private QualityGate buildShortLivingBranchHardcodedQualityGate() {
72 return new QualityGate(
73 ShortLivingBranchQualityGate.ID,
74 ShortLivingBranchQualityGate.NAME,
75 ShortLivingBranchQualityGate.CONDITIONS.stream()
76 .map(c -> new Condition(metricRepository.getByKey(c.getMetricKey()), c.getOperator(), c.getErrorThreshold(), c.getWarnThreshold(), c.isOnLeak()))
77 .collect(toList(ShortLivingBranchQualityGate.CONDITIONS.size())));