Skip to content

Commit

Permalink
Mark new components as such
Browse files Browse the repository at this point in the history
... via new transient field. Required for compatibility with #217

Signed-off-by: nscuro <nscuro@protonmail.com>
  • Loading branch information
nscuro committed Jul 4, 2023
1 parent bb776de commit d3c7fb4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 48 deletions.
11 changes: 9 additions & 2 deletions src/main/java/org/dependencytrack/model/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,9 @@ public enum FetchGroup {
private transient String licenseId;
private transient DependencyMetrics metrics;
private transient RepositoryMetaComponent repositoryMeta;
private transient boolean isNew;
private transient int usedBy;

private transient Set<String> dependencyGraph;

private transient boolean expandDependencyGraph;

public long getId() {
Expand Down Expand Up @@ -742,6 +741,14 @@ public void setRepositoryMeta(RepositoryMetaComponent repositoryMeta) {
this.repositoryMeta = repositoryMeta;
}

public boolean isNew() {
return isNew;
}

public void setNew(final boolean aNew) {
isNew = aNew;
}

public Double getLastInheritedRiskScore() {
return lastInheritedRiskScore;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

import static org.apache.commons.lang3.StringUtils.trimToNull;
import static org.dependencytrack.util.PurlUtil.silentPurlCoordinatesOnly;
Expand Down Expand Up @@ -53,24 +55,6 @@ public static Project convertToProject(final org.cyclonedx.model.Component cdxCo
return project;
}

public static List<Component> flattenComponents(final Collection<Component> components) {
final var result = new ArrayList<Component>();
if (components == null || components.isEmpty()) {
return Collections.emptyList();
}

for (final Component component : components) {
if (component.getChildren() != null) {
result.addAll(flattenComponents(component.getChildren()));
component.setChildren(null);
}

result.add(component);
}

return result;
}

public static List<Component> convertComponents(final List<org.cyclonedx.model.Component> cdxComponents) {
if (cdxComponents == null || cdxComponents.isEmpty()) {
return Collections.emptyList();
Expand Down Expand Up @@ -156,24 +140,6 @@ public static Component convertComponent(final org.cyclonedx.model.Component cdx
return component;
}

public static List<ServiceComponent> flattenServices(final Collection<ServiceComponent> services) {
final var result = new ArrayList<ServiceComponent>();
if (services == null || services.isEmpty()) {
return Collections.emptyList();
}

for (final ServiceComponent service : services) {
if (service.getChildren() != null) {
result.addAll(flattenServices(service.getChildren()));
service.setChildren(null);
}

result.add(service);
}

return result;
}

public static List<ServiceComponent> convertServices(final List<org.cyclonedx.model.Service> cdxServices) {
if (cdxServices == null || cdxServices.isEmpty()) {
return Collections.emptyList();
Expand Down Expand Up @@ -228,4 +194,24 @@ private static List<ExternalReference> convertExternalReferences(final List<org.
.toList();
}

public static <T> List<T> flatten(final Collection<T> items, final Function<T, Collection<T>> childrenGetter,
final BiConsumer<T, Collection<T>> childrenSetter) {
final var result = new ArrayList<T>();
if (items == null || items.isEmpty()) {
return Collections.emptyList();
}

for (final T item : items) {
final Collection<T> children = childrenGetter.apply(item);
if (children != null) {
result.addAll(flatten(children, childrenGetter, childrenSetter));
childrenSetter.accept(item, null);
}

result.add(item);
}

return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@
import static org.dependencytrack.parser.cyclonedx.ModelConverterX.convertComponents;
import static org.dependencytrack.parser.cyclonedx.ModelConverterX.convertServices;
import static org.dependencytrack.parser.cyclonedx.ModelConverterX.convertToProject;
import static org.dependencytrack.parser.cyclonedx.ModelConverterX.flattenComponents;
import static org.dependencytrack.parser.cyclonedx.ModelConverterX.flattenServices;
import static org.dependencytrack.parser.cyclonedx.ModelConverterX.flatten;
import static org.dependencytrack.util.InternalComponentIdentificationUtil.isInternalComponent;
import static org.dependencytrack.util.PersistenceUtil.applyIfChanged;

Expand Down Expand Up @@ -170,12 +169,14 @@ void process(final Context ctx, final BomUploadEvent event) throws BomProcessing
} else {
metadataComponent = null;
}
final List<Component> components = flattenComponents(convertComponents(cdxBom.getComponents())).stream()
.filter(distinctComponentByIdentity(identitiesByBomRef, bomRefsByIdentity))
.toList();
final List<ServiceComponent> serviceComponents = flattenServices(convertServices(cdxBom.getServices())).stream()
.filter(distinctServiceByIdentity(identitiesByBomRef, bomRefsByIdentity))
.toList();
final List<Component> components =
flatten(convertComponents(cdxBom.getComponents()), Component::getChildren, Component::setChildren).stream()
.filter(distinctComponentByIdentity(identitiesByBomRef, bomRefsByIdentity))
.toList();
final List<ServiceComponent> serviceComponents =
flatten(convertServices(cdxBom.getServices()), ServiceComponent::getChildren, ServiceComponent::setChildren).stream()
.filter(distinctServiceByIdentity(identitiesByBomRef, bomRefsByIdentity))
.toList();

kafkaEventDispatcher.dispatchAsync(ctx.project.getUuid(), new Notification()
.scope(NotificationScope.PORTFOLIO)
Expand Down Expand Up @@ -338,6 +339,8 @@ private static Map<ComponentIdentity, Component> processComponents(final Context
final Set<Long> oldComponentIds = getAllComponentIds(pm, persistentProject);

// Avoid redundant queries by caching resolved licenses.
// It is likely that if license IDs were present in a BOM,
// they appear multiple times for different components.
final var licenseCache = new HashMap<String, License>();

final var persistentComponents = new HashMap<ComponentIdentity, Component>();
Expand All @@ -357,9 +360,8 @@ private static Map<ComponentIdentity, Component> processComponents(final Context
if (persistentComponent == null) {
component.setProject(persistentProject);
persistentComponent = pm.makePersistent(component);
persistentComponent.setNew(true);
isNewOrUpdated = true;

// TODO: Mark as "new"
} else {
// Only call setters when values actually changed. Otherwise, we'll trigger lots of unnecessary
// database calls.
Expand Down

0 comments on commit d3c7fb4

Please sign in to comment.