From c39bda27d3d380cba7fa0d293c1468c37c33441c Mon Sep 17 00:00:00 2001 From: Abdelrahman Shawki Hassan Date: Tue, 15 Oct 2024 18:36:11 +0200 Subject: [PATCH] feat: load issue html on demand --- src/main/kotlin/io/snyk/plugin/Utils.kt | 2 +- .../toolwindow/panels/JCEFDescriptionPanel.kt | 2 +- .../snyk/common/lsp/LanguageServerWrapper.kt | 17 +++++++++++++++++ src/main/kotlin/snyk/common/lsp/Types.kt | 14 +++++++++++--- .../kotlin/snyk/common/lsp/commands/Commands.kt | 1 + 5 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/io/snyk/plugin/Utils.kt b/src/main/kotlin/io/snyk/plugin/Utils.kt index 7e2c5e912..121ccf263 100644 --- a/src/main/kotlin/io/snyk/plugin/Utils.kt +++ b/src/main/kotlin/io/snyk/plugin/Utils.kt @@ -391,7 +391,7 @@ fun String.toVirtualFile(): VirtualFile { return if (!this.startsWith("file://")) { StandardFileSystems.local().refreshAndFindFileByPath(this) ?: throw FileNotFoundException(this) } else { - VirtualFileManager.getInstance().refreshAndFindFileByNioPath(convertUriToPath(this.toVirtualFileURL())) + VirtualFileManager.getInstance().refreshAndFindFileByNioPath(convertUriToPath(this)) ?: throw FileNotFoundException(this) } } diff --git a/src/main/kotlin/io/snyk/plugin/ui/toolwindow/panels/JCEFDescriptionPanel.kt b/src/main/kotlin/io/snyk/plugin/ui/toolwindow/panels/JCEFDescriptionPanel.kt index 9268a1021..58d656289 100644 --- a/src/main/kotlin/io/snyk/plugin/ui/toolwindow/panels/JCEFDescriptionPanel.kt +++ b/src/main/kotlin/io/snyk/plugin/ui/toolwindow/panels/JCEFDescriptionPanel.kt @@ -155,7 +155,7 @@ class SuggestionDescriptionPanelFromLS( } fun getCustomCssAndScript(): String { - var html = issue.details().ifBlank { issue.additionalData.customUIContent } + var html = issue.details() val ideScript = getCustomScript() // TODO: remove custom stylesheets, deliver variables from LS, replace variables with colors diff --git a/src/main/kotlin/snyk/common/lsp/LanguageServerWrapper.kt b/src/main/kotlin/snyk/common/lsp/LanguageServerWrapper.kt index fa4e235cc..0e8464c18 100644 --- a/src/main/kotlin/snyk/common/lsp/LanguageServerWrapper.kt +++ b/src/main/kotlin/snyk/common/lsp/LanguageServerWrapper.kt @@ -60,6 +60,7 @@ import snyk.common.lsp.commands.COMMAND_LOGIN import snyk.common.lsp.commands.COMMAND_LOGOUT import snyk.common.lsp.commands.COMMAND_REPORT_ANALYTICS import snyk.common.lsp.commands.COMMAND_WORKSPACE_FOLDER_SCAN +import snyk.common.lsp.commands.SNYK_GENERATE_ISSUE_DESCRIPTION import snyk.common.lsp.commands.ScanDoneEvent import snyk.common.lsp.progress.ProgressManager import snyk.common.lsp.settings.LanguageServerSettings @@ -524,6 +525,22 @@ class LanguageServerWrapper( } } + fun generateIssueDescription(issueID: String): String? { + if (!ensureLanguageServerInitialized()) return null + try { + val generateIssueCommand = ExecuteCommandParams(SNYK_GENERATE_ISSUE_DESCRIPTION, listOf(issueID)) + val result = + languageServer.workspaceService + .executeCommand(generateIssueCommand) + .get(2, TimeUnit.SECONDS) + .toString() + return result + } catch (e: TimeoutException) { + logger.warn("could not generate html description", e) + return null + } + } + fun logout() { if (!ensureLanguageServerInitialized()) return val cmd = ExecuteCommandParams(COMMAND_LOGOUT, emptyList()) diff --git a/src/main/kotlin/snyk/common/lsp/Types.kt b/src/main/kotlin/snyk/common/lsp/Types.kt index 095d0863f..626705b78 100644 --- a/src/main/kotlin/snyk/common/lsp/Types.kt +++ b/src/main/kotlin/snyk/common/lsp/Types.kt @@ -254,11 +254,19 @@ data class ScanIssue( fun details(): String { return when (this.filterableIssueType) { - OPEN_SOURCE -> this.additionalData.details ?: "" - else -> this.additionalData.details ?: "" + OPEN_SOURCE, CODE_SECURITY, CODE_QUALITY -> getHtml(this.additionalData.details) + INFRASTRUCTURE_AS_CODE -> getHtml(this.additionalData.customUIContent) + else -> "" } } + private fun getHtml(details: String?): String { + if (details.isNullOrEmpty()) { + return LanguageServerWrapper.getInstance().generateIssueDescription(this.id) ?: "" + } + return details + } + fun annotationMessage(): String { return when (this.filterableIssueType) { OPEN_SOURCE -> this.title + " in " + this.additionalData.packageName + " id: " + this.ruleId() @@ -445,7 +453,7 @@ data class IssueData( @SerializedName("resolve") val resolve: String, @SerializedName("path") val path: List, @SerializedName("references") val references: List, - @SerializedName("customUIContent") val customUIContent: String, + @SerializedName("customUIContent") val customUIContent: String?, // all @SerializedName("key") val key: String, diff --git a/src/main/kotlin/snyk/common/lsp/commands/Commands.kt b/src/main/kotlin/snyk/common/lsp/commands/Commands.kt index 3a8affa41..536967f48 100644 --- a/src/main/kotlin/snyk/common/lsp/commands/Commands.kt +++ b/src/main/kotlin/snyk/common/lsp/commands/Commands.kt @@ -11,3 +11,4 @@ internal const val COMMAND_GET_SETTINGS_SAST_ENABLED = "snyk.getSettingsSastEnab internal const val COMMAND_COPY_AUTH_LINK = "snyk.copyAuthLink" internal const val COMMAND_CODE_FIX_DIFFS = "snyk.code.fixDiffs" internal const val COMMAND_CODE_SUBMIT_FIX_FEEDBACK = "snyk.code.submitFixFeedback" +internal const val SNYK_GENERATE_ISSUE_DESCRIPTION = "snyk.generateIssueDescription"