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 deprecations and update them to current API #484

Merged
merged 14 commits into from
Apr 3, 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
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pluginName=Snyk Security
# for insight into build numbers and IntelliJ Platform versions
# see https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild=233
pluginUntilBuild=233.*
pluginUntilBuild=241.*
platformVersion=2023.3
platformDownloadSources=true

Expand All @@ -13,7 +13,7 @@ platformDownloadSources=true
# see https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
platformPlugins=org.intellij.plugins.hcl:233.11799.172,org.jetbrains.plugins.yaml,org.jetbrains.kotlin,com.intellij.java,org.intellij.groovy
# list of versions for which to check the plugin for api compatibility
pluginVerifierIdeVersions=2023.3
pluginVerifierIdeVersions=2023.3,2024.1
localIdeDirectory=
# opt-out flag for bundling Kotlin standard library
# see https://plugins.jetbrains.com/docs/intellij/kotlin.html#kotlin-standard-library
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import io.snyk.plugin.ui.getReadOnlyClickableHtmlJEditorPane
import org.apache.commons.text.StringEscapeUtils.escapeHtml4
import snyk.common.getEndpointUrl
import snyk.common.isOauth
import snyk.common.lsp.LanguageServerWrapper
import java.awt.BorderLayout
import java.awt.Dimension
import java.awt.datatransfer.StringSelection
Expand All @@ -47,7 +48,7 @@ class SnykCliAuthenticationService(val project: Project) {
downloadCliIfNeeded()
if (getCliFile().exists()) executeAuthCommand()
if (isAuthenticated) executeGetConfigApiCommand()

LanguageServerWrapper.getInstance().updateConfiguration()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Was this something we should have added before and you just noticed?

Copy link
Collaborator Author

@bastiandoetsch bastiandoetsch Apr 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I noticed, that the token was not updated in Language Server after authentication. So I added it.

return token
}

Expand Down
57 changes: 36 additions & 21 deletions src/main/kotlin/io/snyk/plugin/ui/SnykSettingsDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import com.intellij.ui.ContextHelpLabel
import com.intellij.ui.DocumentAdapter
import com.intellij.ui.IdeBorderFactory
import com.intellij.ui.components.JBPasswordField
import com.intellij.ui.components.JBTextField
import com.intellij.ui.components.fields.ExpandableTextField
import com.intellij.ui.dsl.builder.AlignX
import com.intellij.ui.dsl.builder.panel
import com.intellij.uiDesigner.core.Spacer
import com.intellij.util.Alarm
import com.intellij.util.FontUtil
import com.intellij.util.ui.GridBag
import com.intellij.util.ui.UI
import io.snyk.plugin.events.SnykCliDownloadListener
import io.snyk.plugin.getCliFile
import io.snyk.plugin.getSnykCliAuthenticationService
Expand Down Expand Up @@ -84,7 +86,7 @@ class SnykSettingsDialog(

private val manageBinariesAutomatically: JCheckBox = JCheckBox()
private val cliPathTextBoxWithFileBrowser = TextFieldWithBrowseButton()
private val cliBaseDownloadUrlTextField = JTextField()
private val cliBaseDownloadUrlTextField = JBTextField()

private val logger = Logger.getInstance(this::class.java)

Expand Down Expand Up @@ -522,33 +524,44 @@ class SnykSettingsDialog(

cliBaseDownloadUrlTextField.toolTipText = "The default URL is https://static.snyk.io. " +
"for FIPS-enabled CLIs (only available for Windows and Linux), please use https://static.snyk.io/fips"
executableSettingsPanel.add(
UI.PanelFactory
.panel(cliBaseDownloadUrlTextField)
.withLabel("Base URL to download the CLI: ").createPanel(),
gb.nextLine()
)
val cliBaseDownloadPanel = panel {
row {
label("Base URL to download the CLI: ")
cell(cliBaseDownloadUrlTextField).align(AlignX.FILL)
}
}
executableSettingsPanel.add(cliBaseDownloadPanel, gb.nextLine())

cliPathTextBoxWithFileBrowser.toolTipText = "The default path is ${getCliFile().canonicalPath}."
val descriptor = FileChooserDescriptor(true, false, false, false, false, false)
cliPathTextBoxWithFileBrowser.addBrowseFolderListener(
"", "Please choose the Snyk CLI you want to use:", null,
"",
"Please choose the Snyk CLI you want to use:",
null,
descriptor,
TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT
)

executableSettingsPanel.add(
UI.PanelFactory
.panel(cliPathTextBoxWithFileBrowser)
.withLabel("Path to Snyk CLI: ").createPanel(),
panel {
row {
label("Path to Snyk CLI: ")
cell(cliPathTextBoxWithFileBrowser).align(AlignX.FILL)
}
},
gb.nextLine()
)

manageBinariesAutomatically.text = "Automatically manage needed binaries"
executableSettingsPanel.add(
manageBinariesAutomatically,
panel {
row {
label("Automatically manage needed binaries: ")
cell(manageBinariesAutomatically).align(AlignX.FILL)
}
},
gb.nextLine()
)

val descriptionLabelManageBinaries =
JLabel(
"<html>These options allow you to customize the handling, where and how plugin " +
Expand Down Expand Up @@ -598,14 +611,16 @@ class SnykSettingsDialog(
}

private fun setupValidation(textField: JTextField, message: String, isValidText: (sourceStr: String?) -> Boolean) {
ComponentValidator(rootPanel).withValidator(Supplier<ValidationInfo?> {
val validationInfo: ValidationInfo = if (!isValidText(textField.text)) {
ValidationInfo(message, textField)
} else {
ValidationInfo("")
ComponentValidator(rootPanel).withValidator(
Supplier<ValidationInfo?> {
val validationInfo: ValidationInfo = if (!isValidText(textField.text)) {
ValidationInfo(message, textField)
} else {
ValidationInfo("")
}
validationInfo
}
validationInfo
}).installOn(textField)
).installOn(textField)

textField.document.addDocumentListener(object : DocumentAdapter() {
override fun textChanged(event: DocumentEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.snyk.plugin.ui.settings

import com.intellij.openapi.project.Project
import com.intellij.ui.components.JBCheckBox
import com.intellij.ui.layout.panel
import com.intellij.util.ui.JBUI
import io.snyk.plugin.getSnykTaskQueueService
import io.snyk.plugin.pluginSettings
Expand All @@ -14,45 +13,34 @@ class IssueViewOptionsPanel(
private val settings
get() = pluginSettings()

private var currentOpenIssuesEnabled = settings.openIssuesEnabled
private var currentIgnoredIssuesEnabled = settings.ignoredIssuesEnabled

val panel = panel {
val panel = com.intellij.ui.dsl.builder.panel {
row {
cell {
checkBox(
text = "Open issues",
getter = { settings.openIssuesEnabled },
setter = { settings.openIssuesEnabled = it }
).component.apply {
this.addItemListener {
if (canOptionChange(this, currentOpenIssuesEnabled)) {
currentOpenIssuesEnabled = this.isSelected
settings.openIssuesEnabled = currentOpenIssuesEnabled
getSnykTaskQueueService(project)?.scan()
}
checkBox(
text = "Open issues",
).component.apply {
isSelected = settings.openIssuesEnabled
this.addItemListener {
if (canOptionChange(this, settings.openIssuesEnabled)) {
settings.openIssuesEnabled = this.isSelected
getSnykTaskQueueService(project)?.scan()
}
name = "Open issues"
}
name = "Open issues"
}
}
row {
cell {
checkBox(
text = "Ignored issues",
getter = { settings.ignoredIssuesEnabled },
setter = { settings.ignoredIssuesEnabled = it }
).component.apply {
isSelected = settings.ignoredIssuesEnabled
this.addItemListener {
if (canOptionChange(this, currentIgnoredIssuesEnabled)) {
currentIgnoredIssuesEnabled = this.isSelected
settings.ignoredIssuesEnabled =currentIgnoredIssuesEnabled
if (canOptionChange(this, settings.ignoredIssuesEnabled)) {
settings.ignoredIssuesEnabled = this.isSelected
getSnykTaskQueueService(project)?.scan()
}
}
name = "Ignored issues"
}
}
}
}.apply {
name = "issueViewPanel"
Expand All @@ -61,8 +49,8 @@ class IssueViewOptionsPanel(

private fun canOptionChange(component: JBCheckBox, wasEnabled: Boolean): Boolean {
val onlyOneEnabled = arrayOf(
currentOpenIssuesEnabled,
currentIgnoredIssuesEnabled,
settings.openIssuesEnabled,
settings.ignoredIssuesEnabled,
).count { it } == 1

if (onlyOneEnabled && wasEnabled) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package io.snyk.plugin.ui.settings

import com.intellij.ui.components.JBCheckBox
import com.intellij.ui.layout.panel
import com.intellij.ui.dsl.builder.panel
import com.intellij.util.ui.JBUI
import io.snyk.plugin.Severity
import io.snyk.plugin.pluginSettings
import io.snyk.plugin.ui.SnykBalloonNotificationHelper
import java.awt.event.ItemEvent

class SeveritiesEnablementPanel {
private val settings
Expand All @@ -18,62 +19,42 @@ class SeveritiesEnablementPanel {

val panel = panel {
row {
cell {
checkBox(
text = Severity.CRITICAL.toPresentableString(),
getter = { settings.criticalSeverityEnabled },
setter = { settings.criticalSeverityEnabled = it }
).component.apply {
this.addItemListener {
isLastSeverityDisabling(this, currentCriticalSeverityEnabled)
currentCriticalSeverityEnabled = this.isSelected
}
name = Severity.CRITICAL.toPresentableString()
checkBox(Severity.CRITICAL.toPresentableString()).applyToComponent {
name = text
isSelected = settings.criticalSeverityEnabled
this.addItemListener {
correctLastSeverityDisabled(it)
settings.criticalSeverityEnabled = this.isSelected
}
}
}
row {
cell {
checkBox(
text = Severity.HIGH.toPresentableString(),
getter = { settings.highSeverityEnabled },
setter = { settings.highSeverityEnabled = it }
).component.apply {
this.addItemListener {
isLastSeverityDisabling(this, currentHighSeverityEnabled)
currentHighSeverityEnabled = this.isSelected
}
name = Severity.HIGH.toPresentableString()
checkBox(Severity.HIGH.toPresentableString()).applyToComponent {
name = text
isSelected = settings.highSeverityEnabled
this.addItemListener {
correctLastSeverityDisabled(it)
settings.highSeverityEnabled = this.isSelected
}
}
}
row {
cell {
checkBox(
text = Severity.MEDIUM.toPresentableString(),
getter = { settings.mediumSeverityEnabled },
setter = { settings.mediumSeverityEnabled = it }
).component.apply {
this.addItemListener {
isLastSeverityDisabling(this, currentMediumSeverityEnabled)
currentMediumSeverityEnabled = this.isSelected
}
name = Severity.MEDIUM.toPresentableString()
checkBox(Severity.MEDIUM.toPresentableString()).applyToComponent {
name = text
isSelected = settings.mediumSeverityEnabled
this.addItemListener {
correctLastSeverityDisabled(it)
settings.mediumSeverityEnabled = this.isSelected
}
}
}
row {
cell {
checkBox(
text = Severity.LOW.toPresentableString(),
getter = { settings.lowSeverityEnabled },
setter = { settings.lowSeverityEnabled = it }
).component.apply {
this.addItemListener {
isLastSeverityDisabling(this, currentLowSeverityEnabled)
currentLowSeverityEnabled = this.isSelected
}
name = Severity.LOW.toPresentableString()
checkBox(Severity.LOW.toPresentableString()).applyToComponent {
name = text
isSelected = settings.lowSeverityEnabled
this.addItemListener {
correctLastSeverityDisabled(it)
settings.lowSeverityEnabled = this.isSelected
}
}
}
Expand All @@ -82,6 +63,11 @@ class SeveritiesEnablementPanel {
border = JBUI.Borders.empty(2)
}

private fun JBCheckBox.correctLastSeverityDisabled(it: ItemEvent) {
val deselected = it.stateChange == ItemEvent.DESELECTED
isLastSeverityDisabling(this, deselected)
}

private fun isLastSeverityDisabling(component: JBCheckBox, wasEnabled: Boolean): Boolean {
val onlyOneEnabled = arrayOf(
currentCriticalSeverityEnabled,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.snyk.plugin.ui.toolwindow

import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.Service
import com.intellij.openapi.project.Project
import org.jetbrains.annotations.NotNull


/**
* Top-Level disposable for the Snyk plugin.
*/
@Service(Service.Level.APP, Service.Level.PROJECT)
class SnykPluginDisposable : Disposable {
companion object {
@NotNull
fun getInstance(): Disposable {
return ApplicationManager.getApplication().getService(SnykPluginDisposable::class.java)
}

@NotNull
fun getInstance(@NotNull project: Project): Disposable {
return project.getService(SnykPluginDisposable::class.java)
}
}

override fun dispose() = Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ class SnykToolWindow(private val project: Project) : SimpleToolWindowPanel(false
private fun updateActionsPresentation() =
ApplicationManager.getApplication().invokeLater { actionToolbar.updateActionsImmediately() }

override fun dispose() = Unit
var isDisposed = false
override fun dispose() {
isDisposed = true
}

inner class ExpandNodeChildAction(
val tree: JTree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowFactory
import io.snyk.plugin.pluginSettings

/**
* IntelliJ ToolWindowFactory for Snyk plugin.
Expand All @@ -13,11 +14,10 @@ class SnykToolWindowFactory : ToolWindowFactory, DumbAware {

override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
val toolWindowPanel = SnykToolWindow(project)
Disposer.register(SnykPluginDisposable.getInstance(), toolWindowPanel)
val contentManager = toolWindow.contentManager
val content = contentManager.factory.createContent(toolWindowPanel, null, false)
contentManager.addContent(content)

Disposer.register(project, toolWindowPanel)
}

companion object {
Expand Down
Loading
Loading