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.step;
22 import java.util.Optional;
23 import java.util.function.Supplier;
24 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
25 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
26 import org.sonar.ce.task.projectanalysis.period.NewCodePeriodResolver;
27 import org.sonar.ce.task.projectanalysis.period.Period;
28 import org.sonar.ce.task.projectanalysis.period.PeriodHolder;
29 import org.sonar.ce.task.projectanalysis.period.PeriodHolderImpl;
30 import org.sonar.ce.task.step.ComputationStep;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.newcodeperiod.NewCodePeriodDao;
34 import org.sonar.db.newcodeperiod.NewCodePeriodDto;
37 * Populates the {@link PeriodHolder}
39 * Here is how these periods are computed :
40 * - Read the new code period from DB
41 * - Try to find the matching snapshots from the setting
42 * - If a snapshot is found, a period is set to the repository, otherwise fail with MessageException
44 public class LoadPeriodsStep implements ComputationStep {
46 private final AnalysisMetadataHolder analysisMetadataHolder;
47 private final NewCodePeriodDao newCodePeriodDao;
48 private final TreeRootHolder treeRootHolder;
49 private final PeriodHolderImpl periodsHolder;
50 private final DbClient dbClient;
51 private final NewCodePeriodResolver resolver;
53 public LoadPeriodsStep(AnalysisMetadataHolder analysisMetadataHolder, NewCodePeriodDao newCodePeriodDao, TreeRootHolder treeRootHolder,
54 PeriodHolderImpl periodsHolder, DbClient dbClient, NewCodePeriodResolver resolver) {
55 this.analysisMetadataHolder = analysisMetadataHolder;
56 this.newCodePeriodDao = newCodePeriodDao;
57 this.treeRootHolder = treeRootHolder;
58 this.periodsHolder = periodsHolder;
59 this.dbClient = dbClient;
60 this.resolver = resolver;
64 public String getDescription() {
65 return "Load new code period";
69 public void execute(ComputationStep.Context context) {
70 if (analysisMetadataHolder.isFirstAnalysis() || !analysisMetadataHolder.isLongLivingBranch()) {
71 periodsHolder.setPeriod(null);
75 String projectUuid = getProjectBranchUuid();
76 String branchUuid = treeRootHolder.getRoot().getUuid();
77 String projectVersion = treeRootHolder.getRoot().getProjectAttributes().getProjectVersion();
79 try (DbSession dbSession = dbClient.openSession(false)) {
80 Optional<NewCodePeriodDto> dto = firstPresent(
81 () -> getBranchSetting(dbSession, projectUuid, branchUuid),
82 () -> getProjectSetting(dbSession, projectUuid),
83 () -> getGlobalSetting(dbSession));
85 long analysisDate = analysisMetadataHolder.getAnalysisDate();
86 Period period = dto.map(d -> resolver.resolve(dbSession, branchUuid, d, analysisDate, projectVersion))
87 .orElseGet(() -> resolver.resolve(dbSession, branchUuid, NewCodePeriodDto.defaultInstance(), analysisDate, projectVersion));
88 periodsHolder.setPeriod(period);
92 private static <T> Optional<T> firstPresent(Supplier<Optional<T>>... suppliers) {
93 for (Supplier<Optional<T>> supplier : suppliers) {
94 Optional<T> result = supplier.get();
95 if (result.isPresent()) {
99 return Optional.empty();
102 private Optional<NewCodePeriodDto> getBranchSetting(DbSession dbSession, String projectUuid, String branchUuid) {
103 return newCodePeriodDao.selectByBranch(dbSession, projectUuid, branchUuid);
106 private Optional<NewCodePeriodDto> getProjectSetting(DbSession dbSession, String projectUuid) {
107 return newCodePeriodDao.selectByProject(dbSession, projectUuid);
110 private Optional<NewCodePeriodDto> getGlobalSetting(DbSession dbSession) {
111 return newCodePeriodDao.selectGlobal(dbSession);
114 private String getProjectBranchUuid() {
115 return analysisMetadataHolder.getProject().getUuid();