-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
LoadPeriodsStep.java
104 lines (94 loc) · 4.15 KB
/
LoadPeriodsStep.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* SonarQube
* Copyright (C) 2009-2018 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.ce.task.projectanalysis.step;
import javax.annotation.CheckForNull;
import org.sonar.api.config.Configuration;
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
import org.sonar.ce.task.projectanalysis.component.Component;
import org.sonar.ce.task.projectanalysis.component.ComponentVisitor;
import org.sonar.ce.task.projectanalysis.component.ConfigurationRepository;
import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
import org.sonar.ce.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
import org.sonar.ce.task.projectanalysis.component.TypeAwareVisitorAdapter;
import org.sonar.ce.task.projectanalysis.period.Period;
import org.sonar.ce.task.projectanalysis.period.PeriodHolder;
import org.sonar.ce.task.projectanalysis.period.PeriodHolderImpl;
import org.sonar.ce.task.step.ComputationStep;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
/**
* Populates the {@link PeriodHolder}
* <p/>
* Here is how these periods are computed :
* - Read the period property ${@link org.sonar.core.config.CorePropertyDefinitions#LEAK_PERIOD}
* - Try to find the matching snapshots from the property
* - If a snapshot is found, a period is set to the repository
*/
public class LoadPeriodsStep implements ComputationStep {
private final AnalysisMetadataHolder analysisMetadataHolder;
private final DbClient dbClient;
private final ConfigurationRepository configRepository;
private final TreeRootHolder treeRootHolder;
private final PeriodHolderImpl periodsHolder;
public LoadPeriodsStep(AnalysisMetadataHolder analysisMetadataHolder, DbClient dbClient,
ConfigurationRepository configurationRepository, TreeRootHolder treeRootHolder, PeriodHolderImpl periodsHolder) {
this.analysisMetadataHolder = analysisMetadataHolder;
this.dbClient = dbClient;
this.configRepository = configurationRepository;
this.treeRootHolder = treeRootHolder;
this.periodsHolder = periodsHolder;
}
@Override
public void execute(ComputationStep.Context context) {
if (analysisMetadataHolder.isFirstAnalysis()) {
return;
}
new DepthTraversalTypeAwareCrawler(
new TypeAwareVisitorAdapter(CrawlerDepthLimit.PROJECT, ComponentVisitor.Order.PRE_ORDER) {
@Override
public void visitProject(Component project) {
execute(project);
}
}).visit(treeRootHolder.getRoot());
}
public void execute(Component projectOrView) {
try (DbSession dbSession = dbClient.openSession(false)) {
periodsHolder.setPeriod(buildPeriod(projectOrView, dbSession));
}
}
@CheckForNull
private Period buildPeriod(Component projectOrView, DbSession session) {
boolean isReportType = projectOrView.getType().isReportType();
PeriodResolver periodResolver = new PeriodResolver(dbClient, session, projectOrView.getUuid(), analysisMetadataHolder.getAnalysisDate(),
isReportType ? projectOrView.getReportAttributes().getVersion() : null);
Configuration config = configRepository.getConfiguration();
Period period = periodResolver.resolve(config);
// SONAR-4700 Add a past snapshot only if it exists
if (period != null) {
return period;
}
return null;
}
@Override
public String getDescription() {
return "Load differential periods";
}
}