3 * Copyright (C) 2009-2022 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.Arrays;
23 import java.util.Collection;
24 import java.util.Optional;
25 import java.util.function.Supplier;
26 import org.sonar.api.utils.System2;
27 import org.sonar.ce.task.log.CeTaskMessages;
28 import org.sonar.ce.task.log.CeTaskMessages.Message;
29 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
30 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
31 import org.sonar.ce.task.projectanalysis.period.NewCodePeriodResolver;
32 import org.sonar.ce.task.projectanalysis.period.Period;
33 import org.sonar.ce.task.projectanalysis.period.PeriodHolder;
34 import org.sonar.ce.task.projectanalysis.period.PeriodHolderImpl;
35 import org.sonar.ce.task.step.ComputationStep;
36 import org.sonar.db.DbClient;
37 import org.sonar.db.DbSession;
38 import org.sonar.db.newcodeperiod.NewCodePeriodDao;
39 import org.sonar.db.newcodeperiod.NewCodePeriodDto;
41 import static org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH;
44 * Populates the {@link PeriodHolder}
46 * Here is how these periods are computed :
47 * - Read the new code period from DB
48 * - Try to find the matching snapshots from the setting
49 * - If a snapshot is found, a period is set to the repository, otherwise fail with MessageException
51 public class LoadPeriodsStep implements ComputationStep {
53 private final AnalysisMetadataHolder analysisMetadataHolder;
54 private final NewCodePeriodDao newCodePeriodDao;
55 private final TreeRootHolder treeRootHolder;
56 private final PeriodHolderImpl periodsHolder;
57 private final DbClient dbClient;
58 private final NewCodePeriodResolver resolver;
59 private final CeTaskMessages ceTaskMessages;
60 private final System2 system2;
62 public LoadPeriodsStep(AnalysisMetadataHolder analysisMetadataHolder, NewCodePeriodDao newCodePeriodDao, TreeRootHolder treeRootHolder,
63 PeriodHolderImpl periodsHolder, DbClient dbClient, NewCodePeriodResolver resolver, CeTaskMessages ceTaskMessages, System2 system2) {
64 this.analysisMetadataHolder = analysisMetadataHolder;
65 this.newCodePeriodDao = newCodePeriodDao;
66 this.treeRootHolder = treeRootHolder;
67 this.periodsHolder = periodsHolder;
68 this.dbClient = dbClient;
69 this.resolver = resolver;
70 this.ceTaskMessages = ceTaskMessages;
71 this.system2 = system2;
75 public String getDescription() {
76 return "Load new code period";
80 public void execute(ComputationStep.Context context) {
81 if (!analysisMetadataHolder.isBranch()) {
82 periodsHolder.setPeriod(null);
86 String projectUuid = getProjectBranchUuid();
87 String branchUuid = treeRootHolder.getRoot().getUuid();
88 String projectVersion = treeRootHolder.getRoot().getProjectAttributes().getProjectVersion();
90 var newCodePeriod = analysisMetadataHolder.getNewCodeReferenceBranch()
91 .filter(s -> !s.isBlank())
92 .map(b -> new NewCodePeriodDto().setType(REFERENCE_BRANCH).setValue(b))
95 try (DbSession dbSession = dbClient.openSession(false)) {
96 Optional<NewCodePeriodDto> specificSetting = firstPresent(Arrays.asList(
97 () -> getBranchSetting(dbSession, projectUuid, branchUuid),
98 () -> getProjectSetting(dbSession, projectUuid)
101 if (newCodePeriod == null) {
102 newCodePeriod = specificSetting.or(() -> getGlobalSetting(dbSession)).orElse(NewCodePeriodDto.defaultInstance());
104 if (analysisMetadataHolder.isFirstAnalysis() && newCodePeriod.getType() != REFERENCE_BRANCH) {
105 periodsHolder.setPeriod(null);
108 } else if (specificSetting.isPresent()) {
109 ceTaskMessages.add(new Message("A scanner parameter is defining a new code reference branch "
110 + "but one is already defined specifically for the branch in the New Code Period settings", system2.now()));
113 Period period = resolver.resolve(dbSession, branchUuid, newCodePeriod, projectVersion);
114 periodsHolder.setPeriod(period);
118 private static <T> Optional<T> firstPresent(Collection<Supplier<Optional<T>>> suppliers) {
119 for (Supplier<Optional<T>> supplier : suppliers) {
120 Optional<T> result = supplier.get();
121 if (result.isPresent()) {
125 return Optional.empty();
128 private Optional<NewCodePeriodDto> getBranchSetting(DbSession dbSession, String projectUuid, String branchUuid) {
129 return newCodePeriodDao.selectByBranch(dbSession, projectUuid, branchUuid);
132 private Optional<NewCodePeriodDto> getProjectSetting(DbSession dbSession, String projectUuid) {
133 return newCodePeriodDao.selectByProject(dbSession, projectUuid);
136 private Optional<NewCodePeriodDto> getGlobalSetting(DbSession dbSession) {
137 return newCodePeriodDao.selectGlobal(dbSession);
140 private String getProjectBranchUuid() {
141 return analysisMetadataHolder.getProject().getUuid();