Skip to content

Commit

Permalink
feat: add sendFeatureFlagCommand for feature flag status query
Browse files Browse the repository at this point in the history
  • Loading branch information
cat2608 committed Mar 19, 2024
1 parent d3f1dbc commit 249dc95
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
27 changes: 27 additions & 0 deletions src/main/kotlin/snyk/common/lsp/LanguageServerWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import org.eclipse.lsp4j.WorkspaceFoldersChangeEvent
import org.eclipse.lsp4j.jsonrpc.Launcher
import org.eclipse.lsp4j.launch.LSPLauncher
import org.eclipse.lsp4j.services.LanguageServer
import org.json.JSONObject
import snyk.common.EnvironmentHelper
import snyk.common.getEndpointUrl
import snyk.common.lsp.commands.ScanDoneEvent
Expand Down Expand Up @@ -225,6 +226,32 @@ class LanguageServerWrapper(
}
}

fun sendFeatureFlagCommand(featureFlag: String): Boolean {
ensureLanguageServerInitialized()
try {
val param = ExecuteCommandParams()
param.command = "snyk.getFeatureFlagStatus"
param.arguments = listOf(featureFlag)
val result = languageServer.workspaceService.executeCommand(param).get()

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

if (ok) {
logger.info("Feature flag $featureFlag is enabled.")
return true
} else {
logger.warn("Feature flag $featureFlag is disabled. Message: $userMessage")
return false
}

} catch (e: Exception) {
logger.error("Error while checking feature flag: ${e.message}", e)
return false
}
}

private fun sendFolderScanCommand(folder: String) {
try {
val param = ExecuteCommandParams()
Expand Down
16 changes: 11 additions & 5 deletions src/main/kotlin/snyk/common/lsp/SnykLanguageClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,18 @@ class SnykLanguageClient : LanguageClient {
check(snykScan.product == "code") { "Expected Snyk Code scan result" }
if (snykScan.issues.isNullOrEmpty()) return emptyMap()

var includeIgnoredIssues = pluginSettings().ignoredIssuesEnabled
var includeOpenedIssues = pluginSettings().openIssuesEnabled
val includeIgnoredIssues = pluginSettings().ignoredIssuesEnabled
val includeOpenedIssues = pluginSettings().openIssuesEnabled

val map = snykScan.issues
// TODO: check feature flag before filtering based on ignores
.filter { it.isVisible(includeOpenedIssues, includeIgnoredIssues) }
val isFeatureFlagEnabled = LanguageServerWrapper.getInstance().sendFeatureFlagCommand("snykCodeConsistentIgnores")

val processedIssues = if (isFeatureFlagEnabled) {
snykScan.issues.filter { it.isVisible(includeOpenedIssues, includeIgnoredIssues) }
} else {
snykScan.issues
}

val map = processedIssues
.groupBy { it.filePath }
.mapNotNull { (file, issues) -> SnykCodeFile(project, file.toVirtualFile()) to issues.sorted() }
.filter { it.second.isNotEmpty() }
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/snyk/common/lsp/LanguageServerWrapperTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,19 @@ class LanguageServerWrapperTest {
assertEquals("${settings.ignoreUnknownCA}", actual.insecure)
assertEquals(getCliFile().absolutePath, actual.cliPath)
}

@Test
fun `sendFeatureFlagCommand should return true if feature flag is enabled`() {
// Arrange
val featureFlag = "testFeatureFlag"
val commandResponse = CompletableFuture.completedFuture("{\"ok\": true}" as Any)
every { lsMock.workspaceService.executeCommand(any<ExecuteCommandParams>()) } returns commandResponse

// Act
val result = cut.sendFeatureFlagCommand(featureFlag) // This is a blocking call, so it will return the result immediately

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

0 comments on commit 249dc95

Please sign in to comment.