3 * Copyright (C) 2009-2023 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 org.sonar.api.utils.System2;
23 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
24 import org.sonar.ce.task.projectanalysis.component.Component;
25 import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
26 import org.sonar.ce.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
27 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
28 import org.sonar.ce.task.projectanalysis.component.TypeAwareVisitorAdapter;
29 import org.sonar.ce.task.projectanalysis.period.Period;
30 import org.sonar.ce.task.projectanalysis.period.PeriodHolder;
31 import org.sonar.ce.task.step.ComputationStep;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.component.SnapshotDto;
36 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
41 public class PersistAnalysisStep implements ComputationStep {
43 private final System2 system2;
44 private final DbClient dbClient;
45 private final TreeRootHolder treeRootHolder;
46 private final AnalysisMetadataHolder analysisMetadataHolder;
47 private final PeriodHolder periodHolder;
49 public PersistAnalysisStep(System2 system2, DbClient dbClient, TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder, PeriodHolder periodHolder) {
50 this.system2 = system2;
51 this.dbClient = dbClient;
52 this.treeRootHolder = treeRootHolder;
53 this.analysisMetadataHolder = analysisMetadataHolder;
54 this.periodHolder = periodHolder;
58 public void execute(ComputationStep.Context context) {
59 try (DbSession dbSession = dbClient.openSession(false)) {
60 new DepthTraversalTypeAwareCrawler(
61 new PersistSnapshotsPathAwareVisitor(dbSession, analysisMetadataHolder.getAnalysisDate()))
62 .visit(treeRootHolder.getRoot());
67 private class PersistSnapshotsPathAwareVisitor extends TypeAwareVisitorAdapter {
69 private final DbSession dbSession;
70 private final long analysisDate;
72 public PersistSnapshotsPathAwareVisitor(DbSession dbSession, long analysisDate) {
73 super(CrawlerDepthLimit.ROOTS, Order.PRE_ORDER);
74 this.dbSession = dbSession;
75 this.analysisDate = analysisDate;
79 public void visitProject(Component project) {
80 SnapshotDto snapshot = createAnalysis(analysisMetadataHolder.getUuid(), project);
81 updateSnapshotPeriods(snapshot);
82 persist(snapshot, dbSession);
86 public void visitView(Component view) {
87 SnapshotDto snapshot = createAnalysis(analysisMetadataHolder.getUuid(), view);
88 updateSnapshotPeriods(snapshot);
89 persist(snapshot, dbSession);
92 private void updateSnapshotPeriods(SnapshotDto snapshotDto) {
93 if (!periodHolder.hasPeriod()) {
96 Period period = periodHolder.getPeriod();
97 snapshotDto.setPeriodMode(period.getMode());
98 snapshotDto.setPeriodParam(period.getModeParameter());
99 snapshotDto.setPeriodDate(period.getDate());
102 private SnapshotDto createAnalysis(String snapshotUuid, Component component) {
103 String componentUuid = component.getUuid();
104 String projectVersion = component.getType() == PROJECT ? component.getProjectAttributes().getProjectVersion() : null;
105 String buildString = component.getType() == PROJECT ? component.getProjectAttributes().getBuildString().orElse(null) : null;
106 SnapshotDto dto = new SnapshotDto()
107 .setUuid(snapshotUuid)
108 .setProjectVersion(projectVersion)
109 .setBuildString(buildString)
110 .setRootComponentUuid(componentUuid)
112 .setStatus(SnapshotDto.STATUS_UNPROCESSED)
113 .setCreatedAt(analysisDate)
114 .setBuildDate(system2.now());
116 if (component.getType() == PROJECT) {
117 component.getProjectAttributes().getScmRevisionId().ifPresent(dto::setRevision);
123 private void persist(SnapshotDto snapshotDto, DbSession dbSession) {
124 dbClient.snapshotDao().insert(dbSession, snapshotDto);
129 public String getDescription() {
130 return "Persist analysis";