Skip to content

Commit

Permalink
Add action Copy Port URL on forwarded ports in terminals from JetBr…
Browse files Browse the repository at this point in the history
…ains EAP IDEs

Co-authored-by: Andrea Falzetti <[email protected]>
  • Loading branch information
felladrin and Andrea Falzetti committed Nov 2, 2022
1 parent 84e1f36 commit d39d5d6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -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<GitpodManager>()
.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
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
<projectService serviceImplementation="io.gitpod.jetbrains.remote.GitpodTerminalService" client="controller" preload="true"/>
<projectService serviceImplementation="io.gitpod.jetbrains.remote.latest.GitpodPortForwardingService" client="controller" preload="true"/>
</extensions>
<!--
<actions>
<action id="io.gitpod.jetbrains.remote.latest.GitpodPortsActionCopyUrl"
class="io.gitpod.jetbrains.remote.latest.GitpodPortsActionCopyUrl"
<action id="io.gitpod.jetbrains.remote.latest.GitpodCopyPortUrlAction"
class="io.gitpod.jetbrains.remote.latest.GitpodCopyPortUrlAction"
text="Gitpod: Copy Port URL"
icon="AllIcons.Actions.Copy">
<add-to-group group-id="PortForwardingPortGroup" anchor="last"/>
</action>
</actions>
-->
</idea-plugin>

0 comments on commit d39d5d6

Please sign in to comment.