Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
mustard-mh committed Jan 24, 2025
1 parent dd33851 commit 95e8259
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package io.gitpod.toolbox.gateway

import com.jetbrains.toolbox.api.remoteDev.AbstractRemoteProviderEnvironment
import com.jetbrains.toolbox.api.remoteDev.AfterDisconnectHook
import com.jetbrains.toolbox.api.remoteDev.BeforeConnectionHook
import com.jetbrains.toolbox.api.remoteDev.EnvironmentVisibilityState
import com.jetbrains.toolbox.api.remoteDev.environments.EnvironmentContentsView
import com.jetbrains.toolbox.api.remoteDev.states.CustomRemoteEnvironmentState
Expand All @@ -16,6 +18,7 @@ import com.jetbrains.toolbox.api.ui.observables.ObservableList
import com.jetbrains.toolbox.api.ui.observables.ObservablePropertiesFactory
import io.gitpod.publicapi.experimental.v1.Workspaces.WorkspaceInstanceStatus
import io.gitpod.toolbox.auth.GitpodAuthManager
import io.gitpod.toolbox.components.SimpleButton
import io.gitpod.toolbox.service.ConnectParams
import io.gitpod.toolbox.service.GitpodPublicApiManager
import io.gitpod.toolbox.service.Utils
Expand All @@ -24,7 +27,9 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch
import java.net.URI
import java.util.concurrent.CompletableFuture
import java.util.function.Consumer

class GitpodRemoteEnvironment(
private val connectParams: ConnectParams,
Expand All @@ -44,6 +49,14 @@ class GitpodRemoteEnvironment(
lastWSEnvState.collect { lastState ->
val state = lastState.getState()
val actions = mutableListOf<ActionDescription>()
if (lastState.phase == WorkspaceInstanceStatus.Phase.PHASE_STOPPED) {
actions.add(SimpleButton("Restart") {
if (publicApi.gitpodHost.isNullOrBlank()) {
return@SimpleButton
}
Utils.localDesktopManager.openUrl(URI("https://${publicApi.gitpodHost}/start#${connectParams.workspaceId}").toURL())
})
}
actionList.clear()
actionList.addAll(actions)
listenerSet.forEach { it.consume(state) }
Expand Down Expand Up @@ -89,6 +102,34 @@ class GitpodRemoteEnvironment(
override fun dispose() {
watchWorkspaceJob?.cancel()
}

override fun getAfterDisconnectHooks(): MutableList<AfterDisconnectHook> {
return mutableListOf(object: AfterDisconnectHook {
override fun afterDisconnect() {
Utils.logger.info("=============afterDisconnect")
}
})
}

override fun getBeforeConnectionHooks(): MutableList<BeforeConnectionHook> {
return mutableListOf(object: BeforeConnectionHook {
override fun beforeConnection() {
Utils.logger.info("=============beforeConnection")
}
})
}

fun connect() {
connectionRequestListenerSet.forEach {
it.accept(true)
}
}

fun disconnect() {
connectionRequestListenerSet.forEach {
it.accept(false)
}
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class GitpodRemoteProvider(
private suspend fun setEnvironmentVisibility(connectParams: ConnectParams) {
val workspaceId = connectParams.workspaceId
Utils.logger.debug("setEnvironmentVisibility $workspaceId, $connectParams")
val obj = environmentMap[connectParams.uniqueID]
var obj = environmentMap[connectParams.uniqueID]
var (workspace) = obj ?: Pair(null, null)
if (obj == null) {
workspace = publicApi.getWorkspace(workspaceId)
Expand All @@ -60,15 +60,20 @@ class GitpodRemoteProvider(
)
environmentMap[connectParams.uniqueID] = Pair(workspace, env)
consumer.consumeEnvironments(environmentMap.values.map { it.second }, true)
obj = environmentMap[connectParams.uniqueID]
}
if (obj != null) {
val joinLinkInfo = publicApi.fetchJoinLink2Info(workspaceId, workspace!!.getIDEUrl())
// TODO(hw): verify if it's working
Utils.clientHelper.prepareClient(joinLinkInfo.ideVersion)
Utils.clientHelper.setAutoConnectOnEnvironmentReady(
connectParams.uniqueID,
joinLinkInfo.ideVersion,
joinLinkInfo.projectPath
)
obj.second.disconnect()
obj.second.connect()
}
val joinLinkInfo = publicApi.fetchJoinLink2Info(workspaceId, workspace!!.getIDEUrl())
// TODO(hw): verify if it's working
Utils.clientHelper.prepareClient(joinLinkInfo.ideVersion)
Utils.clientHelper.setAutoConnectOnEnvironmentReady(
connectParams.uniqueID,
joinLinkInfo.ideVersion,
joinLinkInfo.projectPath
)
}

private fun showWorkspacesList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class GitpodPublicApiManager(private val authManger: GitpodAuthManager) {
private var organizationApi: TeamsServiceClientInterface? = null
private var userApi: UserServiceClientInterface? = null
private var account: GitpodAccount? = null
var gitpodHost: String? = null

init {
authManger.addLogoutListener {
Expand All @@ -46,6 +47,7 @@ class GitpodPublicApiManager(private val authManger: GitpodAuthManager) {
workspaceApi = WorkspacesServiceClient(client)
organizationApi = TeamsServiceClient(client)
userApi = UserServiceClient(client)
gitpodHost = account.getHost()
}

fun watchWorkspaceStatus(workspaceId: String, consumer: (String, Workspaces.WorkspaceInstanceStatus) -> Unit): Job {
Expand Down

0 comments on commit 95e8259

Please sign in to comment.