Skip to content

Commit

Permalink
SONAR-7861 support unset name of project in scanner report
Browse files Browse the repository at this point in the history
  • Loading branch information
sns-seb committed Aug 30, 2016
1 parent d0c6971 commit 990247c
Show file tree
Hide file tree
Showing 6 changed files with 659 additions and 188 deletions.
Expand Up @@ -19,20 +19,17 @@
*/ */
package org.sonar.server.computation.task.projectanalysis.component; package org.sonar.server.computation.task.projectanalysis.component;


import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
import org.sonar.scanner.protocol.output.ScannerReport;
import org.sonar.scanner.protocol.output.ScannerReport.Component.ComponentType;


import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkState;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static java.util.Objects.requireNonNull;
import static org.apache.commons.lang.StringUtils.trimToNull; import static org.apache.commons.lang.StringUtils.trimToNull;


@Immutable @Immutable
Expand All @@ -53,7 +50,7 @@ public class ComponentImpl implements Component {
private ComponentImpl(Builder builder) { private ComponentImpl(Builder builder) {
this.type = builder.type; this.type = builder.type;
this.key = builder.key; this.key = builder.key;
this.name = builder.name == null ? String.valueOf(builder.key) : builder.name; this.name = builder.name;
this.description = builder.description; this.description = builder.description;
this.uuid = builder.uuid; this.uuid = builder.uuid;
this.reportAttributes = builder.reportAttributes; this.reportAttributes = builder.reportAttributes;
Expand Down Expand Up @@ -108,86 +105,74 @@ public ProjectViewAttributes getProjectViewAttributes() {
throw new IllegalStateException("Only component of type PROJECT_VIEW have a FileAttributes object"); throw new IllegalStateException("Only component of type PROJECT_VIEW have a FileAttributes object");
} }


public static Builder builder(ScannerReport.Component component) { public static Builder builder(Type type) {
return new Builder(component); return new Builder(type);
} }


public static final class Builder { public static final class Builder {


private static final String KEY_CANNOT_BE_NULL = "key can't be null";
private static final String UUID_CANNOT_BE_NULL = "uuid can't be null";
private static final String REPORT_ATTRIBUTES_CANNOT_BE_NULL = "reportAttributes can't be null";
private static final String NAME_CANNOT_BE_NULL = "name can't be null";

private final Type type; private final Type type;
private final ReportAttributes reportAttributes; private ReportAttributes reportAttributes;
private String uuid; private String uuid;
private String key; private String key;
private String name; private String name;
private String description; private String description;
private FileAttributes fileAttributes; private FileAttributes fileAttributes;
private final List<Component> children = new ArrayList<>(); private final List<Component> children = new ArrayList<>();


private Builder(ScannerReport.Component component) { private Builder(Type type){
checkNotNull(component); this.type = requireNonNull(type, "type can't be null");
this.type = convertType(component.getType()); }
this.name = component.getName();
this.description = trimToNull(component.getDescription()); public Builder setReportAttributes(ReportAttributes reportAttributes) {
this.reportAttributes = createBatchAttributes(component); this.reportAttributes = requireNonNull(reportAttributes, REPORT_ATTRIBUTES_CANNOT_BE_NULL);
this.fileAttributes = createFileAttributes(component); return this;
} }


public Builder setUuid(String s) { public Builder setUuid(String s) {
this.uuid = checkNotNull(s); this.uuid = requireNonNull(s, UUID_CANNOT_BE_NULL);
return this; return this;
} }


public Builder setKey(String s) { public Builder setKey(String s) {
this.key = checkNotNull(s); this.key = requireNonNull(s, KEY_CANNOT_BE_NULL);
return this; return this;
} }


public Builder addChildren(Component... c) { public Builder setName(String name) {
for (Component component : c) { this.name = requireNonNull(name, NAME_CANNOT_BE_NULL);
checkArgument(component.getType().isReportType());
}
this.children.addAll(asList(c));
return this; return this;
} }


public ComponentImpl build() { public Builder setDescription(@Nullable String description) {
checkNotNull(key); this.description = trimToNull(description);
checkNotNull(uuid); return this;
return new ComponentImpl(this);
} }


private static ReportAttributes createBatchAttributes(ScannerReport.Component component) { public Builder setFileAttributes(@Nullable FileAttributes fileAttributes) {
return ReportAttributes.newBuilder(component.getRef()) this.fileAttributes = fileAttributes;
.setVersion(trimToNull(component.getVersion())) return this;
.setPath(trimToNull(component.getPath()))
.build();
} }


@CheckForNull public Builder addChildren(Component... c) {
private static FileAttributes createFileAttributes(ScannerReport.Component component) { for (Component component : c) {
if (component.getType() != ComponentType.FILE) { checkArgument(component.getType().isReportType());
return null;
} }

this.children.addAll(asList(c));
return new FileAttributes( return this;
component.getIsTest(),
trimToNull(component.getLanguage()));
} }


@VisibleForTesting public ComponentImpl build() {
static Type convertType(ComponentType type) { requireNonNull(reportAttributes, REPORT_ATTRIBUTES_CANNOT_BE_NULL);
switch (type) { requireNonNull(uuid, UUID_CANNOT_BE_NULL);
case PROJECT: requireNonNull(key, KEY_CANNOT_BE_NULL);
return Type.PROJECT; requireNonNull(name, NAME_CANNOT_BE_NULL);
case MODULE: return new ComponentImpl(this);
return Type.MODULE;
case DIRECTORY:
return Type.DIRECTORY;
case FILE:
return Type.FILE;
default:
throw new IllegalArgumentException("Unsupported ComponentType value " + type);
}
} }
} }


Expand Down
@@ -0,0 +1,147 @@
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.computation.task.projectanalysis.component;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.base.Supplier;
import java.util.function.Function;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.db.component.ComponentDto;
import org.sonar.scanner.protocol.output.ScannerReport;

import static com.google.common.collect.Iterables.toArray;
import static java.lang.String.format;
import static org.apache.commons.lang.StringUtils.trimToNull;
import static org.sonar.core.component.ComponentKeys.createEffectiveKey;
import static org.sonar.core.component.ComponentKeys.createKey;
import static org.sonar.core.util.stream.Collectors.toList;

public class ComponentRootBuilder {
private final Function<String, String> uuidSupplier;
private final Function<Integer, ScannerReport.Component> scannerComponentSupplier;
private final Supplier<Optional<ComponentDto>> projectDtoSupplier;
@CheckForNull
private final String branch;

public ComponentRootBuilder(@Nullable String branch,
Function<String, String> uuidSupplier,
Function<Integer, ScannerReport.Component> scannerComponentSupplier,
Supplier<Optional<ComponentDto>> projectDtoSupplier) {
this.uuidSupplier = uuidSupplier;
this.scannerComponentSupplier = scannerComponentSupplier;
this.projectDtoSupplier = projectDtoSupplier;
this.branch = branch;
}

public Component build(ScannerReport.Component reportProject, String projectKey) {
return buildComponent(reportProject, projectKey);
}

private ComponentImpl buildComponent(ScannerReport.Component reportComponent, String latestModuleKey) {
switch (reportComponent.getType()) {
case PROJECT:
return createCommonBuilder(reportComponent, latestModuleKey, latestModuleKey)
.setName(nameOfProject(reportComponent, latestModuleKey, projectDtoSupplier))
.build();
case MODULE:
String moduleKey = createKey(reportComponent.getKey(), branch);
return buildOtherComponent(reportComponent, moduleKey, moduleKey);
case DIRECTORY:
case FILE:
return buildOtherComponent(reportComponent, createEffectiveKey(latestModuleKey, reportComponent.getPath()), latestModuleKey);
default:
throw new IllegalArgumentException(format("Unsupported component type '%s'", reportComponent.getType()));
}
}

private ComponentImpl buildOtherComponent(ScannerReport.Component reportComponent, String componentKey, String latestModuleKey) {
return createCommonBuilder(reportComponent, componentKey, latestModuleKey)
.setName(nameOfOthers(reportComponent, componentKey))
.build();
}

private ComponentImpl.Builder createCommonBuilder(ScannerReport.Component reportComponent, String componentKey, String latestModuleKey) {
return ComponentImpl.builder(convertType(reportComponent.getType()))
.setUuid(uuidSupplier.apply(componentKey))
.setKey(componentKey)
.setDescription(trimToNull(reportComponent.getDescription()))
.setFileAttributes(createFileAttributes(reportComponent))
.setReportAttributes(createReportAttributes(reportComponent))
.addChildren(toArray(buildChildren(reportComponent, latestModuleKey), Component.class));
}

private Iterable<Component> buildChildren(ScannerReport.Component component, String latestModuleKey) {
return component.getChildRefList()
.stream()
.map(componentRef -> buildComponent(scannerComponentSupplier.apply(componentRef), latestModuleKey))
.collect(toList(component.getChildRefList().size()));
}

private static String nameOfProject(ScannerReport.Component project, String projectKey, Supplier<Optional<ComponentDto>> projectDtoSupplier) {
String name = trimToNull(project.getName());
if (name == null) {
return projectDtoSupplier.get().transform(ComponentDto::name).or(projectKey);
}
return name;
}

private static String nameOfOthers(ScannerReport.Component reportComponent, String componentKey) {
String name = trimToNull(reportComponent.getName());
return name == null ? componentKey : name;
}

@VisibleForTesting
static ReportAttributes createReportAttributes(ScannerReport.Component component) {
return ReportAttributes.newBuilder(component.getRef())
.setPath(trimToNull(component.getPath()))
.setVersion(trimToNull(component.getVersion()))
.build();
}

@VisibleForTesting
@CheckForNull
static FileAttributes createFileAttributes(ScannerReport.Component component) {
if (component.getType() != ScannerReport.Component.ComponentType.FILE) {
return null;
}

return new FileAttributes(
component.getIsTest(),
trimToNull(component.getLanguage()));
}

@VisibleForTesting
static Component.Type convertType(ScannerReport.Component.ComponentType type) {
switch (type) {
case PROJECT:
return Component.Type.PROJECT;
case MODULE:
return Component.Type.MODULE;
case DIRECTORY:
return Component.Type.DIRECTORY;
case FILE:
return Component.Type.FILE;
default:
throw new IllegalArgumentException("Unsupported ComponentType value " + type);
}
}
}

0 comments on commit 990247c

Please sign in to comment.