-
Notifications
You must be signed in to change notification settings - Fork 34
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
fix: add iac ignore button [IDE-683] #634
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
297d493
fix: add iac ignore button
DariusZdroba 3ff7b95
chore: rename path variable
DariusZdroba 0203b73
Merge branch 'master' into fix/add-iac-ignore-button
DariusZdroba 0e50aba
chore: update changelog
DariusZdroba 118c51b
chore: use getContentRootPaths
DariusZdroba 99b5118
chore: use Path.relativeTo instead of trimPrefix
DariusZdroba 986defd
chore: move ignore in file specific js logic to ls
DariusZdroba 053e0cf
chore: add back toggle element show only in plugin
DariusZdroba 7031ce3
chore: add back script tag for injecting script
DariusZdroba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/main/kotlin/io/snyk/plugin/ui/jcef/IgnoreInFileHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package io.snyk.plugin.ui.jcef | ||
|
||
import com.intellij.openapi.diagnostic.LogLevel | ||
import com.intellij.openapi.diagnostic.Logger | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.ui.jcef.JBCefBrowserBase | ||
import com.intellij.ui.jcef.JBCefJSQuery | ||
import io.snyk.plugin.getContentRootPaths | ||
import io.snyk.plugin.runInBackground | ||
import io.snyk.plugin.ui.SnykBalloonNotificationHelper | ||
import org.cef.browser.CefBrowser | ||
import org.cef.browser.CefFrame | ||
import org.cef.handler.CefLoadHandlerAdapter | ||
import snyk.common.IgnoreService | ||
import snyk.common.lsp.LanguageServerWrapper | ||
import java.io.IOException | ||
import kotlin.io.path.Path | ||
import kotlin.io.path.relativeTo | ||
|
||
class IgnoreInFileHandler( | ||
private val project: Project, | ||
) { | ||
val logger = Logger.getInstance(this::class.java).apply { | ||
// tie log level to language server log level | ||
val languageServerWrapper = LanguageServerWrapper.getInstance() | ||
if (languageServerWrapper.logger.isDebugEnabled) this.setLevel(LogLevel.DEBUG) | ||
if (languageServerWrapper.logger.isTraceEnabled) this.setLevel(LogLevel.TRACE) | ||
} | ||
|
||
fun generateIgnoreInFileCommand(jbCefBrowser: JBCefBrowserBase): CefLoadHandlerAdapter { | ||
val applyIgnoreInFileQuery = JBCefJSQuery.create(jbCefBrowser) | ||
|
||
applyIgnoreInFileQuery.addHandler { value -> | ||
val params = value.split("|@", limit = 2) | ||
val issueId = params[0] // ID of issue that needs to be ignored | ||
val filePath = params[1] | ||
// Computed path that will be used in the snyk ignore command for the --path arg | ||
val computedPath = Path(filePath).relativeTo(project.getContentRootPaths().firstOrNull()!!).toString(); | ||
// Avoid blocking the UI thread | ||
runInBackground("Snyk: applying ignore...") { | ||
val result = try { | ||
applyIgnoreInFileAndSave(issueId, computedPath) | ||
} catch (e: IOException) { | ||
logger.error("Error ignoring in file: $filePath. e:$e") | ||
Result.failure(e) | ||
} catch (e: Exception) { | ||
logger.error("Unexpected error applying ignore. e:$e") | ||
Result.failure(e) | ||
} | ||
|
||
if (result.isSuccess) { | ||
val script = """ | ||
window.receiveIgnoreInFileResponse(true); | ||
""".trimIndent() | ||
jbCefBrowser.cefBrowser.executeJavaScript(script, jbCefBrowser.cefBrowser.url, 0) | ||
} else { | ||
val errorMessage = "Error ignoring in file: ${result.exceptionOrNull()?.message}" | ||
SnykBalloonNotificationHelper.showError(errorMessage, project) | ||
val errorScript = """ | ||
window.receiveIgnoreInFileResponse(false, "$errorMessage"); | ||
""".trimIndent() | ||
jbCefBrowser.cefBrowser.executeJavaScript(errorScript, jbCefBrowser.cefBrowser.url, 0) | ||
} | ||
} | ||
|
||
return@addHandler JBCefJSQuery.Response("success") | ||
} | ||
|
||
return object : CefLoadHandlerAdapter() { | ||
override fun onLoadEnd(browser: CefBrowser, frame: CefFrame, httpStatusCode: Int) { | ||
if (frame.isMain) { | ||
val script = """ | ||
(function() { | ||
if (window.applyIgnoreInFileQuery) { | ||
return; | ||
} | ||
window.applyIgnoreInFileQuery = function(value) { ${applyIgnoreInFileQuery.inject("value")} }; | ||
})(); | ||
""".trimIndent() | ||
browser.executeJavaScript(script, browser.url, 0) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun applyIgnoreInFileAndSave(issueId: String, filePath: String): Result<Unit> { | ||
val ignoreService = IgnoreService(project) | ||
if (issueId != "" && filePath != "") { | ||
ignoreService.ignoreInstance(issueId, filePath) | ||
} else { | ||
logger.error("[applyIgnoreInFileAndSave] Failed to find document for: $filePath") | ||
val errorMessage = "Failed to find document for: $filePath" | ||
SnykBalloonNotificationHelper.showError(errorMessage, project) | ||
return Result.failure(IOException(errorMessage)) | ||
} | ||
return Result.success(Unit) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/test/kotlin/io/snyk/plugin/ui/jcef/IgnoreInFileHandlerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.snyk.plugin.ui.jcef | ||
|
||
import com.intellij.openapi.application.WriteAction | ||
import com.intellij.openapi.fileEditor.FileEditorManager | ||
import com.intellij.openapi.vfs.VirtualFile | ||
import com.intellij.psi.PsiFile | ||
import com.intellij.testFramework.PlatformTestUtil | ||
import com.intellij.testFramework.fixtures.BasePlatformTestCase | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.unmockkAll | ||
import io.mockk.verify | ||
import io.snyk.plugin.resetSettings | ||
import org.eclipse.lsp4j.ExecuteCommandParams | ||
import org.eclipse.lsp4j.services.LanguageServer | ||
import snyk.common.IgnoreService | ||
import snyk.common.annotator.SnykCodeAnnotator | ||
import snyk.common.annotator.SnykIaCAnnotator | ||
import snyk.common.lsp.LanguageServerWrapper | ||
import snyk.common.lsp.commands.COMMAND_EXECUTE_CLI | ||
import java.io.File | ||
import java.nio.file.Paths | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.function.BooleanSupplier | ||
|
||
class IgnoreInFileHandlerTest : BasePlatformTestCase() { | ||
private lateinit var ignorer: IgnoreInFileHandler | ||
private val fileName = "fargate.json" | ||
val lsMock = mockk<LanguageServer>() | ||
|
||
override fun getTestDataPath(): String { | ||
val resource = SnykIaCAnnotator::class.java.getResource("/iac-test-results") | ||
requireNotNull(resource) { "Make sure that the resource $resource exists!" } | ||
return Paths.get(resource.toURI()).toString() | ||
} | ||
|
||
override fun setUp() { | ||
super.setUp() | ||
unmockkAll() | ||
resetSettings(project) | ||
val languageServerWrapper = LanguageServerWrapper.getInstance() | ||
languageServerWrapper.languageServer = lsMock | ||
languageServerWrapper.isInitialized = true | ||
ignorer = IgnoreInFileHandler(project) | ||
} | ||
|
||
fun `test issue should be ignored in file`() { | ||
every { lsMock.workspaceService.executeCommand(any()) } returns CompletableFuture.completedFuture(null) | ||
val filePath = this.getTestDataPath()+ File.separator + fileName; | ||
ignorer.applyIgnoreInFileAndSave("SNYK-CC-TF-61", filePath ) | ||
val projectBasePath = project.basePath ?: ""; | ||
|
||
// Expected args for executeCommandParams | ||
val args: List<String> = arrayListOf(projectBasePath, "ignore", "--id=SNYK-CC-TF-61", "--path=${filePath}") | ||
|
||
val executeCommandParams = ExecuteCommandParams (COMMAND_EXECUTE_CLI, args); | ||
verify { lsMock.workspaceService.executeCommand(executeCommandParams) } | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When extracting the script to migrate it to Language Server, we need acces to some variables which are locked in the scope of this self invoked function and can't be accessed outside,
we can have all the logic encapsulated in a self invoked function in Language Server, that would require some minimal changes to all IDE's that inject scripts.