Skip to content

Commit

Permalink
Make notifications about open ports display localhost links when the …
Browse files Browse the repository at this point in the history
…plugin is auto-forwarding ports
  • Loading branch information
felladrin committed Jul 15, 2022
1 parent 6eece7c commit 54943c3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.jetbrains.io.response
import java.io.OutputStreamWriter
import java.nio.file.InvalidPathException
import java.nio.file.Path
import java.util.regex.Pattern

@Suppress("UnstableApiUsage")
class GitpodCLIService : RestService() {
Expand Down Expand Up @@ -64,10 +65,30 @@ class GitpodCLIService : RestService() {
}
}
if (operation == "preview") {
val url = getStringParameter("url", urlDecoder)
var url = getStringParameter("url", urlDecoder)

if (url.isNullOrBlank()) {
return "url is missing"
}

// When it's auto-forwarding ports, we need to consider Gitpod Tasks running "gp preview".
// For example: gp preview --external $(gp url 3000)
if (manager.isAutoForwardingPorts) {
val isGitpodPortUrl = url.matches(
Regex("^https?://([0-9]{1,5})-" + System.getenv("JETBRAINS_GITPOD_WORKSPACE_HOST") + "$")
)

if (isGitpodPortUrl) {
val portsPrefix = "://"
val matcher = Pattern.compile("$portsPrefix([0-9]{1,5})").matcher(url)

if (matcher.find()) {
val portString = matcher.group().substring(portsPrefix.length)
url = "http://localhost:${portString}"
}
}
}

return withClient(request, context) { project ->
BrowserUtil.browse(url, project)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import org.jetbrains.ide.BuiltInServerManager
import java.util.concurrent.CancellationException
import java.util.concurrent.CompletableFuture

@Suppress("UnstableApiUsage", "OPT_IN_USAGE")
class GitpodClientProjectSessionTracker(
private val session: ClientProjectSession
) : Disposable {
Expand All @@ -54,19 +55,26 @@ class GitpodClientProjectSessionTracker(
}
}

private fun isExposedServedPort(port: Status.PortsStatus?): Boolean {
private fun isExposedServedPort(port: PortsStatus?): Boolean {
if (port === null) {
return false
}
return port.served && port.hasExposed()
}

private fun getForwardedPortUrl(port: PortsStatus): String {
return when {
manager.isAutoForwardingPorts -> "http://localhost:${port.localPort}"
else -> port.exposed.url
}
}

private fun showOpenServiceNotification(port: PortsStatus, offerMakePublic: Boolean = false) {
val message = "A service is available on port ${port.localPort}"
val notification = manager.notificationGroup.createNotification(message, NotificationType.INFORMATION)

val openBrowserAction = NotificationAction.createSimple("Open Browser") {
openBrowser(port.exposed.url)
val openBrowserAction = NotificationAction.createSimple("Open browser") {
openBrowser(getForwardedPortUrl(port))
}
notification.addAction(openBrowserAction)

Expand All @@ -76,7 +84,7 @@ class GitpodClientProjectSessionTracker(
makePortPublic(info.workspaceId, port)
}
}
val makePublicAction = NotificationAction.createSimple("Make Public", makePublicLambda)
val makePublicAction = NotificationAction.createSimple("Make public", makePublicLambda)
notification.addAction(makePublicAction)
}

Expand Down Expand Up @@ -113,7 +121,7 @@ class GitpodClientProjectSessionTracker(
val backendPort = BuiltInServerManager.getInstance().waitForStart().port
val serverPort = StartupUtil.getServerFuture().await().port
val ignorePorts = listOf(backendPort, serverPort, 5990)
val portsStatus = hashMapOf<Int, Status.PortsStatus>()
val portsStatus = hashMapOf<Int, PortsStatus>()

val status = StatusServiceGrpc.newStub(GitpodManager.supervisorChannel)
while (isActive) {
Expand Down Expand Up @@ -147,7 +155,7 @@ class GitpodClientProjectSessionTracker(
}

if (port.exposed.onExposed.number == Status.OnPortExposedAction.open_browser_VALUE || port.exposed.onExposed.number == Status.OnPortExposedAction.open_preview_VALUE) {
openBrowser(port.exposed.url)
openBrowser(getForwardedPortUrl(port))
continue
}

Expand All @@ -157,7 +165,7 @@ class GitpodClientProjectSessionTracker(
}

if (port.exposed.onExposed.number == Status.OnPortExposedAction.notify_private_VALUE) {
showOpenServiceNotification(port, port.exposed.visibilityValue !== PortVisibility.public_visibility_VALUE)
showOpenServiceNotification(port, port.exposed.visibilityValue != PortVisibility.public_visibility_VALUE)
continue
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class GitpodManager : Disposable {
}

val devMode = System.getenv("JB_DEV").toBoolean()
var isAutoForwardingPorts = false
private val backendKind = System.getenv("JETBRAINS_GITPOD_BACKEND_KIND") ?: "unknown"
private val backendQualifier = System.getenv("JETBRAINS_BACKEND_QUALIFIER") ?: "unknown"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package io.gitpod.jetbrains.remote.latest

import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.project.Project
import com.intellij.remoteDev.util.onTerminationOrNow
Expand Down Expand Up @@ -38,6 +39,10 @@ class GitpodPortForwardingService(private val project: Project) {
}

private fun observePortsListWhileProjectIsOpen() = application.executeOnPooledThread {
val gitpodManager = service<GitpodManager>()

gitpodManager.isAutoForwardingPorts = true

while (project.lifetime.status == LifetimeStatus.Alive) {
try {
observePortsList().get()
Expand All @@ -54,6 +59,8 @@ class GitpodPortForwardingService(private val project: Project) {

TimeUnit.SECONDS.sleep(1)
}

gitpodManager.isAutoForwardingPorts = false
}

private fun observePortsList(): CompletableFuture<Void> {
Expand Down

0 comments on commit 54943c3

Please sign in to comment.