Skip to content

Commit

Permalink
fix: fetch feature flag status during LanguageServerWrapper initialis…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
cat2608 committed Mar 21, 2024
1 parent 18f80f6 commit e784be7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import java.util.UUID
)
class SnykApplicationSettingsStateService : PersistentStateComponent<SnykApplicationSettingsStateService> {

var isGlobalIgnoresFeatureEnabled = false
var cliBaseDownloadURL: String = "https://static.snyk.io"
var cliPath: String = getPluginPath() + separator + Platform.current().snykWrapperFileName
var manageBinariesAutomatically: Boolean = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ class SnykProjectSettingsConfigurable(val project: Project) : SearchableConfigur
snykProjectSettingsService?.additionalParameters = snykSettingsDialog.getAdditionalParameters()
}

val params = DidChangeConfigurationParams(LanguageServerWrapper.getInstance().getSettings())
LanguageServerWrapper.getInstance().languageServer.workspaceService.didChangeConfiguration(params)
val wrapper = LanguageServerWrapper.getInstance()
val params = DidChangeConfigurationParams(wrapper.getSettings())
wrapper.languageServer.workspaceService.didChangeConfiguration(params)

if (rescanNeeded) {
getSnykToolWindowPanel(project)?.cleanUiAndCaches()
Expand Down
17 changes: 11 additions & 6 deletions src/main/kotlin/snyk/common/lsp/LanguageServerWrapper.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package snyk.common.lsp

import com.intellij.ide.impl.ProjectUtil
import com.intellij.openapi.application.invokeLater
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
Expand Down Expand Up @@ -98,8 +99,8 @@ class LanguageServerWrapper(
isInitializing = true
val snykLanguageClient = SnykLanguageClient()
languageClient = snykLanguageClient
val logLevel = if (snykLanguageClient.logger.isDebugEnabled) "debug" else "info"
val cmd = listOf(lsPath, "language-server", "-l", logLevel, "-f", "/tmp/snyk-ls.log")
val logLevel = if (snykLanguageClient.logger.isDebugEnabled) "trace" else "info"
val cmd = listOf(lsPath, "language-server", "-l", logLevel)

val processBuilder = ProcessBuilder(cmd)
pluginSettings().token?.let { EnvironmentHelper.updateEnvironment(processBuilder.environment(), it) }
Expand All @@ -120,6 +121,9 @@ class LanguageServerWrapper(
} finally {
isInitializing = false
}
// update feature flags
pluginSettings().isGlobalIgnoresFeatureEnabled =
sendFeatureFlagCommand("snykCodeConsistentIgnores")
}

fun shutdown(): Future<*> {
Expand Down Expand Up @@ -232,11 +236,12 @@ class LanguageServerWrapper(
val param = ExecuteCommandParams()
param.command = "snyk.getFeatureFlagStatus"
param.arguments = listOf(featureFlag)
val result = languageServer.workspaceService.executeCommand(param).get()
val result = languageServer.workspaceService.executeCommand(param).get(5, TimeUnit.SECONDS)

val resultMap = result as? Map<*, *>
val ok = resultMap?.get("ok") as? Boolean ?: false
val userMessage = resultMap?.get("userMessage") as? String ?: "No message provided"

val resultJson = JSONObject(result.toString())
val ok = resultJson.getBoolean("ok")
val userMessage = resultJson.optString("userMessage")

if (ok) {
logger.info("Feature flag $featureFlag is enabled.")
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/snyk/common/lsp/SnykLanguageClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ class SnykLanguageClient : LanguageClient {
val includeIgnoredIssues = pluginSettings().ignoredIssuesEnabled
val includeOpenedIssues = pluginSettings().openIssuesEnabled

val isFeatureFlagEnabled = LanguageServerWrapper.getInstance().sendFeatureFlagCommand("snykCodeConsistentIgnores")
// get enablement status from settings
val isFeatureFlagEnabled = pluginSettings().isGlobalIgnoresFeatureEnabled

val processedIssues = if (isFeatureFlagEnabled) {
snykScan.issues.filter { it.isVisible(includeOpenedIssues, includeIgnoredIssues) }
Expand Down
13 changes: 10 additions & 3 deletions src/test/java/snyk/common/lsp/LanguageServerWrapperTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,22 @@ class LanguageServerWrapperTest {
@Test
fun `sendFeatureFlagCommand should return true if feature flag is enabled`() {
// Arrange
cut.languageClient = mockk(relaxed = true)
val processMock = mockk<Process>(relaxed = true)
cut.process = processMock
val featureFlag = "testFeatureFlag"
val commandResponse = CompletableFuture.completedFuture("{\"ok\": true}" as Any)
every { lsMock.workspaceService.executeCommand(any<ExecuteCommandParams>()) } returns commandResponse
every { processMock.info().startInstant().isPresent } returns true
every { processMock.isAlive } returns true

every {
lsMock.workspaceService.executeCommand(any<ExecuteCommandParams>())
} returns CompletableFuture.completedFuture(mapOf("ok" to true))

// Act
val result = cut.sendFeatureFlagCommand(featureFlag)

// Assert
verify { lsMock.workspaceService.executeCommand(match { it.arguments.contains(featureFlag) }) }
assertEquals(true, result)

}
}

0 comments on commit e784be7

Please sign in to comment.