Skip to content

Commit

Permalink
refactor: remove unnecessary code, centralize bulk file listening
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiandoetsch committed Oct 2, 2024
1 parent 6b3c340 commit 27746b1
Show file tree
Hide file tree
Showing 19 changed files with 93 additions and 806 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/io/snyk/plugin/SnykBulkFileListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.snyk.plugin

import com.intellij.ide.impl.ProjectUtil
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectFileIndex
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.isFile
import com.intellij.openapi.vfs.newvfs.BulkFileListener
Expand Down Expand Up @@ -49,7 +50,6 @@ import com.intellij.openapi.vfs.newvfs.events.VFilePropertyChangeEvent
* - addressed at `before` state, old file processed to _clean_ caches
*/
abstract class SnykBulkFileListener : BulkFileListener {

/****************************** Before **************************/

override fun before(events: List<VFileEvent>) {
Expand Down
8 changes: 2 additions & 6 deletions src/main/kotlin/io/snyk/plugin/SnykPostStartupActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ import com.intellij.openapi.startup.ProjectActivity
import com.intellij.openapi.vfs.VirtualFileManager
import io.snyk.plugin.extensions.SnykControllerImpl
import io.snyk.plugin.extensions.SnykControllerManager
import io.snyk.plugin.snykcode.SnykCodeBulkFileListener
import io.snyk.plugin.ui.SnykBalloonNotifications
import snyk.common.AnnotatorCommon
import snyk.common.lsp.LanguageServerBulkFileListener
import snyk.container.ContainerBulkFileListener
import snyk.iac.IacBulkFileListener
import snyk.oss.OssBulkFileListener
import java.time.Instant
import java.time.temporal.ChronoUnit
import java.util.Date
Expand All @@ -43,9 +41,7 @@ class SnykPostStartupActivity : ProjectActivity {
if (!listenersActivated) {
val messageBusConnection = ApplicationManager.getApplication().messageBus.connect()
// TODO: add subscription for language server messages
messageBusConnection.subscribe(VirtualFileManager.VFS_CHANGES, OssBulkFileListener())
messageBusConnection.subscribe(VirtualFileManager.VFS_CHANGES, SnykCodeBulkFileListener())
messageBusConnection.subscribe(VirtualFileManager.VFS_CHANGES, IacBulkFileListener())
messageBusConnection.subscribe(VirtualFileManager.VFS_CHANGES, LanguageServerBulkFileListener())
messageBusConnection.subscribe(VirtualFileManager.VFS_CHANGES, ContainerBulkFileListener())
messageBusConnection.subscribe(ProjectManager.TOPIC, SnykProjectManagerListener())
listenersActivated = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class SnykProjectManagerListener : ProjectManagerListener {
// limit clean up to TIMEOUT
try {
threadPool.submit {
// lets all running ProgressIndicators release MUTEX first
val ls = LanguageServerWrapper.getInstance()
if (ls.isInitialized) {
ls.updateWorkspaceFolders(emptySet(), ls.getWorkspaceFolders(project))
Expand Down
4 changes: 0 additions & 4 deletions src/main/kotlin/io/snyk/plugin/events/SnykScanListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ interface SnykScanListener {

fun scanningStarted()

fun scanningIacFinished(iacResult: IacResult)

fun scanningIacError(snykError: SnykError)

fun scanningContainerFinished(containerResult: ContainerResult)

fun scanningContainerError(snykError: SnykError)
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/io/snyk/plugin/ui/UIUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ fun descriptionHeaderPanel(
2 + // CVSS
2 + // Snyk description
customLabels.size * 2 // Labels with `|`
panel.layout = GridLayoutManager(1, columnCount, Insets(0, 0, 0, 0), 5, 0)
panel.layout = GridLayoutManager(1, columnCount, JBUI.emptyInsets(), 5, 0)

panel.add(
JLabel(issueNaming).apply { font = font14 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ import snyk.container.ui.ContainerIssueTreeNode
import snyk.iac.IacIssue
import snyk.iac.IacResult
import snyk.iac.ignorableErrorCodes
import snyk.iac.ui.toolwindow.IacFileTreeNode
import snyk.iac.ui.toolwindow.IacIssueTreeNode
import java.awt.BorderLayout
import java.util.Objects.nonNull
import javax.swing.JPanel
Expand Down Expand Up @@ -189,16 +187,6 @@ class SnykToolWindowPanel(
}
}

override fun scanningIacFinished(iacResult: IacResult) {
ApplicationManager.getApplication().invokeLater {
displayIacResults(iacResult)
}
if (iacResult.getVisibleErrors().isNotEmpty()) {
notifyAboutErrorsIfNeeded(ProductType.IAC, iacResult)
}
refreshAnnotationsForOpenFiles(project)
}

override fun scanningContainerFinished(containerResult: ContainerResult) {
ApplicationManager.getApplication().invokeLater {
displayContainerResults(containerResult)
Expand All @@ -225,24 +213,6 @@ class SnykToolWindowPanel(
}
}

override fun scanningIacError(snykError: SnykError) {
var iacResultsCount: Int? = null
if (snykError.code != null && ignorableErrorCodes.contains(snykError.code)) {
iacResultsCount = NODE_NOT_SUPPORTED_STATE
} else {
SnykBalloonNotificationHelper.showError(snykError.message, project)
if (snykError.message.startsWith(AUTH_FAILED_TEXT)) {
pluginSettings().token = null
}
}
ApplicationManager.getApplication().invokeLater {
removeAllChildren(listOf(rootIacIssuesTreeNode))
updateTreeRootNodesPresentation(iacResultsCount = iacResultsCount)
chooseMainPanelToDisplay()
}
refreshAnnotationsForOpenFiles(project)
}

override fun scanningContainerError(snykError: SnykError) {
var containerResultsCount: Int? = null
if (snykError == ContainerService.NO_IMAGES_TO_SCAN_ERROR) {
Expand Down Expand Up @@ -295,7 +265,6 @@ class SnykToolWindowPanel(

val snykCachedResults = getSnykCachedResults(project) ?: return
ApplicationManager.getApplication().invokeLater {
snykCachedResults.currentIacResult?.let { displayIacResults(it) }
snykCachedResults.currentContainerResult?.let { displayContainerResults(it) }
}
}
Expand Down Expand Up @@ -721,57 +690,6 @@ class SnykToolWindowPanel(
revalidate()
}

fun displayIacResults(iacResult: IacResult) {
val userObjectsForExpandedChildren = userObjectsForExpandedNodes(rootIacIssuesTreeNode)
val selectedNodeUserObject = TreeUtil.findObjectInPath(vulnerabilitiesTree.selectionPath, Any::class.java)

rootIacIssuesTreeNode.removeAllChildren()

fun navigateToIaCIssue(
virtualFile: VirtualFile?,
lineStartOffset: Int,
): () -> Unit =
{
if (virtualFile?.isValid == true) {
navigateToSource(project, virtualFile, lineStartOffset)
}
}

val settings = pluginSettings()
if (settings.iacScanEnabled && settings.treeFiltering.iacResults) {
iacResult.allCliIssues?.forEach { iacVulnerabilitiesForFile ->
if (iacVulnerabilitiesForFile.infrastructureAsCodeIssues.isNotEmpty()) {
val fileTreeNode = IacFileTreeNode(iacVulnerabilitiesForFile, project)
rootIacIssuesTreeNode.add(fileTreeNode)

iacVulnerabilitiesForFile.infrastructureAsCodeIssues
.filter { settings.hasSeverityEnabledAndFiltered(it.getSeverity()) }
.sortedByDescending { it.getSeverity() }
.forEach {
val navigateToSource =
navigateToIaCIssue(
iacVulnerabilitiesForFile.virtualFile,
it.lineStartOffset,
)
fileTreeNode.add(IacIssueTreeNode(it, project, navigateToSource))
}
}
}
iacResult.getVisibleErrors().forEach { snykError ->
rootIacIssuesTreeNode.add(
ErrorTreeNode(snykError, project, navigateToIaCIssue(snykError.virtualFile, 0)),
)
}
}

updateTreeRootNodesPresentation(
iacResultsCount = iacResult.issuesCount,
addHMLPostfix = buildHMLpostfix(iacResult),
)

smartReloadRootNode(rootIacIssuesTreeNode, userObjectsForExpandedChildren, selectedNodeUserObject)
}

fun displayContainerResults(containerResult: ContainerResult) {
val userObjectsForExpandedChildren = userObjectsForExpandedNodes(rootContainerIssuesTreeNode)
val selectedNodeUserObject = TreeUtil.findObjectInPath(vulnerabilitiesTree.selectionPath, Any::class.java)
Expand Down Expand Up @@ -949,12 +867,6 @@ class SnykToolWindowPanel(
}
}

fun selectNodeAndDisplayDescription(iacIssue: IacIssue) =
selectAndDisplayNodeWithIssueDescription { treeNode ->
treeNode is IacIssueTreeNode &&
(treeNode.userObject as IacIssue) == iacIssue
}

fun selectNodeAndDisplayDescription(issuesForImage: ContainerIssuesForImage) =
selectAndDisplayNodeWithIssueDescription { treeNode ->
treeNode is ContainerImageTreeNode &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ import snyk.container.ContainerIssue
import snyk.container.ContainerIssuesForImage
import snyk.container.ui.ContainerImageTreeNode
import snyk.container.ui.ContainerIssueTreeNode
import snyk.iac.IacIssue
import snyk.iac.IacIssuesForFile
import snyk.iac.ui.toolwindow.IacFileTreeNode
import snyk.iac.ui.toolwindow.IacIssueTreeNode
import java.util.Locale
import javax.swing.Icon
import javax.swing.JTree
import javax.swing.tree.DefaultMutableTreeNode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package io.snyk.plugin.ui.toolwindow.panels
import com.intellij.openapi.editor.colors.EditorColors
import com.intellij.openapi.editor.colors.EditorColorsManager
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.uiDesigner.core.GridLayoutManager
import com.intellij.util.ui.JBUI
import com.intellij.util.ui.UIUtil
import io.snyk.plugin.SnykFile
import io.snyk.plugin.toVirtualFile
import io.snyk.plugin.ui.DescriptionHeaderPanel
import io.snyk.plugin.ui.SnykBalloonNotificationHelper
import io.snyk.plugin.ui.baseGridConstraintsAnchorWest
Expand All @@ -26,7 +26,6 @@ import snyk.common.lsp.ScanIssue
import stylesheets.SnykStylesheets
import java.awt.BorderLayout
import java.awt.Font
import java.nio.file.Paths
import javax.swing.JLabel
import javax.swing.JPanel
import kotlin.collections.set
Expand All @@ -43,65 +42,60 @@ class SuggestionDescriptionPanelFromLS(
"Snyk encountered an issue while rendering the vulnerability description. Please try again, or contact support if the problem persists. We apologize for any inconvenience caused."

init {
if (issue.canLoadSuggestionPanelFromHTML()) {
val loadHandlerGenerators: MutableList<LoadHandlerGenerator> =
emptyList<LoadHandlerGenerator>().toMutableList()
val loadHandlerGenerators: MutableList<LoadHandlerGenerator> =
emptyList<LoadHandlerGenerator>().toMutableList()

// TODO: replace directly in HTML instead of JS
loadHandlerGenerators += {
ThemeBasedStylingGenerator().generate(it)
}
// TODO: replace directly in HTML instead of JS
loadHandlerGenerators += {
ThemeBasedStylingGenerator().generate(it)
}

when (issue.filterableIssueType) {
ScanIssue.CODE_QUALITY, ScanIssue.CODE_SECURITY -> {
val virtualFiles = LinkedHashMap<String, VirtualFile?>()
for (dataFlow in issue.additionalData.dataFlow) {
virtualFiles[dataFlow.filePath] =
VirtualFileManager.getInstance().findFileByNioPath(Paths.get(dataFlow.filePath))
}
when (issue.filterableIssueType) {
ScanIssue.CODE_QUALITY, ScanIssue.CODE_SECURITY -> {
val virtualFiles = LinkedHashMap<String, VirtualFile?>()
for (dataFlow in issue.additionalData.dataFlow) {
virtualFiles[dataFlow.filePath] = dataFlow.filePath.toVirtualFile()
}

val openFileLoadHandlerGenerator = OpenFileLoadHandlerGenerator(snykFile.project, virtualFiles)
loadHandlerGenerators += {
openFileLoadHandlerGenerator.generate(it)
}
val openFileLoadHandlerGenerator = OpenFileLoadHandlerGenerator(snykFile.project, virtualFiles)
loadHandlerGenerators += {
openFileLoadHandlerGenerator.generate(it)
}

val generateAIFixHandler = GenerateAIFixHandler()
loadHandlerGenerators += {
generateAIFixHandler.generateAIFixCommand(it)
}
val generateAIFixHandler = GenerateAIFixHandler()
loadHandlerGenerators += {
generateAIFixHandler.generateAIFixCommand(it)
}

val applyFixHandler = ApplyFixHandler(snykFile.project)
loadHandlerGenerators += {
applyFixHandler.generateApplyFixCommand(it)
}
val applyFixHandler = ApplyFixHandler(snykFile.project)
loadHandlerGenerators += {
applyFixHandler.generateApplyFixCommand(it)
}
}
val html = this.getCustomCssAndScript()
val jbCefBrowserComponent =
JCEFUtils.getJBCefBrowserComponentIfSupported(html, loadHandlerGenerators)
if (jbCefBrowserComponent == null) {
val statePanel = StatePanel(SnykToolWindowPanel.SELECT_ISSUE_TEXT)
this.add(wrapWithScrollPane(statePanel), BorderLayout.CENTER)
SnykBalloonNotificationHelper.showError(unexpectedErrorMessage, null)
} else {
val lastRowToAddSpacer = 5
val panel =
JPanel(
GridLayoutManager(lastRowToAddSpacer + 1, 1, JBUI.insets(0, 10, 20, 10), -1, 20),
).apply {
this.add(
jbCefBrowserComponent,
panelGridConstraints(1),
)
}
this.add(
wrapWithScrollPane(panel),
BorderLayout.CENTER,
)
this.add(panel)
}
}
val html = this.getCustomCssAndScript()
val jbCefBrowserComponent =
JCEFUtils.getJBCefBrowserComponentIfSupported(html, loadHandlerGenerators)
if (jbCefBrowserComponent == null) {
val statePanel = StatePanel(SnykToolWindowPanel.SELECT_ISSUE_TEXT)
this.add(wrapWithScrollPane(statePanel), BorderLayout.CENTER)
SnykBalloonNotificationHelper.showError(unexpectedErrorMessage, null)
} else {
createUI()
val lastRowToAddSpacer = 5
val panel =
JPanel(
GridLayoutManager(lastRowToAddSpacer + 1, 1, JBUI.insets(0, 10, 20, 10), -1, 20),
).apply {
this.add(
jbCefBrowserComponent,
panelGridConstraints(1),
)
}
this.add(
wrapWithScrollPane(panel),
BorderLayout.CENTER,
)
this.add(panel)
}
}

Expand Down
17 changes: 0 additions & 17 deletions src/main/kotlin/snyk/common/SnykCachedResults.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ class SnykCachedResults(
get() = if (field?.isExpired() == false) field else null

val currentIacResultsLS: MutableMap<SnykFile, List<ScanIssue>> = ConcurrentMap()
var currentIacResult: IacResult? = null
get() = if (field?.isExpired() == false) field else null

var currentOssError: SnykError? = null
var currentContainerError: SnykError? = null
Expand All @@ -60,7 +58,6 @@ class SnykCachedResults(

fun cleanCaches() {
currentContainerResult = null
currentIacResult = null
currentOssError = null
currentContainerError = null
currentIacError = null
Expand All @@ -83,24 +80,10 @@ class SnykCachedResults(
currentContainerError = null
}

override fun scanningIacFinished(iacResult: IacResult) {
currentIacResult = iacResult
}

override fun scanningContainerFinished(containerResult: ContainerResult) {
currentContainerResult = containerResult
}

override fun scanningIacError(snykError: SnykError) {
currentIacResult = null
currentIacError =
when {
snykError.message.startsWith(SnykToolWindowPanel.NO_IAC_FILES) -> null
snykError.message.startsWith(SnykToolWindowPanel.AUTH_FAILED_TEXT) -> null
else -> snykError
}
}

override fun scanningContainerError(snykError: SnykError) {
currentContainerResult = null
currentContainerError =
Expand Down
Loading

0 comments on commit 27746b1

Please sign in to comment.