|
| 1 | +// Copyright (c) 2022 Gitpod GmbH. All rights reserved. |
| 2 | +// Licensed under the GNU Affero General Public License (AGPL). |
| 3 | +// See License-AGPL.txt in the project root for license information. |
| 4 | + |
| 5 | +package io.gitpod.jetbrains.remote.latest |
| 6 | + |
| 7 | +import com.intellij.ide.BrowserUtil |
| 8 | +import com.intellij.notification.NotificationAction |
| 9 | +import com.intellij.notification.NotificationType |
| 10 | +import com.intellij.openapi.actionSystem.ActionPlaces |
| 11 | +import com.intellij.openapi.actionSystem.ActionUpdateThread |
| 12 | +import com.intellij.openapi.actionSystem.AnAction |
| 13 | +import com.intellij.openapi.actionSystem.AnActionEvent |
| 14 | +import com.intellij.openapi.components.service |
| 15 | +import com.intellij.openapi.ide.CopyPasteManager |
| 16 | +import com.intellij.util.application |
| 17 | +import com.jetbrains.rd.platform.codeWithMe.portForwarding.PortForwardingDataKeys |
| 18 | +import io.gitpod.jetbrains.remote.GitpodManager |
| 19 | +import io.gitpod.supervisor.api.Status.PortsStatusRequest |
| 20 | +import io.gitpod.supervisor.api.StatusServiceGrpc |
| 21 | +import kotlinx.coroutines.launch |
| 22 | +import java.awt.datatransfer.StringSelection |
| 23 | + |
| 24 | +@Suppress("ComponentNotRegistered", "UnstableApiUsage") |
| 25 | +class GitpodCopyPortUrlAction : AnAction() { |
| 26 | + override fun actionPerformed(e: AnActionEvent) { |
| 27 | + e.dataContext.getData(PortForwardingDataKeys.PORT)?.let { forwardedPort -> |
| 28 | + application.coroutineScope.launch { |
| 29 | + getUrlFromPort(forwardedPort.hostPortNumber)?.let { |
| 30 | + CopyPasteManager.getInstance().setContents(StringSelection(it)) |
| 31 | + service<GitpodManager>() |
| 32 | + .notificationGroup |
| 33 | + .createNotification("Port URL copied to clipboard!", NotificationType.INFORMATION) |
| 34 | + .addAction(NotificationAction.createSimple("Open in browser") { |
| 35 | + BrowserUtil.browse(it, e.project) |
| 36 | + }).notify(e.project) |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + override fun update(e: AnActionEvent) { |
| 43 | + // We want it to be enabled only when action comes from the Port Forwarding Suggestion Popup. |
| 44 | + // In other places, like the Search Actions Menu, we want it disabled (greyed-out). |
| 45 | + e.presentation.isEnabled = (e.place == ActionPlaces.POPUP) |
| 46 | + } |
| 47 | + |
| 48 | + override fun getActionUpdateThread(): ActionUpdateThread { |
| 49 | + return ActionUpdateThread.BGT |
| 50 | + } |
| 51 | + |
| 52 | + private fun getUrlFromPort(port: Number): String? { |
| 53 | + val blockingStub = StatusServiceGrpc.newBlockingStub(GitpodManager.supervisorChannel) |
| 54 | + val request = PortsStatusRequest.newBuilder().setObserve(false).build() |
| 55 | + val response = blockingStub.portsStatus(request) |
| 56 | + while (response.hasNext()) { |
| 57 | + val portStatusResponse = response.next() |
| 58 | + for (portStatus in portStatusResponse.portsList) { |
| 59 | + if (portStatus.localPort == port) return portStatus.exposed.url |
| 60 | + } |
| 61 | + } |
| 62 | + return null |
| 63 | + } |
| 64 | +} |
0 commit comments