forked from jfrog/build-info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DependencyTree.java
255 lines (219 loc) · 7.52 KB
/
DependencyTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package org.jfrog.build.extractor.scan;
import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.StringUtils;
import org.jfrog.build.api.util.Log;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import java.util.*;
import java.util.stream.Collectors;
/**
* Dependency tree for Xray scan. Used in 'Eclipse' and 'Idea' Xray plugins.
*
* @author yahavi
*/
@JsonFilter("xray-graph-filter")
public class DependencyTree extends DefaultMutableTreeNode {
private Set<License> violatedLicenses = new HashSet<>();
private Set<License> licenses = new HashSet<>();
private Set<Issue> issues = new HashSet<>();
private Set<Scope> scopes = new HashSet<>();
private Issue topIssue = new Issue();
private GeneralInfo generalInfo;
private String packagePrefix = "";
/**
* metadata should be true if one of the following statement is true:
* Node represents the root of the dependency tree.
* Node represents a project.
* Node represents a module in project.
*/
private boolean metadata;
public DependencyTree() {
super();
}
public DependencyTree(Object userObject) {
super(userObject);
}
@SuppressWarnings("unused")
public void setViolatedLicenses(Set<License> violatedLicenses) {
this.violatedLicenses = violatedLicenses;
}
public Set<License> getViolatedLicenses() {
return violatedLicenses;
}
public void setLicenses(Set<License> licenses) {
this.licenses = licenses;
}
public void setIssues(Set<Issue> issues) {
this.issues = issues;
}
@JsonProperty("component_id")
@SuppressWarnings("unused")
public String getComponentId() {
return packagePrefix + this;
}
public void setScopes(Set<Scope> scopes) {
this.scopes = scopes;
}
@SuppressWarnings("unused")
public void setGeneralInfo(GeneralInfo generalInfo) {
this.generalInfo = generalInfo;
}
public Set<License> getLicenses() {
return licenses;
}
public Set<Issue> getIssues() {
return issues;
}
public Set<Scope> getScopes() {
return scopes;
}
@SuppressWarnings("unused")
public GeneralInfo getGeneralInfo() {
return generalInfo;
}
/**
* @return top severity issue of the current node and its ancestors
*/
@SuppressWarnings("WeakerAccess")
public Issue getTopIssue() {
return topIssue;
}
@SuppressWarnings("unused")
public boolean isMetadata() {
return metadata;
}
@SuppressWarnings("unused")
public void setMetadata(boolean metadata) {
this.metadata = metadata;
}
public void setPrefix(String prefix) {
packagePrefix = prefix.toLowerCase() + "://";
getChildren().forEach(node -> ((DependencyTree)node).setPrefix(prefix));
}
/**
* @return total number of issues of the current node and its ancestors
*/
@SuppressWarnings("WeakerAccess")
public int getIssueCount() {
return issues.size();
}
/**
* @return Node's children
*/
@SuppressWarnings({"WeakerAccess", "unchecked"})
public Vector<TreeNode> getChildren() {
return children != null ? children : new Vector<>();
}
@JsonProperty(value = "nodes")
@SuppressWarnings({"unchecked", "unused"})
public List<TreeNode> getNodes() {
return children;
}
/**
* 1. Populate current node's issues components
* 2. Populate current node and subtree's issues
* 3. Populate current node and subtree's top issue
* 4. Sort the tree
*
* @return all issues of the current node and its ancestors
*/
@SuppressWarnings({"WeakerAccess", "unused"})
public Set<Issue> processTreeIssues() {
setIssuesComponent();
getChildren().forEach(child -> issues.addAll(((DependencyTree)child).processTreeIssues()));
setTopIssue();
sortChildren();
return issues;
}
private void setIssuesComponent() {
Object userObject = getUserObject();
if (userObject != null) {
issues.forEach(issue -> issue.setComponent(userObject.toString()));
}
}
private void sortChildren() {
Vector children = getChildren();
Vector<DependencyTree> dependencyTrees = (Vector<DependencyTree>) children;
dependencyTrees.sort(Comparator
.comparing(DependencyTree::getTopIssue, Comparator.comparing(Issue::getSeverity))
.thenComparing(DependencyTree::getIssueCount)
.thenComparing(DependencyTree::getChildCount)
.reversed()
.thenComparing(DependencyTree::toString));
}
private void setTopIssue() {
issues.forEach(issue -> {
if (topIssue.isTopSeverity()) {
return;
}
if (issue.isHigherSeverityThan(topIssue)) {
topIssue = issue;
}
});
}
/**
* 1. Populate current node's licenses components
* 2. Populate current node and subtree's violated licenses
*
* @return all violated licenses of the current node and its ancestors
*/
public Set<License> processTreeViolatedLicenses() {
setViolatedLicensesComponent();
violatedLicenses.addAll(licenses.stream().filter(License::isViolate).collect(Collectors.toSet()));
getChildren().forEach(child -> violatedLicenses.addAll(((DependencyTree)child).processTreeViolatedLicenses()));
return violatedLicenses;
}
private void setViolatedLicensesComponent() {
Object userObject = getUserObject();
if (userObject != null) {
licenses.forEach(license -> license.setComponent(userObject.toString()));
}
}
/**
* Recursively, collect all scopes and licenses.
*
* @param allScopes - Out - All dependency tree scopes
* @param allLicenses - Out - All dependency tree licenses
*/
@SuppressWarnings("unused")
public void collectAllScopesAndLicenses(Set<Scope> allScopes, Set<License> allLicenses) {
Enumeration<?> enumeration = breadthFirstEnumeration();
while (enumeration.hasMoreElements()) {
DependencyTree child = (DependencyTree) enumeration.nextElement();
allScopes.addAll(child.getScopes());
allLicenses.addAll(child.getLicenses());
}
}
/**
* Recursively find a node contains the input component ID.
*
* @param componentId - The component ID to search
* @return a node contains the input component ID or null.
*/
public DependencyTree find(String componentId) {
if (StringUtils.equals(toString(), componentId)) {
return this;
}
return getChildren().stream()
.map(child -> ((DependencyTree)child).find(componentId))
.filter(Objects::nonNull)
.findAny()
.orElse(null);
}
/**
* Return true if the node contains a loop.
*
* @param logger - The logger
* @return true if the node contains a loop
*/
public boolean hasLoop(Log logger) {
for (DependencyTree parent = (DependencyTree) getParent(); parent != null; parent = (DependencyTree) parent.getParent()) {
if (Objects.equals(getUserObject(), parent.getUserObject())) {
logger.debug("Loop detected in " + getUserObject());
return true;
}
}
return false;
}
}