3 * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.component;
22 import java.util.List;
23 import java.util.function.Function;
24 import java.util.stream.Collectors;
25 import javax.annotation.CheckForNull;
26 import javax.annotation.Nullable;
27 import org.sonar.db.component.SnapshotDto;
28 import org.sonar.scanner.protocol.output.ScannerReport;
29 import org.sonar.scanner.protocol.output.ScannerReport.Component.FileStatus;
30 import org.sonar.server.computation.task.projectanalysis.analysis.Project;
32 import static com.google.common.base.MoreObjects.firstNonNull;
33 import static com.google.common.base.Preconditions.checkArgument;
34 import static java.lang.String.format;
35 import static org.apache.commons.lang.StringUtils.trimToNull;
37 public class ComponentTreeBuilder {
39 private static final String DEFAULT_PROJECT_VERSION = "not provided";
41 private final ComponentKeyGenerator keyGenerator;
42 private final ComponentKeyGenerator publicKeyGenerator;
44 * Will supply the UUID for any component in the tree, given it's key.
46 * The String argument of the {@link Function#apply(Object)} method is the component's key.
49 private final Function<String, String> uuidSupplier;
52 * Will supply the {@link ScannerReport.Component} of all the components in the component tree as we crawl it from the
55 * The Integer argument of the {@link Function#apply(Object)} method is the component's ref.
58 private final Function<Integer, ScannerReport.Component> scannerComponentSupplier;
60 private final Project project;
63 private final SnapshotDto baseAnalysis;
65 public ComponentTreeBuilder(
66 ComponentKeyGenerator keyGenerator,
67 ComponentKeyGenerator publicKeyGenerator,
68 Function<String, String> uuidSupplier,
69 Function<Integer, ScannerReport.Component> scannerComponentSupplier,
71 @Nullable SnapshotDto baseAnalysis) {
73 this.keyGenerator = keyGenerator;
74 this.publicKeyGenerator = publicKeyGenerator;
75 this.uuidSupplier = uuidSupplier;
76 this.scannerComponentSupplier = scannerComponentSupplier;
77 this.project = project;
78 this.baseAnalysis = baseAnalysis;
81 public Component buildProject(ScannerReport.Component project) {
82 return buildComponent(project, project);
85 private List<Component> buildChildren(ScannerReport.Component component, ScannerReport.Component parentModule) {
86 return component.getChildRefList()
88 .map(scannerComponentSupplier::apply)
89 .map(c -> buildComponent(c, parentModule))
90 .collect(Collectors.toList());
93 private ComponentImpl buildComponent(ScannerReport.Component component, ScannerReport.Component closestModule) {
94 switch (component.getType()) {
96 String projectKey = keyGenerator.generateKey(component, null);
97 String uuid = uuidSupplier.apply(projectKey);
98 String projectPublicKey = publicKeyGenerator.generateKey(component, null);
99 return ComponentImpl.builder(Component.Type.PROJECT)
102 .setPublicKey(projectPublicKey)
103 .setName(nameOfProject(component))
104 .setStatus(convertStatus(component.getStatus()))
105 .setDescription(trimToNull(component.getDescription()))
106 .setReportAttributes(createAttributesBuilder(component)
107 .setVersion(createProjectVersion(component))
109 .addChildren(buildChildren(component, component))
113 String moduleKey = keyGenerator.generateKey(component, null);
114 String modulePublicKey = publicKeyGenerator.generateKey(component, null);
115 return ComponentImpl.builder(Component.Type.MODULE)
116 .setUuid(uuidSupplier.apply(moduleKey))
118 .setPublicKey(modulePublicKey)
119 .setName(nameOfOthers(component, modulePublicKey))
120 .setStatus(convertStatus(component.getStatus()))
121 .setDescription(trimToNull(component.getDescription()))
122 .setReportAttributes(createAttributesBuilder(component).build())
123 .addChildren(buildChildren(component, component))
128 String key = keyGenerator.generateKey(closestModule, component);
129 String publicKey = publicKeyGenerator.generateKey(closestModule, component);
130 return ComponentImpl.builder(convertDirOrFileType(component.getType()))
131 .setUuid(uuidSupplier.apply(key))
133 .setPublicKey(publicKey)
134 .setName(nameOfOthers(component, publicKey))
135 .setStatus(convertStatus(component.getStatus()))
136 .setDescription(trimToNull(component.getDescription()))
137 .setReportAttributes(createAttributesBuilder(component).build())
138 .setFileAttributes(createFileAttributes(component))
139 .addChildren(buildChildren(component, closestModule))
143 throw new IllegalArgumentException(format("Unsupported component type '%s'", component.getType()));
147 private static Component.Status convertStatus(FileStatus status) {
150 return Component.Status.ADDED;
152 return Component.Status.SAME;
154 return Component.Status.CHANGED;
156 return Component.Status.UNAVAILABLE;
159 throw new IllegalArgumentException("Unsupported ComponentType value " + status);
163 private String nameOfProject(ScannerReport.Component component) {
164 String name = trimToNull(component.getName());
168 return project.getName();
171 private static String nameOfOthers(ScannerReport.Component reportComponent, String defaultName) {
172 String name = trimToNull(reportComponent.getName());
173 return name == null ? defaultName : name;
176 private String createProjectVersion(ScannerReport.Component component) {
177 String version = trimToNull(component.getVersion());
178 if (version != null) {
181 if (baseAnalysis != null) {
182 return firstNonNull(baseAnalysis.getVersion(), DEFAULT_PROJECT_VERSION);
184 return DEFAULT_PROJECT_VERSION;
187 private static ReportAttributes.Builder createAttributesBuilder(ScannerReport.Component component) {
188 return ReportAttributes.newBuilder(component.getRef())
189 .setVersion(trimToNull(component.getVersion()))
190 .setPath(trimToNull(component.getPath()));
194 private static FileAttributes createFileAttributes(ScannerReport.Component component) {
195 if (component.getType() != ScannerReport.Component.ComponentType.FILE) {
199 checkArgument(component.getLines() > 0, "File '%s' has no line", component.getPath());
200 return new FileAttributes(
201 component.getIsTest(),
202 trimToNull(component.getLanguage()),
203 component.getLines());
206 private static Component.Type convertDirOrFileType(ScannerReport.Component.ComponentType type) {
209 return Component.Type.DIRECTORY;
211 return Component.Type.FILE;
213 throw new IllegalArgumentException("Unsupported ComponentType value " + type);