diff --git a/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/latest/GitpodCopyPortUrlAction.kt b/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/latest/GitpodCopyPortUrlAction.kt new file mode 100644 index 00000000000000..f1888fb65c8fd1 --- /dev/null +++ b/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/latest/GitpodCopyPortUrlAction.kt @@ -0,0 +1,64 @@ +// Copyright (c) 2022 Gitpod GmbH. All rights reserved. +// Licensed under the GNU Affero General Public License (AGPL). +// See License-AGPL.txt in the project root for license information. + +package io.gitpod.jetbrains.remote.latest + +import com.intellij.ide.BrowserUtil +import com.intellij.notification.NotificationAction +import com.intellij.notification.NotificationType +import com.intellij.openapi.actionSystem.ActionPlaces +import com.intellij.openapi.actionSystem.ActionUpdateThread +import com.intellij.openapi.actionSystem.AnAction +import com.intellij.openapi.actionSystem.AnActionEvent +import com.intellij.openapi.components.service +import com.intellij.openapi.ide.CopyPasteManager +import com.intellij.util.application +import com.jetbrains.rd.platform.codeWithMe.portForwarding.PortForwardingDataKeys +import io.gitpod.jetbrains.remote.GitpodManager +import io.gitpod.supervisor.api.Status.PortsStatusRequest +import io.gitpod.supervisor.api.StatusServiceGrpc +import kotlinx.coroutines.launch +import java.awt.datatransfer.StringSelection + +@Suppress("ComponentNotRegistered", "UnstableApiUsage") +class GitpodCopyPortUrlAction : AnAction() { + override fun actionPerformed(e: AnActionEvent) { + e.dataContext.getData(PortForwardingDataKeys.PORT)?.let { forwardedPort -> + application.coroutineScope.launch { + getUrlFromPort(forwardedPort.hostPortNumber)?.let { + CopyPasteManager.getInstance().setContents(StringSelection(it)) + service() + .notificationGroup + .createNotification("Port URL copied to clipboard!", NotificationType.INFORMATION) + .addAction(NotificationAction.createSimple("Open in browser") { + BrowserUtil.browse(it, e.project) + }).notify(e.project) + } + } + } + } + + override fun update(e: AnActionEvent) { + // We want it to be enabled only when action comes from the Port Forwarding Suggestion Popup. + // In other places, like the Search Actions Menu, we want it disabled (greyed-out). + e.presentation.isEnabled = (e.place == ActionPlaces.POPUP) + } + + override fun getActionUpdateThread(): ActionUpdateThread { + return ActionUpdateThread.BGT + } + + private fun getUrlFromPort(port: Number): String? { + val blockingStub = StatusServiceGrpc.newBlockingStub(GitpodManager.supervisorChannel) + val request = PortsStatusRequest.newBuilder().setObserve(false).build() + val response = blockingStub.portsStatus(request) + while (response.hasNext()) { + val portStatusResponse = response.next() + for (portStatus in portStatusResponse.portsList) { + if (portStatus.localPort == port) return portStatus.exposed.url + } + } + return null + } +} diff --git a/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/latest/GitpodPortsActionCopyUrl.kt b/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/latest/GitpodPortsActionCopyUrl.kt deleted file mode 100644 index 83a54087580936..00000000000000 --- a/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/latest/GitpodPortsActionCopyUrl.kt +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (c) 2022 Gitpod GmbH. All rights reserved. -// Licensed under the GNU Affero General Public License (AGPL). -// See License-AGPL.txt in the project root for license information. - -package io.gitpod.jetbrains.remote.latest - -import com.intellij.openapi.actionSystem.AnAction -import com.intellij.openapi.actionSystem.AnActionEvent -import com.intellij.openapi.diagnostic.thisLogger -import com.intellij.openapi.ide.CopyPasteManager -import com.jetbrains.rd.platform.codeWithMe.portForwarding.PortForwardingDataKeys -import java.awt.datatransfer.StringSelection - -@Suppress("ComponentNotRegistered", "UnstableApiUsage") -class GitpodPortsActionCopyUrl : AnAction() { - override fun actionPerformed(e: AnActionEvent) { - val port = e.dataContext.getData(PortForwardingDataKeys.PORT) - if (port != null) { - thisLogger().warn("gitpod: Exec GitpodPortsActionCopyUrl: ${port.hostPortNumber}") - CopyPasteManager.getInstance().setContents(StringSelection(port.hostPortNumber.toString())) - } else { - thisLogger().warn("gitpod: Exec: GitpodPortsActionCopyUrl: error unknown port") - } - } -} diff --git a/components/ide/jetbrains/backend-plugin/src/main/resources-latest/META-INF/extensions.xml b/components/ide/jetbrains/backend-plugin/src/main/resources-latest/META-INF/extensions.xml index 1cd44f68a250dc..ec20ef184fe10b 100644 --- a/components/ide/jetbrains/backend-plugin/src/main/resources-latest/META-INF/extensions.xml +++ b/components/ide/jetbrains/backend-plugin/src/main/resources-latest/META-INF/extensions.xml @@ -13,14 +13,12 @@ -