Skip to content

Commit 5501093

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

File tree

4 files changed

+124
-30
lines changed

4 files changed

+124
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.PerClientPortForwardingManager
18+
import com.jetbrains.rd.platform.codeWithMe.portForwarding.PortForwardingDataKeys
19+
import com.jetbrains.rd.platform.codeWithMe.portForwarding.isPerClientForwardedPort
20+
import io.gitpod.jetbrains.remote.GitpodManager
21+
import io.gitpod.jetbrains.remote.GitpodPortsService
22+
import io.gitpod.supervisor.api.Status.PortsStatusRequest
23+
import io.gitpod.supervisor.api.StatusServiceGrpc
24+
import kotlinx.coroutines.launch
25+
import java.awt.datatransfer.StringSelection
26+
27+
@Suppress("ComponentNotRegistered", "UnstableApiUsage")
28+
class GitpodCopyUrlAction : AnAction() {
29+
override fun actionPerformed(e: AnActionEvent) {
30+
e.dataContext.getData(PortForwardingDataKeys.SUGGESTION)?.getSuggestedHostPort()?.let { hostPort ->
31+
service<GitpodPortsService>().getLocalHostUriFromHostPort(hostPort).let { localHostUri ->
32+
CopyPasteManager.getInstance().setContents(StringSelection(localHostUri.toString()))
33+
service<GitpodManager>()
34+
.notificationGroup
35+
.createNotification("URL copied to clipboard!", NotificationType.INFORMATION)
36+
.addAction(NotificationAction.createSimple("Open in browser") {
37+
BrowserUtil.browse(localHostUri.toString(), e.project)
38+
}).notify(e.project)
39+
}
40+
}
41+
}
42+
43+
override fun update(e: AnActionEvent) {
44+
// We want it to be disabled (greyed-out) when action comes from the Search Menu.
45+
e.presentation.isEnabled = (e.place != ActionPlaces.ACTION_SEARCH)
46+
}
47+
48+
override fun getActionUpdateThread(): ActionUpdateThread {
49+
return ActionUpdateThread.BGT
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 GitpodCopyWebUrlAction : AnAction() {
26+
override fun actionPerformed(e: AnActionEvent) {
27+
e.dataContext.getData(PortForwardingDataKeys.SUGGESTION)?.getSuggestedHostPort()?.let { hostPort ->
28+
application.coroutineScope.launch {
29+
getUrlFromPort(hostPort)?.let {
30+
CopyPasteManager.getInstance().setContents(StringSelection(it))
31+
service<GitpodManager>()
32+
.notificationGroup
33+
.createNotification("Web 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 disabled (greyed-out) when action comes from the Search Menu.
44+
e.presentation.isEnabled = (e.place != ActionPlaces.ACTION_SEARCH)
45+
}
46+
47+
override fun getActionUpdateThread(): ActionUpdateThread {
48+
return ActionUpdateThread.BGT
49+
}
50+
51+
private fun getUrlFromPort(port: Number): String? {
52+
val blockingStub = StatusServiceGrpc.newBlockingStub(GitpodManager.supervisorChannel)
53+
val request = PortsStatusRequest.newBuilder().setObserve(false).build()
54+
val response = blockingStub.portsStatus(request)
55+
while (response.hasNext()) {
56+
val portStatusResponse = response.next()
57+
for (portStatus in portStatusResponse.portsList) {
58+
if (portStatus.localPort == port) return portStatus.exposed.url
59+
}
60+
}
61+
return null
62+
}
63+
}

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@
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"
20-
text="Gitpod: Copy Port URL"
17+
<action id="io.gitpod.jetbrains.remote.latest.GitpodCopyUrlAction"
18+
class="io.gitpod.jetbrains.remote.latest.GitpodCopyUrlAction"
19+
text="Copy URL"
2120
icon="AllIcons.Actions.Copy">
2221
<add-to-group group-id="PortForwardingPortGroup" anchor="last"/>
2322
</action>
23+
<action id="io.gitpod.jetbrains.remote.latest.GitpodCopyWebUrlAction"
24+
class="io.gitpod.jetbrains.remote.latest.GitpodCopyWebUrlAction"
25+
text="Copy Web URL"
26+
icon="AllIcons.Actions.Copy">
27+
<add-to-group group-id="PortForwardingPortGroup" anchor="last"/>
28+
<add-to-group group-id="PortForwardingSuggestionGroup" anchor="last"/>
29+
</action>
2430
</actions>
25-
-->
2631
</idea-plugin>

0 commit comments

Comments
 (0)