Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: summary count unique transitive dependencies #135

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

Expand All @@ -39,6 +41,7 @@
import com.redhat.exhort.api.DependenciesSummary;
import com.redhat.exhort.api.DependencyReport;
import com.redhat.exhort.api.Issue;
import com.redhat.exhort.api.PackageRef;
import com.redhat.exhort.api.Remediation;
import com.redhat.exhort.api.Summary;
import com.redhat.exhort.api.TransitiveDependencyReport;
Expand All @@ -61,15 +64,21 @@ public AnalysisReport transform(@Body GraphRequest request) {
List<DependencyReport> depsReport = new ArrayList<>();
Collection<DirectDependency> direct = request.tree().dependencies().values();
VulnerabilityCounter counter = new VulnerabilityCounter();
Set<PackageRef> uniqueDeps = new HashSet<>();

direct.forEach(
d -> {
if (uniqueDeps.contains(d.ref())) {
return;
}
uniqueDeps.add(d.ref());
List<Issue> issues = request.issues().get(d.ref().name());
if (issues == null) {
issues = Collections.emptyList();
}
List<TransitiveDependencyReport> transitiveReport =
getTransitiveDependenciesReport(d, request);
updateVulnerabilitySummary(issues, transitiveReport, counter);
updateVulnerabilitySummary(issues, transitiveReport, counter, uniqueDeps);
Optional<Issue> highestVulnerability = issues.stream().findFirst();
Optional<Issue> highestTransitive =
transitiveReport.stream()
Expand Down Expand Up @@ -121,11 +130,16 @@ public AnalysisReport transform(@Body GraphRequest request) {
private void updateVulnerabilitySummary(
List<Issue> issues,
List<TransitiveDependencyReport> transitiveReport,
VulnerabilityCounter counter) {
VulnerabilityCounter counter,
Set<PackageRef> uniqueDeps) {

issues.forEach(i -> incrementCounter(i, counter));
transitiveReport.forEach(
tr -> {
tr.getIssues().forEach(i -> incrementCounter(i, counter));
if (!uniqueDeps.contains(tr.getRef())) {
uniqueDeps.add(tr.getRef());
tr.getIssues().forEach(i -> incrementCounter(i, counter));
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,35 @@ public void testFilterTransitiveWithoutIssues() {
assertEquals(1, report.getDependencies().get(1).getTransitive().size());
}

@Test
public void testIgnoreDuplicates() {
Map<String, List<Issue>> issues =
Map.of(
"aa", List.of(buildIssue(1, 5f)),
"aaa", List.of(buildIssue(2, 5f)));
GraphRequest req =
new GraphRequest.Builder(Constants.NPM_PKG_MANAGER, List.of(Constants.SNYK_PROVIDER))
.tree(buildTreeWithDuplicates())
.issues(issues)
.build();

AnalysisReport report = new ReportTransformer().transform(req);

assertNotNull(report);
assertEquals(2, report.getDependencies().size());

assertTrue(report.getDependencies().stream().anyMatch(d -> d.getRef().name().equals("aa")));
assertTrue(report.getDependencies().stream().anyMatch(d -> d.getRef().name().equals("aa")));

assertEquals(1, report.getDependencies().get(0).getTransitive().size());
assertEquals(1, report.getDependencies().get(1).getTransitive().size());

assertEquals(2, report.getSummary().getDependencies().getScanned());
assertEquals(4, report.getSummary().getDependencies().getTransitive());

assertEquals(2, report.getSummary().getVulnerabilities().getTotal());
}

@Test
public void testFilterRecommendations() {
Map<String, PackageRef> recommendations =
Expand Down Expand Up @@ -172,6 +201,70 @@ private DependencyTree buildTree() {
.build();
}

private DependencyTree buildTreeWithDuplicates() {
Map<PackageRef, DirectDependency> direct =
Map.of(
PackageRef.builder()
.name("aa")
.version("1")
.pkgManager(Constants.NPM_PKG_MANAGER)
.build(),
DirectDependency.builder()
.ref(
PackageRef.builder()
.name("aa")
.version("1")
.pkgManager(Constants.NPM_PKG_MANAGER)
.build())
.transitive(
Set.of(
PackageRef.builder()
.name("aaa")
.version("1")
.pkgManager(Constants.NPM_PKG_MANAGER)
.build(),
PackageRef.builder()
.name("aab")
.version("1")
.pkgManager(Constants.NPM_PKG_MANAGER)
.build()))
.build(),
PackageRef.builder()
.name("ab")
.version("1")
.pkgManager(Constants.NPM_PKG_MANAGER)
.build(),
DirectDependency.builder()
.ref(
PackageRef.builder()
.name("ab")
.version("1")
.pkgManager(Constants.NPM_PKG_MANAGER)
.build())
.transitive(
Set.of(
PackageRef.builder()
.name("aaa")
.version("1")
.pkgManager(Constants.NPM_PKG_MANAGER)
.build(),
PackageRef.builder()
.name("abb")
.version("1")
.pkgManager(Constants.NPM_PKG_MANAGER)
.build()))
.build());
return DependencyTree.builder()
.root(
PackageRef.builder()
.name("a")
.version("1")
.pkgManager(Constants.NPM_PKG_MANAGER)
.build())
.dependencies(direct)
.build();
}

private Issue buildIssue(int id, Float score) {
return new Issue()
.id(String.format("ISSUE-00%d", id))
Expand Down