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.function.Function;
23 import javax.annotation.CheckForNull;
24 import javax.annotation.Nullable;
25 import org.sonar.db.component.SnapshotDto;
26 import org.sonar.scanner.protocol.output.ScannerReport;
27 import org.sonar.scanner.protocol.output.ScannerReport.Component.FileStatus;
28 import org.sonar.server.computation.task.projectanalysis.analysis.Project;
30 import static com.google.common.base.MoreObjects.firstNonNull;
31 import static com.google.common.base.Preconditions.checkArgument;
32 import static java.lang.String.format;
33 import static org.apache.commons.lang.StringUtils.trimToNull;
35 public class ComponentTreeBuilder {
37 private static final String DEFAULT_PROJECT_VERSION = "not provided";
39 private final ComponentKeyGenerator keyGenerator;
40 private final ComponentKeyGenerator publicKeyGenerator;
42 * Will supply the UUID for any component in the tree, given it's key.
44 * The String argument of the {@link Function#apply(Object)} method is the component's key.
47 private final Function<String, String> uuidSupplier;
50 * Will supply the {@link ScannerReport.Component} of all the components in the component tree as we crawl it from the
53 * The Integer argument of the {@link Function#apply(Object)} method is the component's ref.
56 private final Function<Integer, ScannerReport.Component> scannerComponentSupplier;
58 private final Project project;
61 private final SnapshotDto baseAnalysis;
63 public ComponentTreeBuilder(
64 ComponentKeyGenerator keyGenerator,
65 ComponentKeyGenerator publicKeyGenerator,
66 Function<String, String> uuidSupplier,
67 Function<Integer, ScannerReport.Component> scannerComponentSupplier,
69 @Nullable SnapshotDto baseAnalysis) {
71 this.keyGenerator = keyGenerator;
72 this.publicKeyGenerator = publicKeyGenerator;
73 this.uuidSupplier = uuidSupplier;
74 this.scannerComponentSupplier = scannerComponentSupplier;
75 this.project = project;
76 this.baseAnalysis = baseAnalysis;
79 public Component buildProject(ScannerReport.Component project) {
80 return buildComponent(project, project);
83 private Component[] buildChildren(ScannerReport.Component component, ScannerReport.Component parentModule) {
84 return component.getChildRefList()
86 .map(scannerComponentSupplier::apply)
87 .map(c -> buildComponent(c, parentModule))
88 .toArray(Component[]::new);
91 private ComponentImpl buildComponent(ScannerReport.Component component, ScannerReport.Component closestModule) {
92 switch (component.getType()) {
94 String projectKey = keyGenerator.generateKey(component, null);
95 String uuid = uuidSupplier.apply(projectKey);
96 return ComponentImpl.builder(Component.Type.PROJECT)
99 .setPublicKey(publicKeyGenerator.generateKey(component, null))
100 .setName(nameOfProject(component))
101 .setStatus(convertStatus(component.getStatus()))
102 .setDescription(trimToNull(component.getDescription()))
103 .setReportAttributes(createAttributesBuilder(component)
104 .setVersion(createProjectVersion(component))
106 .addChildren(buildChildren(component, component))
110 String moduleKey = keyGenerator.generateKey(component, null);
111 return ComponentImpl.builder(Component.Type.MODULE)
112 .setUuid(uuidSupplier.apply(moduleKey))
114 .setPublicKey(publicKeyGenerator.generateKey(component, null))
115 .setName(nameOfOthers(component, moduleKey))
116 .setStatus(convertStatus(component.getStatus()))
117 .setDescription(trimToNull(component.getDescription()))
118 .setReportAttributes(createAttributesBuilder(component).build())
119 .addChildren(buildChildren(component, component))
124 String key = keyGenerator.generateKey(closestModule, component);
125 return ComponentImpl.builder(convertDirOrFileType(component.getType()))
126 .setUuid(uuidSupplier.apply(key))
128 .setPublicKey(publicKeyGenerator.generateKey(closestModule, component))
129 .setName(nameOfOthers(component, key))
130 .setStatus(convertStatus(component.getStatus()))
131 .setDescription(trimToNull(component.getDescription()))
132 .setReportAttributes(createAttributesBuilder(component).build())
133 .setFileAttributes(createFileAttributes(component))
134 .addChildren(buildChildren(component, closestModule))
138 throw new IllegalArgumentException(format("Unsupported component type '%s'", component.getType()));
142 private static Component.Status convertStatus(FileStatus status) {
145 return Component.Status.ADDED;
147 return Component.Status.SAME;
149 return Component.Status.CHANGED;
151 return Component.Status.UNAVAILABLE;
154 throw new IllegalArgumentException("Unsupported ComponentType value " + status);
158 private String nameOfProject(ScannerReport.Component component) {
159 String name = trimToNull(component.getName());
163 return project.getName();
166 private static String nameOfOthers(ScannerReport.Component reportComponent, String defaultName) {
167 String name = trimToNull(reportComponent.getName());
168 return name == null ? defaultName : name;
171 private String createProjectVersion(ScannerReport.Component component) {
172 String version = trimToNull(component.getVersion());
173 if (version != null) {
176 if (baseAnalysis != null) {
177 return firstNonNull(baseAnalysis.getVersion(), DEFAULT_PROJECT_VERSION);
179 return DEFAULT_PROJECT_VERSION;
182 private static ReportAttributes.Builder createAttributesBuilder(ScannerReport.Component component) {
183 return ReportAttributes.newBuilder(component.getRef())
184 .setVersion(trimToNull(component.getVersion()))
185 .setPath(trimToNull(component.getPath()));
189 private static FileAttributes createFileAttributes(ScannerReport.Component component) {
190 if (component.getType() != ScannerReport.Component.ComponentType.FILE) {
194 checkArgument(component.getLines() > 0, "File '%s' has no line", component.getPath());
195 return new FileAttributes(
196 component.getIsTest(),
197 trimToNull(component.getLanguage()),
198 component.getLines());
201 private static Component.Type convertDirOrFileType(ScannerReport.Component.ComponentType type) {
204 return Component.Type.DIRECTORY;
206 return Component.Type.FILE;
208 throw new IllegalArgumentException("Unsupported ComponentType value " + type);