Skip to content

Commit 78e1425

Browse files
refactor: remove code-duplication [IDE-134] (#481)
* refactor: remove duplication in displaySnykCodeResults [IDE-134] * docs: update changelog * refactor: remove duplication * refactor: reformatting
1 parent 43b83b3 commit 78e1425

File tree

2 files changed

+67
-57
lines changed

2 files changed

+67
-57
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# Snyk Changelog
22

3+
## [2.7.6]
4+
### Fixed
5+
- some code refactorings and code smells
6+
7+
## [2.7.5]
8+
### Fixed
9+
- bump deps
10+
- remove remnants of false-positives
11+
12+
### Added
13+
feat: integrate experimental option to get Snyk Code results from Language Server (pre-alpha) [IDE-134] by @bastiandoetsch in #474
14+
15+
316
## [2.7.4]
417
### Fixed
518
- move all clean-up tasks on project close to a background task and limit execution to 5s

src/main/kotlin/io/snyk/plugin/ui/toolwindow/SnykToolWindowSnykCodeScanListenerLS.kt

Lines changed: 54 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -56,73 +56,70 @@ class SnykToolWindowSnykCodeScanListenerLS(
5656
val selectedNodeUserObject = TreeUtil.findObjectInPath(vulnerabilitiesTree.selectionPath, Any::class.java)
5757

5858
// display Security issues
59-
val userObjectsForExpandedSecurityNodes =
60-
snykToolWindowPanel.userObjectsForExpandedNodes(rootSecurityIssuesTreeNode)
61-
rootSecurityIssuesTreeNode.removeAllChildren()
62-
63-
var securityIssuesCount: Int? = null
64-
var securityIssuesHMLPostfix = ""
65-
if (pluginSettings().snykCodeSecurityIssuesScanEnable) {
66-
val securityResults = snykCodeResults
67-
.map { it.key to it.value.filter { issue -> issue.additionalData.isSecurityType } }
68-
.toMap()
69-
securityIssuesCount = securityResults.values.flatten().distinct().size
70-
securityIssuesHMLPostfix = buildSeveritiesPostfixForFileNode(securityResults)
59+
displayIssues(snykCodeResults, selectedNodeUserObject, true)
7160

72-
if (pluginSettings().treeFiltering.codeSecurityResults) {
73-
val securityResultsToDisplay = securityResults.map { entry ->
74-
entry.key to entry.value
75-
.filter { pluginSettings().hasSeverityEnabledAndFiltered(it.getSeverityAsEnum()) }
76-
}.toMap()
61+
// display Quality (non Security) issues
62+
displayIssues(snykCodeResults, selectedNodeUserObject, false)
63+
}
7764

78-
displayResultsForCodeRoot(
79-
rootSecurityIssuesTreeNode,
80-
securityResultsToDisplay
81-
)
82-
}
65+
private fun displayIssues(
66+
snykCodeResults: Map<SnykCodeFile, List<ScanIssue>>,
67+
selectedNodeUserObject: Any?,
68+
isSecurity: Boolean
69+
) {
70+
val rootNode = if (isSecurity) this.rootSecurityIssuesTreeNode else this.rootQualityIssuesTreeNode
71+
72+
val userObjectsForExpandedNodes =
73+
snykToolWindowPanel.userObjectsForExpandedNodes(rootNode)
74+
75+
rootNode.removeAllChildren()
76+
77+
var issuesCount: Int? = null
78+
var rootNodePostFix = ""
79+
val settings = pluginSettings()
80+
val enabledInSettings = when {
81+
isSecurity -> settings.snykCodeSecurityIssuesScanEnable
82+
else -> settings.snykCodeQualityIssuesScanEnable
8383
}
84-
snykToolWindowPanel.updateTreeRootNodesPresentation(
85-
securityIssuesCount = securityIssuesCount,
86-
addHMLPostfix = securityIssuesHMLPostfix
87-
)
88-
snykToolWindowPanel.smartReloadRootNode(
89-
rootSecurityIssuesTreeNode,
90-
userObjectsForExpandedSecurityNodes,
91-
selectedNodeUserObject
92-
)
9384

94-
// display Quality (non Security) issues
95-
val userObjectsForExpandedQualityNodes =
96-
snykToolWindowPanel.userObjectsForExpandedNodes(rootQualityIssuesTreeNode)
97-
rootQualityIssuesTreeNode.removeAllChildren()
98-
99-
var qualityIssuesCount: Int? = null
100-
var qualityIssuesHMLPostfix = ""
101-
if (pluginSettings().snykCodeQualityIssuesScanEnable) {
102-
val qualityResults = snykCodeResults
103-
.map { it.key to it.value.filter { issue -> !issue.additionalData.isSecurityType } }
85+
if (enabledInSettings) {
86+
val filteredResults = snykCodeResults
87+
.map { it.key to it.value.filter { issue -> issue.additionalData.isSecurityType == isSecurity } }
10488
.toMap()
105-
qualityIssuesCount = qualityResults.values.flatten().distinct().size
106-
qualityIssuesHMLPostfix = buildSeveritiesPostfixForFileNode(qualityResults)
10789

108-
if (pluginSettings().treeFiltering.codeQualityResults) {
109-
val qualityResultsToDisplay = qualityResults.map { entry ->
110-
entry.key to entry.value
111-
.filter { pluginSettings().hasSeverityEnabledAndFiltered(it.getSeverityAsEnum()) }
90+
issuesCount = filteredResults.values.flatten().distinct().size
91+
rootNodePostFix = buildSeveritiesPostfixForFileNode(filteredResults)
92+
93+
val treeFiltering = when {
94+
isSecurity -> settings.treeFiltering.codeSecurityResults
95+
else -> settings.treeFiltering.codeQualityResults
96+
}
97+
98+
if (treeFiltering) {
99+
val resultsToDisplay = filteredResults.map { entry ->
100+
entry.key to entry.value.filter {
101+
settings.hasSeverityEnabledAndFiltered(it.getSeverityAsEnum())
102+
}
112103
}.toMap()
113-
displayResultsForCodeRoot(
114-
rootQualityIssuesTreeNode,
115-
qualityResultsToDisplay
116-
)
104+
displayResultsForCodeRoot(rootNode, resultsToDisplay)
117105
}
118106
}
119-
snykToolWindowPanel.updateTreeRootNodesPresentation(
120-
qualityIssuesCount = qualityIssuesCount,
121-
addHMLPostfix = qualityIssuesHMLPostfix
122-
)
107+
108+
if (isSecurity) {
109+
snykToolWindowPanel.updateTreeRootNodesPresentation(
110+
securityIssuesCount = issuesCount,
111+
addHMLPostfix = rootNodePostFix
112+
)
113+
} else {
114+
snykToolWindowPanel.updateTreeRootNodesPresentation(
115+
qualityIssuesCount = issuesCount,
116+
addHMLPostfix = rootNodePostFix
117+
)
118+
}
119+
123120
snykToolWindowPanel.smartReloadRootNode(
124-
rootQualityIssuesTreeNode,
125-
userObjectsForExpandedQualityNodes,
121+
rootNode,
122+
userObjectsForExpandedNodes,
126123
selectedNodeUserObject
127124
)
128125
}

0 commit comments

Comments
 (0)