generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from returntocorp/austin/nudge-and-install
feat: nudge and install
- Loading branch information
Showing
22 changed files
with
349 additions
and
58 deletions.
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
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/com/semgrep/idea/actions/DismissLoginNudgeAction.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,13 @@ | ||
package com.semgrep.idea.actions | ||
|
||
import com.intellij.notification.Notification | ||
import com.intellij.openapi.actionSystem.AnAction | ||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import com.semgrep.idea.settings.AppState | ||
|
||
class DismissLoginNudgeAction(private val notification: Notification) : AnAction("Don't ask again") { | ||
override fun actionPerformed(e: AnActionEvent) { | ||
AppState.getInstance().pluginState.dismissedLoginNudge = true | ||
notification.expire() | ||
} | ||
} |
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 |
---|---|---|
@@ -1,18 +1,20 @@ | ||
package com.semgrep.idea.actions | ||
|
||
import com.intellij.ide.BrowserUtil | ||
import com.intellij.notification.Notification | ||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import com.intellij.platform.lsp.api.LspServer | ||
import com.semgrep.idea.lsp.custom_notifications.LoginFinishRequest | ||
import com.semgrep.idea.lsp.custom_requests.LoginRequest | ||
|
||
class LoginAction : LspAction("Sign In to Semgrep Code") { | ||
class LoginAction(private val notification: Notification? = null) : LspAction("Sign In to Semgrep Code") { | ||
override fun actionPerformed(e: AnActionEvent, servers: List<com.semgrep.idea.lsp.SemgrepLspServer>) { | ||
val loginRequest = LoginRequest(servers.first()) | ||
val response = (servers.first() as LspServer).requestExecutor.sendRequestSync(loginRequest) ?: return | ||
BrowserUtil.browse(response.url) | ||
servers.forEach { | ||
it.requestExecutor.sendNotification(LoginFinishRequest(it, response)) | ||
LoginFinishRequest(it, response).sendNotification() | ||
} | ||
notification?.expire() | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.semgrep.idea.lsp | ||
|
||
import com.intellij.execution.configurations.GeneralCommandLine | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.util.text.SemVer | ||
import com.semgrep.idea.settings.AppState | ||
import com.semgrep.idea.settings.SemgrepPluginSettings | ||
import com.semgrep.idea.ui.SemgrepNotifier | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.jsonObject | ||
import java.net.URI | ||
import java.net.http.HttpClient | ||
import java.net.http.HttpRequest | ||
import java.net.http.HttpResponse | ||
|
||
object SemgrepInstaller { | ||
enum class InstallOption(val binary: String, val installCommand: String) { | ||
BREW("brew", "brew install semgrep"), | ||
PIP("pip3", "pip3 install semgrep"); | ||
|
||
fun isInstalled(): Boolean { | ||
return which(binary) != null | ||
} | ||
|
||
fun install(project: Project) { | ||
val cmd = GeneralCommandLine("sh", "-c", installCommand) | ||
val process = cmd.createProcess() | ||
val ret = process.waitFor() | ||
val out = process.inputStream.bufferedReader().readText() | ||
val semgrepNotifier = SemgrepNotifier(project) | ||
if (ret == 0) { | ||
semgrepNotifier.notifyInstallSuccess() | ||
SemgrepLspServer.startServersIfNeeded(project) | ||
} else { | ||
semgrepNotifier.notifyInstallFailure(out, ret) | ||
} | ||
} | ||
} | ||
|
||
fun semgrepInstalled(): Boolean { | ||
val defaultPath = SemgrepPluginSettings().path | ||
val state = AppState.getInstance().appSettings | ||
return state.path != defaultPath && which(defaultPath) != null | ||
} | ||
|
||
fun getCliVersion(): SemVer? { | ||
val cmd = GeneralCommandLine("semgrep", "--version") | ||
val process = cmd.createProcess() | ||
process.waitFor() | ||
val out = process.inputStream.bufferedReader().readText().trim() | ||
return SemVer.parseFromText(out) | ||
} | ||
|
||
fun getMostUpToDateCliVersion(): SemVer? { | ||
val client = HttpClient.newBuilder().build() | ||
val request = HttpRequest.newBuilder() | ||
.uri(URI.create("https://semgrep.dev/api/check-version")) | ||
.build() | ||
val response = client.send(request, HttpResponse.BodyHandlers.ofString()).body() | ||
// Can't figure out how to actually parse this to a string, not a quoted string. Oh well | ||
val version = Json.parseToJsonElement(response).jsonObject.get("latest").toString().replace("\"", "") | ||
return SemVer.parseFromText(version) | ||
} | ||
|
||
fun which(binary: String): String? { | ||
val cmd = GeneralCommandLine("which", binary) | ||
|
||
val process = cmd.createProcess() | ||
process.waitFor() | ||
val result = process.inputStream.bufferedReader().readLine() | ||
|
||
return if (result == "") null else result | ||
} | ||
|
||
fun getInstallOptions(): List<InstallOption> { | ||
return InstallOption.values().filter { it.isInstalled() } | ||
} | ||
|
||
|
||
} |
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
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/com/semgrep/idea/lsp/SemgrepLspServerListener.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,36 @@ | ||
package com.semgrep.idea.lsp | ||
|
||
import com.intellij.openapi.project.Project | ||
import com.intellij.platform.lsp.api.LspServerListener | ||
import com.intellij.util.text.SemVer | ||
import com.semgrep.idea.lsp.custom_requests.LoginStatusRequest | ||
import com.semgrep.idea.settings.AppState | ||
import com.semgrep.idea.ui.SemgrepNotifier | ||
import org.eclipse.lsp4j.InitializeResult | ||
|
||
class SemgrepLspServerListener(val project: Project) : LspServerListener { | ||
override fun serverInitialized(params: InitializeResult) { | ||
super.serverInitialized(params) | ||
val settings = AppState.getInstance() | ||
val servers = SemgrepLspServer.getInstances(project) | ||
val first = servers.firstOrNull() | ||
if (first != null && !settings.pluginState.dismissedLoginNudge) { | ||
val loginStatusRequest = LoginStatusRequest(first) | ||
loginStatusRequest.sendRequest().handle({ it, _ -> | ||
if (!it.loggedIn) { | ||
SemgrepNotifier(project).notifyLoginNudge() | ||
} | ||
}) | ||
val current = SemgrepInstaller.getCliVersion() | ||
val needed = SemVer.parseFromText(SemgrepLspServer.MIN_SEMGREP_VERSION) | ||
val latest = SemgrepInstaller.getMostUpToDateCliVersion() | ||
if (current != null) { | ||
if (needed != null && current < needed) { | ||
SemgrepNotifier(project).notifyUpdateNeeded(needed, current) | ||
} else if (latest != null && current < latest) { | ||
SemgrepNotifier(project).notifyUpdateAvailable(current, latest) | ||
} | ||
} | ||
} | ||
} | ||
} |
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
29 changes: 0 additions & 29 deletions
29
src/main/kotlin/com/semgrep/idea/settings/AppSettingsState.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.