Skip to content

Commit d39d5d6

Browse files
felladrinAndrea Falzetti
and
Andrea Falzetti
committed
Add action Copy Port URL on forwarded ports in terminals from JetBrains EAP IDEs
Co-authored-by: Andrea Falzetti <[email protected]>
1 parent 84e1f36 commit d39d5d6

File tree

3 files changed

+66
-29
lines changed

3 files changed

+66
-29
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
}

Diff for: components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/latest/GitpodPortsActionCopyUrl.kt

-25
This file was deleted.

Diff for: components/ide/jetbrains/backend-plugin/src/main/resources-latest/META-INF/extensions.xml

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313
<projectService serviceImplementation="io.gitpod.jetbrains.remote.GitpodTerminalService" client="controller" preload="true"/>
1414
<projectService serviceImplementation="io.gitpod.jetbrains.remote.latest.GitpodPortForwardingService" client="controller" preload="true"/>
1515
</extensions>
16-
<!--
1716
<actions>
18-
<action id="io.gitpod.jetbrains.remote.latest.GitpodPortsActionCopyUrl"
19-
class="io.gitpod.jetbrains.remote.latest.GitpodPortsActionCopyUrl"
17+
<action id="io.gitpod.jetbrains.remote.latest.GitpodCopyPortUrlAction"
18+
class="io.gitpod.jetbrains.remote.latest.GitpodCopyPortUrlAction"
2019
text="Gitpod: Copy Port URL"
2120
icon="AllIcons.Actions.Copy">
2221
<add-to-group group-id="PortForwardingPortGroup" anchor="last"/>
2322
</action>
2423
</actions>
25-
-->
2624
</idea-plugin>

0 commit comments

Comments
 (0)