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;
41 * Will supply the UUID for any component in the tree, given it's key.
43 * The String argument of the {@link Function#apply(Object)} method is the component's key.
46 private final Function<String, String> uuidSupplier;
49 * Will supply the {@link ScannerReport.Component} of all the components in the component tree as we crawl it from the
52 * The Integer argument of the {@link Function#apply(Object)} method is the component's ref.
55 private final Function<Integer, ScannerReport.Component> scannerComponentSupplier;
57 private final Project project;
60 private final SnapshotDto baseAnalysis;
62 public ComponentTreeBuilder(
63 ComponentKeyGenerator keyGenerator,
64 Function<String, String> uuidSupplier,
65 Function<Integer, ScannerReport.Component> scannerComponentSupplier,
67 @Nullable SnapshotDto baseAnalysis) {
69 this.keyGenerator = keyGenerator;
70 this.uuidSupplier = uuidSupplier;
71 this.scannerComponentSupplier = scannerComponentSupplier;
72 this.project = project;
73 this.baseAnalysis = baseAnalysis;
76 public Component buildProject(ScannerReport.Component project) {
77 return buildComponent(project, project);
80 private Component[] buildChildren(ScannerReport.Component component, ScannerReport.Component parentModule) {
81 return component.getChildRefList()
83 .map(scannerComponentSupplier::apply)
84 .map(c -> buildComponent(c, parentModule))
85 .toArray(Component[]::new);
88 private ComponentImpl buildComponent(ScannerReport.Component component, ScannerReport.Component closestModule) {
89 switch (component.getType()) {
91 String projectKey = keyGenerator.generateKey(component, null);
92 String uuid = uuidSupplier.apply(projectKey);
93 return ComponentImpl.builder(Component.Type.PROJECT)
96 .setName(nameOfProject(component))
97 .setStatus(convertStatus(component.getStatus()))
98 .setDescription(trimToNull(component.getDescription()))
99 .setReportAttributes(createAttributesBuilder(component)
100 .setVersion(createProjectVersion(component))
102 .addChildren(buildChildren(component, component))
106 String moduleKey = keyGenerator.generateKey(component, null);
107 return ComponentImpl.builder(Component.Type.MODULE)
108 .setUuid(uuidSupplier.apply(moduleKey))
110 .setName(nameOfOthers(component, moduleKey))
111 .setStatus(convertStatus(component.getStatus()))
112 .setDescription(trimToNull(component.getDescription()))
113 .setReportAttributes(createAttributesBuilder(component).build())
114 .addChildren(buildChildren(component, component))
119 String key = keyGenerator.generateKey(closestModule, component);
120 return ComponentImpl.builder(convertDirOrFileType(component.getType()))
121 .setUuid(uuidSupplier.apply(key))
123 .setName(nameOfOthers(component, key))
124 .setStatus(convertStatus(component.getStatus()))
125 .setDescription(trimToNull(component.getDescription()))
126 .setReportAttributes(createAttributesBuilder(component).build())
127 .setFileAttributes(createFileAttributes(component))
128 .addChildren(buildChildren(component, closestModule))
132 throw new IllegalArgumentException(format("Unsupported component type '%s'", component.getType()));
136 private static Component.Status convertStatus(FileStatus status) {
139 return Component.Status.ADDED;
141 return Component.Status.SAME;
143 return Component.Status.CHANGED;
145 return Component.Status.UNAVAILABLE;
148 throw new IllegalArgumentException("Unsupported ComponentType value " + status);
152 private String nameOfProject(ScannerReport.Component component) {
153 String name = trimToNull(component.getName());
157 return project.getName();
160 private static String nameOfOthers(ScannerReport.Component reportComponent, String defaultName) {
161 String name = trimToNull(reportComponent.getName());
162 return name == null ? defaultName : name;
165 private String createProjectVersion(ScannerReport.Component component) {
166 String version = trimToNull(component.getVersion());
167 if (version != null) {
170 if (baseAnalysis != null) {
171 return firstNonNull(baseAnalysis.getVersion(), DEFAULT_PROJECT_VERSION);
173 return DEFAULT_PROJECT_VERSION;
176 private static ReportAttributes.Builder createAttributesBuilder(ScannerReport.Component component) {
177 return ReportAttributes.newBuilder(component.getRef())
178 .setVersion(trimToNull(component.getVersion()))
179 .setPath(trimToNull(component.getPath()));
183 private static FileAttributes createFileAttributes(ScannerReport.Component component) {
184 if (component.getType() != ScannerReport.Component.ComponentType.FILE) {
188 checkArgument(component.getLines() > 0, "File '%s' has no line", component.getPath());
189 return new FileAttributes(
190 component.getIsTest(),
191 trimToNull(component.getLanguage()),
192 component.getLines());
195 private static Component.Type convertDirOrFileType(ScannerReport.Component.ComponentType type) {
198 return Component.Type.DIRECTORY;
200 return Component.Type.FILE;
202 throw new IllegalArgumentException("Unsupported ComponentType value " + type);