]> source.dussan.org Git - sonarqube.git/blob
0b322adf7491f33cad88aad65c3a5af36276b40f
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.computation.task.projectanalysis.step;
21
22 import com.google.common.collect.Iterables;
23 import javax.annotation.CheckForNull;
24 import javax.annotation.Nullable;
25 import org.sonar.core.component.ComponentKeys;
26 import org.sonar.db.DbClient;
27 import org.sonar.db.DbSession;
28 import org.sonar.db.component.SnapshotDto;
29 import org.sonar.db.component.SnapshotQuery;
30 import org.sonar.scanner.protocol.output.ScannerReport;
31 import org.sonar.server.computation.task.projectanalysis.analysis.MutableAnalysisMetadataHolder;
32 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
33 import org.sonar.server.computation.task.projectanalysis.component.Component;
34 import org.sonar.server.computation.task.projectanalysis.component.ComponentImpl;
35 import org.sonar.server.computation.task.projectanalysis.component.MutableTreeRootHolder;
36 import org.sonar.server.computation.task.projectanalysis.component.UuidFactory;
37 import org.sonar.server.computation.task.projectanalysis.analysis.Analysis;
38 import org.sonar.server.computation.task.step.ComputationStep;
39
40 import static com.google.common.collect.Iterables.toArray;
41 import static org.sonar.server.computation.task.projectanalysis.component.ComponentImpl.builder;
42
43 /**
44  * Populates the {@link MutableTreeRootHolder} and {@link MutableAnalysisMetadataHolder} from the {@link BatchReportReader}
45  */
46 public class BuildComponentTreeStep implements ComputationStep {
47
48   private final DbClient dbClient;
49   private final BatchReportReader reportReader;
50   private final MutableTreeRootHolder treeRootHolder;
51   private final MutableAnalysisMetadataHolder analysisMetadataHolder;
52
53   public BuildComponentTreeStep(DbClient dbClient, BatchReportReader reportReader, MutableTreeRootHolder treeRootHolder, MutableAnalysisMetadataHolder analysisMetadataHolder) {
54     this.dbClient = dbClient;
55     this.reportReader = reportReader;
56     this.treeRootHolder = treeRootHolder;
57     this.analysisMetadataHolder = analysisMetadataHolder;
58   }
59
60   @Override
61   public void execute() {
62     String branch = analysisMetadataHolder.getBranch();
63     ScannerReport.Component reportProject = reportReader.readComponent(analysisMetadataHolder.getRootComponentRef());
64     UuidFactory uuidFactory = new UuidFactory(dbClient, moduleKey(reportProject, branch));
65     Component project = new ComponentRootBuilder(reportProject, uuidFactory, branch).build();
66     treeRootHolder.setRoot(project);
67     setBaseAnalysis(project.getUuid());
68   }
69
70   private void setBaseAnalysis(String projectUuid) {
71     DbSession dbSession = dbClient.openSession(false);
72     try {
73       SnapshotDto snapshotDto = dbClient.snapshotDao().selectAnalysisByQuery(dbSession,
74         new SnapshotQuery()
75           .setComponentUuid(projectUuid)
76           .setIsLast(true));
77       analysisMetadataHolder.setBaseAnalysis(toAnalysis(snapshotDto));
78     } finally {
79       dbClient.closeSession(dbSession);
80     }
81   }
82
83   @CheckForNull
84   private static Analysis toAnalysis(@Nullable SnapshotDto snapshotDto) {
85     return snapshotDto == null ? null : new Analysis.Builder()
86       .setId(snapshotDto.getId())
87       .setUuid(snapshotDto.getUuid())
88       .setCreatedAt(snapshotDto.getCreatedAt())
89       .build();
90   }
91
92   private class ComponentRootBuilder {
93
94     private final ScannerReport.Component reportProject;
95
96     private final UuidFactory uuidFactory;
97
98     @CheckForNull
99     private final String branch;
100
101     public ComponentRootBuilder(ScannerReport.Component reportProject, UuidFactory uuidFactory, @Nullable String branch) {
102       this.reportProject = reportProject;
103       this.uuidFactory = uuidFactory;
104       this.branch = branch;
105     }
106
107     private Component build() {
108       return buildComponent(reportProject, moduleKey(reportProject, branch));
109     }
110
111     private ComponentImpl buildComponent(ScannerReport.Component reportComponent, String latestModuleKey) {
112       switch (reportComponent.getType()) {
113         case PROJECT:
114         case MODULE:
115           String moduleKey = moduleKey(reportComponent, branch);
116           return buildComponent(reportComponent, moduleKey, moduleKey);
117         case DIRECTORY:
118         case FILE:
119           return buildComponent(reportComponent, ComponentKeys.createEffectiveKey(latestModuleKey, reportComponent.getPath()), latestModuleKey);
120         default:
121           throw new IllegalStateException(String.format("Unsupported component type '%s'", reportComponent.getType()));
122       }
123     }
124
125     private ComponentImpl buildComponent(ScannerReport.Component reportComponent, String componentKey, String latestModuleKey) {
126       return builder(reportComponent)
127         .addChildren(toArray(buildChildren(reportComponent, latestModuleKey), Component.class))
128         .setKey(componentKey)
129         .setUuid(uuidFactory.getOrCreateForKey(componentKey))
130         .build();
131     }
132
133     private Iterable<Component> buildChildren(ScannerReport.Component component, final String latestModuleKey) {
134       return Iterables.transform(
135         component.getChildRefList(),
136         componentRef -> buildComponent(reportReader.readComponent(componentRef), latestModuleKey));
137     }
138   }
139
140   private static String moduleKey(ScannerReport.Component reportComponent, @Nullable String branch) {
141     return ComponentKeys.createKey(reportComponent.getKey(), branch);
142   }
143
144   @Override
145   public String getDescription() {
146     return "Build tree of components";
147   }
148 }