Skip to content

Commit 09e18b7

Browse files
fix: tree sorting for oss (#228)
Co-authored-by: DariusZdroba <darius-beniamin.zdroba@snyk.io>
1 parent cda022e commit 09e18b7

2 files changed

Lines changed: 13 additions & 51 deletions

File tree

plugin/src/main/java/io/snyk/languageserver/SnykIssueCache.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public Collection<Issue> getIssues(String path, String displayProduct) {
7777
* @param issues The collection of issues to add
7878
*/
7979
public void addCodeIssues(String path, Collection<Issue> issues) {
80-
var qualityIssues = new TreeSet<Issue>(new IssueComparator(issues));
81-
var securityIssues = new TreeSet<Issue>(new IssueComparator(issues));
80+
var qualityIssues = new TreeSet<Issue>(new IssueComparator());
81+
var securityIssues = new TreeSet<Issue>(new IssueComparator());
8282
for (Issue issue : issues) {
8383
if (issue.additionalData().isSecurityType()) {
8484
securityIssues.add(issue);
@@ -140,7 +140,9 @@ public void removeCodeIssuesForPath(String path) {
140140
*/
141141
public void addOssIssues(String path, Collection<Issue> issues) {
142142
if (issues.size() > 0) {
143-
ossIssues.put(path, new TreeSet<Issue>(new IssueComparator(issues)));
143+
var treeSet = new TreeSet<Issue>(new IssueComparator());
144+
treeSet.addAll(issues);
145+
ossIssues.put(path, treeSet);
144146
} else {
145147
ossIssues.remove(path);
146148
}
@@ -175,7 +177,9 @@ public void removeOssIssuesForPath(String path) {
175177
*/
176178
public void addIacIssues(String path, Collection<Issue> issues) {
177179
if (issues.size() > 0) {
178-
iacIssues.put(path, new TreeSet<Issue>(new IssueComparator(issues)));
180+
var treeSet = new TreeSet<Issue>(new IssueComparator());
181+
treeSet.addAll(issues);
182+
iacIssues.put(path, treeSet);
179183
} else {
180184
iacIssues.remove(path);
181185
}

plugin/src/main/java/io/snyk/languageserver/protocolextension/messageObjects/scanResults/IssueComparator.java

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,29 @@
11
package io.snyk.languageserver.protocolextension.messageObjects.scanResults;
22

3-
import java.util.*;
3+
import java.util.Comparator;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
47
import io.snyk.eclipse.plugin.domain.ProductConstants;
58

69
public class IssueComparator implements Comparator<Issue> {
7-
8-
private final Map<Issue, List<Issue>> issuesGrouped;
9-
10-
public IssueComparator() {
11-
this.issuesGrouped = new HashMap<>();
12-
}
13-
14-
public IssueComparator(Collection<Issue> issues) {
15-
this.issuesGrouped = new HashMap<>();
16-
for (Issue issue : issues) {
17-
this.issuesGrouped.computeIfAbsent(issue, k -> new ArrayList<>()).add(issue);
18-
}
19-
}
20-
2110
@Override
2211
public int compare(Issue o1, Issue o2) {
2312
Map<String, Integer> severityOrder = getSeverityOrderHashMap();
2413

25-
// Get ranks for the severities of the two issues
2614
int rank1 = severityOrder.getOrDefault(o1.severity().toLowerCase(), Integer.MAX_VALUE);
2715
int rank2 = severityOrder.getOrDefault(o2.severity().toLowerCase(), Integer.MAX_VALUE);
2816

29-
// Compare based on severity rank (lower rank = higher severity)
17+
// Compare based on severity rank
3018
int severityComparison = Integer.compare(rank1, rank2);
3119
if (severityComparison != 0) {
3220
return severityComparison;
3321
}
3422

35-
// Fallback: Compare by issue counts grouped by severity (cascading)
36-
int o1Criticals = getCount(o1, ProductConstants.SEVERITY_CRITICAL);
37-
int o2Criticals = getCount(o2, ProductConstants.SEVERITY_CRITICAL);
38-
39-
int o1Highs = getCount(o1, ProductConstants.SEVERITY_HIGH);
40-
int o2Highs = getCount(o2, ProductConstants.SEVERITY_HIGH);
41-
42-
int o1Mediums = getCount(o1, ProductConstants.SEVERITY_MEDIUM);
43-
int o2Mediums = getCount(o2, ProductConstants.SEVERITY_MEDIUM);
44-
45-
int o1Lows = getCount(o1, ProductConstants.SEVERITY_LOW);
46-
int o2Lows = getCount(o2, ProductConstants.SEVERITY_LOW);
47-
48-
if (o1Criticals != o2Criticals) {
49-
return Integer.compare(o2Criticals, o1Criticals);
50-
} else if (o1Highs != o2Highs) {
51-
return Integer.compare(o2Highs, o1Highs);
52-
} else if (o1Mediums != o2Mediums) {
53-
return Integer.compare(o2Mediums, o1Mediums);
54-
} else if (o1Lows != o2Lows) {
55-
return Integer.compare(o2Lows, o1Lows);
56-
}
57-
5823
// Fallback to comparing by hash codes if everything else is equal
5924
return Integer.compare(o1.hashCode(), o2.hashCode());
6025
}
6126

62-
private int getCount(Issue issue, String severity) {
63-
List<Issue> issuesForType = issuesGrouped.getOrDefault(issue, Collections.emptyList());
64-
return (int) issuesForType.stream()
65-
.filter(i -> severity.equalsIgnoreCase(i.severity()))
66-
.count();
67-
}
68-
6927
private static Map<String, Integer> getSeverityOrderHashMap() {
7028
Map<String, Integer> severityOrder = new HashMap<>();
7129
severityOrder.put(ProductConstants.SEVERITY_CRITICAL.toLowerCase(), 1);

0 commit comments

Comments
 (0)