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

refactor: remove code-duplication [IDE-134] #481

Merged
merged 4 commits into from
Mar 5, 2024
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
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Snyk Changelog

## [2.7.6]
### Fixed
- some code refactorings and code smells

## [2.7.5]
### Fixed
- bump deps
- remove remnants of false-positives

### Added
feat: integrate experimental option to get Snyk Code results from Language Server (pre-alpha) [IDE-134] by @bastiandoetsch in #474


## [2.7.4]
### Fixed
- move all clean-up tasks on project close to a background task and limit execution to 5s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,73 +56,70 @@ class SnykToolWindowSnykCodeScanListenerLS(
val selectedNodeUserObject = TreeUtil.findObjectInPath(vulnerabilitiesTree.selectionPath, Any::class.java)

// display Security issues
val userObjectsForExpandedSecurityNodes =
snykToolWindowPanel.userObjectsForExpandedNodes(rootSecurityIssuesTreeNode)
rootSecurityIssuesTreeNode.removeAllChildren()

var securityIssuesCount: Int? = null
var securityIssuesHMLPostfix = ""
if (pluginSettings().snykCodeSecurityIssuesScanEnable) {
val securityResults = snykCodeResults
.map { it.key to it.value.filter { issue -> issue.additionalData.isSecurityType } }
.toMap()
securityIssuesCount = securityResults.values.flatten().distinct().size
securityIssuesHMLPostfix = buildSeveritiesPostfixForFileNode(securityResults)
displayIssues(snykCodeResults, selectedNodeUserObject, true)

if (pluginSettings().treeFiltering.codeSecurityResults) {
val securityResultsToDisplay = securityResults.map { entry ->
entry.key to entry.value
.filter { pluginSettings().hasSeverityEnabledAndFiltered(it.getSeverityAsEnum()) }
}.toMap()
// display Quality (non Security) issues
displayIssues(snykCodeResults, selectedNodeUserObject, false)
}

displayResultsForCodeRoot(
rootSecurityIssuesTreeNode,
securityResultsToDisplay
)
}
private fun displayIssues(
snykCodeResults: Map<SnykCodeFile, List<ScanIssue>>,
selectedNodeUserObject: Any?,
isSecurity: Boolean
) {
val rootNode = if (isSecurity) this.rootSecurityIssuesTreeNode else this.rootQualityIssuesTreeNode

val userObjectsForExpandedNodes =
snykToolWindowPanel.userObjectsForExpandedNodes(rootNode)

rootNode.removeAllChildren()

var issuesCount: Int? = null
var rootNodePostFix = ""
val settings = pluginSettings()
val enabledInSettings = when {
isSecurity -> settings.snykCodeSecurityIssuesScanEnable
else -> settings.snykCodeQualityIssuesScanEnable
}
snykToolWindowPanel.updateTreeRootNodesPresentation(
securityIssuesCount = securityIssuesCount,
addHMLPostfix = securityIssuesHMLPostfix
)
snykToolWindowPanel.smartReloadRootNode(
rootSecurityIssuesTreeNode,
userObjectsForExpandedSecurityNodes,
selectedNodeUserObject
)

// display Quality (non Security) issues
val userObjectsForExpandedQualityNodes =
snykToolWindowPanel.userObjectsForExpandedNodes(rootQualityIssuesTreeNode)
rootQualityIssuesTreeNode.removeAllChildren()

var qualityIssuesCount: Int? = null
var qualityIssuesHMLPostfix = ""
if (pluginSettings().snykCodeQualityIssuesScanEnable) {
val qualityResults = snykCodeResults
.map { it.key to it.value.filter { issue -> !issue.additionalData.isSecurityType } }
if (enabledInSettings) {
val filteredResults = snykCodeResults
.map { it.key to it.value.filter { issue -> issue.additionalData.isSecurityType == isSecurity } }
.toMap()
qualityIssuesCount = qualityResults.values.flatten().distinct().size
qualityIssuesHMLPostfix = buildSeveritiesPostfixForFileNode(qualityResults)

if (pluginSettings().treeFiltering.codeQualityResults) {
val qualityResultsToDisplay = qualityResults.map { entry ->
entry.key to entry.value
.filter { pluginSettings().hasSeverityEnabledAndFiltered(it.getSeverityAsEnum()) }
issuesCount = filteredResults.values.flatten().distinct().size
rootNodePostFix = buildSeveritiesPostfixForFileNode(filteredResults)

val treeFiltering = when {
isSecurity -> settings.treeFiltering.codeSecurityResults
else -> settings.treeFiltering.codeQualityResults
}

if (treeFiltering) {
val resultsToDisplay = filteredResults.map { entry ->
entry.key to entry.value.filter {
settings.hasSeverityEnabledAndFiltered(it.getSeverityAsEnum())
}
}.toMap()
displayResultsForCodeRoot(
rootQualityIssuesTreeNode,
qualityResultsToDisplay
)
displayResultsForCodeRoot(rootNode, resultsToDisplay)
}
}
snykToolWindowPanel.updateTreeRootNodesPresentation(
qualityIssuesCount = qualityIssuesCount,
addHMLPostfix = qualityIssuesHMLPostfix
)

if (isSecurity) {
snykToolWindowPanel.updateTreeRootNodesPresentation(
securityIssuesCount = issuesCount,
addHMLPostfix = rootNodePostFix
)
} else {
snykToolWindowPanel.updateTreeRootNodesPresentation(
qualityIssuesCount = issuesCount,
addHMLPostfix = rootNodePostFix
)
}

snykToolWindowPanel.smartReloadRootNode(
rootQualityIssuesTreeNode,
userObjectsForExpandedQualityNodes,
rootNode,
userObjectsForExpandedNodes,
selectedNodeUserObject
)
}
Expand Down
Loading