Skip to content

Commit

Permalink
feat: use intellij environment in ls startup (#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiandoetsch authored Sep 2, 2024
1 parent e8a36ae commit 4d819c1
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 26 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# Snyk Security Changelog

## [2.9.1]

### Changed
- save git folder config in settings
- propagate Jetbrains determined runtime environment to language server
- automatically propagate standard file path for CLI if empty on apply in settings
- guard base branch setting against being empty
- better error messaging when unexpected loop occurs during initialization
- switch downloads to downloads.snyk.io

### Fixes
- add name to code vision provider
- add flashes for auto-fixable Open Source Issues
- show code vision for Open Source also, when Snyk Code is still analysing
- clean-up old open source scan functionality

## [2.9.0]
### Changed
- Updated the language server protocol version to 14 to support new communication model.
Expand Down
7 changes: 0 additions & 7 deletions src/main/kotlin/io/snyk/plugin/SnykFile.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package io.snyk.plugin

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Iconable
import com.intellij.openapi.vfs.VirtualFile
import snyk.common.RelativePathHelper
import javax.swing.Icon

data class SnykFile(val project: Project, val virtualFile: VirtualFile) {
var icon: Icon? = null
val relativePath = RelativePathHelper().getRelativePath(virtualFile, project)
init {
ApplicationManager.getApplication().runReadAction {
virtualFile.getPsiFile(project)?.getIcon(Iconable.ICON_FLAG_READ_STATUS)
}
}
}

fun toSnykFileSet(project: Project, virtualFiles: Set<VirtualFile>) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import io.snyk.plugin.cli.Platform
import io.snyk.plugin.getPluginPath
import io.snyk.plugin.getSnykProjectSettingsService
import io.snyk.plugin.isProjectSettingsAvailable
import snyk.common.lsp.FolderConfig
import java.io.File.separator
import java.time.Instant
import java.time.LocalDate
Expand All @@ -33,7 +32,7 @@ class SnykApplicationSettingsStateService : PersistentStateComponent<SnykApplica
var currentLSProtocolVersion: Int? = 0
var autofixEnabled: Boolean? = false
var isGlobalIgnoresFeatureEnabled = false
var cliBaseDownloadURL: String = "https://static.snyk.io"
var cliBaseDownloadURL: String = "https://downloads.snyk.io"
var cliPath: String = getPluginPath() + separator + Platform.current().snykWrapperFileName
var cliReleaseChannel = "stable"
var manageBinariesAutomatically: Boolean = true
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/io/snyk/plugin/ui/SnykSettingsDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class SnykSettingsDialog(
private val channels = listOf("stable", "rc", "preview").toArray(emptyArray())
private val cliReleaseChannelDropDown = ComboBox(channels).apply { this.isEditable = true }
private val cliBaseDownloadUrlTextField = JBTextField()
val baseBranchInfoLabel = JBLabel("Base branch: ")
private val baseBranchInfoLabel = JBLabel("Base branch: ")

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

Expand Down Expand Up @@ -182,8 +182,8 @@ class SnykSettingsDialog(
scanOnSaveCheckbox.isSelected = applicationSettings.scanOnSave
cliReleaseChannelDropDown.selectedItem = applicationSettings.cliReleaseChannel

baseBranchInfoLabel.text = service<FolderConfigSettings>().getAll()
.values.joinToString("\n") { "Base branch for ${it.folderPath}: ${it.baseBranch}" }
baseBranchInfoLabel.text = "<html>"+service<FolderConfigSettings>().getAll()
.values.joinToString("<br/>") { "Base branch for ${it.folderPath}: ${it.baseBranch}" }+"</html>"
}
}

Expand Down Expand Up @@ -618,8 +618,8 @@ class SnykSettingsDialog(
gb.nextLine(),
)

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"
cliBaseDownloadUrlTextField.toolTipText = "The default URL is https://downloads.snyk.io. " +
"for FIPS-enabled CLIs (only available for Windows and Linux), please use https://downloads.snyk.io/fips"
val cliBaseDownloadPanel =
panel {
row {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class SnykTreeCellRenderer : ColoredTreeCellRenderer() {
val file = entry.key

val pair = updateTextTooltipAndIcon(file, productType, value, entry.value.first())
nodeIcon = pair.first
pair.first?.let { nodeIcon = pair.first }
text = pair.second
val cachedIssues =
getSnykCachedResultsForProduct(entry.key.project, productType)
Expand Down Expand Up @@ -295,7 +295,7 @@ class SnykTreeCellRenderer : ColoredTreeCellRenderer() {
}
}

val nodeIcon = firstIssue?.icon() ?: file.icon
val nodeIcon = firstIssue?.icon()
return Pair(nodeIcon, text)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import io.snyk.plugin.pluginSettings
import io.snyk.plugin.ui.toolwindow.SnykPluginDisposable
import snyk.common.ProductType

class SnykOSSAnnotatorLS : SnykAnnotator(product = ProductType.OSS) {
class SnykOSSAnnotator : SnykAnnotator(product = ProductType.OSS) {
init {
Disposer.register(SnykPluginDisposable.getInstance(), this)
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/snyk/common/EnvironmentHelper.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package snyk.common

import com.intellij.util.EnvironmentUtil
import com.intellij.util.net.HttpConfigurable
import io.snyk.plugin.pluginSettings
import snyk.pluginInfo
Expand All @@ -11,6 +12,8 @@ object EnvironmentHelper {
environment: MutableMap<String, String>,
apiToken: String,
) {
// first of all, use IntelliJ environment tool, to spice up env
environment.putAll(EnvironmentUtil.getEnvironmentMap())
val endpoint = getEndpointUrl()

val oauthEnabledEnvVar = "INTERNAL_SNYK_OAUTH_ENABLED"
Expand Down
10 changes: 6 additions & 4 deletions src/main/kotlin/snyk/common/lsp/LSCodeVisionProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.intellij.codeInsight.codeVision.CodeVisionEntry
import com.intellij.codeInsight.codeVision.CodeVisionProvider
import com.intellij.codeInsight.codeVision.CodeVisionRelativeOrdering
import com.intellij.codeInsight.codeVision.CodeVisionState
import com.intellij.codeInsight.codeVision.settings.CodeVisionGroupSettingProvider
import com.intellij.codeInsight.codeVision.ui.model.ClickableTextCodeVisionEntry
import com.intellij.openapi.application.ReadAction
import com.intellij.openapi.diagnostic.logger
Expand All @@ -16,7 +17,6 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiDocumentManager
import icons.SnykIcons
import io.snyk.plugin.isSnykCodeRunning
import io.snyk.plugin.toLanguageServerURL
import org.eclipse.lsp4j.CodeLens
import org.eclipse.lsp4j.CodeLensParams
Expand All @@ -29,11 +29,14 @@ import java.util.concurrent.TimeoutException
private const val CODELENS_FETCH_TIMEOUT = 2L

@Suppress("UnstableApiUsage")
class LSCodeVisionProvider : CodeVisionProvider<Unit> {
class LSCodeVisionProvider : CodeVisionProvider<Unit>, CodeVisionGroupSettingProvider {
private val logger = logger<LSCodeVisionProvider>()
override val defaultAnchor: CodeVisionAnchorKind = CodeVisionAnchorKind.Default
override val id = "snyk.common.lsp.LSCodeVisionProvider"
override val name = "Snyk Language Server Code Vision Provider"
override val name = "Snyk Security Language Server Code Vision Provider"
override val groupId: String = "Snyk Security"
override val groupName: String = groupId

override val relativeOrderings: List<CodeVisionRelativeOrdering>
get() = emptyList()

Expand All @@ -46,7 +49,6 @@ class LSCodeVisionProvider : CodeVisionProvider<Unit> {
override fun computeCodeVision(editor: Editor, uiData: Unit): CodeVisionState {
if (editor.project == null) return CodeVisionState.READY_EMPTY
if (!LanguageServerWrapper.getInstance().isInitialized) return CodeVisionState.READY_EMPTY
if (isSnykCodeRunning(editor.project!!)) return CodeVisionState.READY_EMPTY

return ReadAction.compute<CodeVisionState, RuntimeException> {
val project = editor.project ?: return@compute CodeVisionState.READY_EMPTY
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/snyk/common/lsp/Types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ data class ScanIssue(

fun hasAIFix(): Boolean {
return when (this.additionalData.getProductType()) {
ProductType.OSS -> false
ProductType.OSS ->
return this.additionalData.isUpgradable == true
ProductType.CODE_SECURITY, ProductType.CODE_QUALITY -> {
return this.additionalData.hasAIFix
}
Expand Down Expand Up @@ -443,7 +444,7 @@ data class IssueData(
@SerializedName("from") val from: List<String>,
@SerializedName("upgradePath") val upgradePath: List<Any>,
@SerializedName("isPatchable") val isPatchable: Boolean,
@SerializedName("isUpgradable") val isUpgradable: Boolean?,
@SerializedName("isUpgradable") val isUpgradable: Boolean,
@SerializedName("projectName") val projectName: String,
@SerializedName("displayTargetFile") val displayTargetFile: String?,
@SerializedName("matchingIssues") val matchingIssues: List<IssueData>,
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@

<notificationGroup id="Snyk" displayType="BALLOON" toolWindowId="Snyk"/>

<codeInsight.codeVisionProvider implementation="snyk.common.lsp.LSCodeVisionProvider"/>
<codeInsight.codeVisionProvider implementation="snyk.common.lsp.LSCodeVisionProvider" id="snyk.common.lsp.LSCodeVisionProvider"/>
<config.codeVisionGroupSettingProvider implementation="snyk.common.lsp.LSCodeVisionProvider" />

<externalAnnotator language="" implementationClass="snyk.code.annotator.SnykCodeAnnotator"/>
<externalAnnotator language="" implementationClass="snyk.code.annotator.SnykOSSAnnotatorLS"/>
<externalAnnotator language="" implementationClass="snyk.code.annotator.SnykOSSAnnotator"/>
</extensions>

<actions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class CliDownloaderTest {

@Test
fun `should refer to snyk static website as base url`() {
assertEquals("https://static.snyk.io", CliDownloader.BASE_URL)
assertEquals("https://downloads.snyk.io", CliDownloader.BASE_URL)
}

@Test
Expand Down

0 comments on commit 4d819c1

Please sign in to comment.