diff --git a/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/latest/GitpodCopyUrlAction.kt b/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/latest/GitpodCopyUrlAction.kt new file mode 100644 index 00000000000000..33865694969803 --- /dev/null +++ b/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/latest/GitpodCopyUrlAction.kt @@ -0,0 +1,32 @@ +// 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.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.jetbrains.rd.platform.codeWithMe.portForwarding.PortForwardingDataKeys +import io.gitpod.jetbrains.remote.GitpodPortsService +import java.awt.datatransfer.StringSelection + +@Suppress("ComponentNotRegistered", "UnstableApiUsage") +class GitpodCopyUrlAction : AnAction() { + override fun actionPerformed(e: AnActionEvent) { + e.dataContext.getData(PortForwardingDataKeys.SUGGESTION)?.getSuggestedHostPort()?.let { hostPort -> + service().getLocalHostUriFromHostPort(hostPort).let { localHostUri -> + CopyPasteManager.getInstance().setContents(StringSelection(localHostUri.toString())) + } + } + } + + override fun update(e: AnActionEvent) { + e.presentation.isEnabled = (e.place != ActionPlaces.ACTION_SEARCH) + } + + override fun getActionUpdateThread() = ActionUpdateThread.BGT +} diff --git a/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/latest/GitpodCopyWebUrlAction.kt b/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/latest/GitpodCopyWebUrlAction.kt new file mode 100644 index 00000000000000..8ee3d449a8d2c0 --- /dev/null +++ b/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/latest/GitpodCopyWebUrlAction.kt @@ -0,0 +1,50 @@ +// 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.ActionPlaces +import com.intellij.openapi.actionSystem.ActionUpdateThread +import com.intellij.openapi.actionSystem.AnAction +import com.intellij.openapi.actionSystem.AnActionEvent +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 GitpodCopyWebUrlAction : AnAction() { + override fun actionPerformed(e: AnActionEvent) { + e.dataContext.getData(PortForwardingDataKeys.SUGGESTION)?.getSuggestedHostPort()?.let { hostPort -> + application.coroutineScope.launch { + getUrlFromPort(hostPort)?.let { + CopyPasteManager.getInstance().setContents(StringSelection(it)) + } + } + } + } + + override fun update(e: AnActionEvent) { + e.presentation.isEnabled = (e.place != ActionPlaces.ACTION_SEARCH) + } + + override fun getActionUpdateThread() = 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..f29832dd1ef3f5 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,19 @@ -