Skip to content

Commit 3426801

Browse files
committed
fix logger caller stack
1 parent 4a4c3fe commit 3426801

File tree

6 files changed

+87
-54
lines changed

6 files changed

+87
-54
lines changed

components/ide/jetbrains/toolbox/src/main/kotlin/io/gitpod/toolbox/auth/GitpodAuthManager.kt

+10-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
package io.gitpod.toolbox.auth
66

7-
import com.connectrpc.Code
87
import com.connectrpc.ConnectException
98
import com.jetbrains.toolbox.gateway.auth.*
109
import io.gitpod.publicapi.experimental.v1.UserServiceClient
@@ -33,9 +32,6 @@ val authScopesJetBrainsToolbox = listOf(
3332
"function:startWorkspace",
3433
"function:stopWorkspace",
3534
"function:deleteWorkspace",
36-
"function:getTeams",
37-
"function:getTeamMembers",
38-
"function:getTeamProjects",
3935
"function:getToken",
4036
"resource:default",
4137
)
@@ -79,13 +75,13 @@ class GitpodAuthManager {
7975
manager.addEventListener {
8076
when (it.type) {
8177
AuthEvent.Type.LOGIN -> {
82-
GitpodLogger.debug("gitpod: user logged in ${it.accountId}")
78+
GitpodLogger.info(" user logged in ${it.accountId}")
8379
resetCurrentAccount(it.accountId)
8480
loginListeners.forEach { it() }
8581
}
8682

8783
AuthEvent.Type.LOGOUT -> {
88-
GitpodLogger.debug("gitpod: user logged out ${it.accountId}")
84+
GitpodLogger.info("user logged out ${it.accountId}")
8985
resetCurrentAccount(it.accountId)
9086
logoutListeners.forEach { it() }
9187
}
@@ -207,16 +203,19 @@ class GitpodAccount : Account {
207203
}
208204

209205
suspend fun isValidate(): Boolean {
210-
val client = GitpodPublicApiManager.createClient(URI(host).host, credentials)
206+
// TODO(hw): 11
207+
val client = GitpodPublicApiManager.createClient(URI("https://$host").host, credentials)
208+
GitpodLogger.debug("validating account $host")
211209
try {
212210
GitpodPublicApiManager.tryGetAuthenticatedUser(UserServiceClient(client))
213211
return true
214212
} catch (e: ConnectException) {
215-
if (e.code == Code.UNAUTHENTICATED) {
216-
return false
217-
}
213+
// // TODO(hw): Server close jsonrpc so papi server respond internal error
214+
// if (e.code == Code.UNAUTHENTICATED) {
215+
// }
216+
GitpodLogger.error("account $host is not valid")
217+
return false
218218
}
219-
return true
220219
}
221220

222221
companion object {

components/ide/jetbrains/toolbox/src/main/kotlin/io/gitpod/toolbox/gateway/GitpodRemoteProvider.kt

+27-27
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ import com.jetbrains.toolbox.gateway.RemoteProvider
1010
import com.jetbrains.toolbox.gateway.ui.AccountDropdownField
1111
import com.jetbrains.toolbox.gateway.ui.ActionDescription
1212
import com.jetbrains.toolbox.gateway.ui.UiPage
13+
import io.gitpod.publicapi.experimental.v1.Workspaces
1314
import io.gitpod.toolbox.auth.GitpodAuthManager
1415
import io.gitpod.toolbox.auth.GitpodLoginPage
1516
import io.gitpod.toolbox.components.GitpodIcon
1617
import io.gitpod.toolbox.components.SimpleButton
17-
import io.gitpod.toolbox.service.ConnectParams
18-
import io.gitpod.toolbox.service.GitpodPublicApiManager
19-
import io.gitpod.toolbox.service.Utils
20-
import io.gitpod.toolbox.service.getConnectParams
18+
import io.gitpod.toolbox.service.*
2119
import io.gitpod.toolbox.utils.GitpodLogger
2220
import kotlinx.coroutines.launch
2321
import java.net.URI
@@ -31,7 +29,7 @@ class GitpodRemoteProvider(
3129
private val loginPage = GitpodLoginPage(authManger)
3230

3331
// cache consumed environments map locally
34-
private val environmentMap = mutableMapOf<String, GitpodRemoteProviderEnvironment>()
32+
private val environmentMap = mutableMapOf<String, Pair<Workspaces.Workspace, GitpodRemoteProviderEnvironment>>()
3533

3634
private var pendingConnectParams: Pair<String, ConnectParams>? = null
3735
private val openInToolboxUriHandler = GitpodOpenInToolboxUriHandler { (gitpodHost, connectParams) ->
@@ -50,30 +48,25 @@ class GitpodRemoteProvider(
5048
return@GitpodOpenInToolboxUriHandler future
5149
}
5250

53-
private fun setEnvironmentVisibility(connectParams: ConnectParams) {
51+
private suspend fun setEnvironmentVisibility(connectParams: ConnectParams) {
5452
val workspaceId = connectParams.workspaceId
5553
GitpodLogger.debug("setEnvironmentVisibility $workspaceId, $connectParams")
56-
val env = environmentMap[connectParams.uniqueID]
57-
if (env != null) {
58-
env.markActive()
59-
Utils.clientHelper.setAutoConnectOnEnvironmentReady(
60-
connectParams.uniqueID,
61-
"GO-242.20224.39",
62-
"/workspace/empty"
63-
)
64-
} else {
65-
GitpodRemoteProviderEnvironment(
54+
val obj = environmentMap[connectParams.uniqueID]
55+
var (workspace, env) = obj ?: Pair(null, null)
56+
if (obj == null) {
57+
workspace = publicApi.getWorkspace(workspaceId)
58+
env = GitpodRemoteProviderEnvironment(
6659
authManger,
6760
connectParams,
6861
publicApi,
6962
Utils.observablePropertiesFactory
70-
).apply {
71-
environmentMap[connectParams.uniqueID] = this
72-
this.markActive()
73-
consumer.consumeEnvironments(listOf(this))
74-
Utils.clientHelper.setAutoConnectOnEnvironmentReady(workspaceId, "GO-242.20224.39", "/workspace/empty")
75-
}
63+
)
64+
environmentMap[connectParams.uniqueID] = Pair(workspace, env)
65+
consumer.consumeEnvironments(environmentMap.values.map { it.second })
7666
}
67+
env!!.markActive()
68+
val joinLinkInfo = workspace!!.fetchJoinLink2Info(publicApi.getWorkspaceOwnerToken(workspaceId))
69+
Utils.clientHelper.setAutoConnectOnEnvironmentReady(workspaceId, joinLinkInfo.ideVersion, joinLinkInfo.projectPath)
7770
}
7871

7972
private fun showWorkspacesList() {
@@ -84,13 +77,13 @@ class GitpodRemoteProvider(
8477
}
8578
consumer.consumeEnvironments(workspaces.map {
8679
val connectParams = it.getConnectParams()
87-
val env = environmentMap[connectParams.uniqueID] ?: GitpodRemoteProviderEnvironment(
80+
val env = environmentMap[connectParams.uniqueID]?.second ?: GitpodRemoteProviderEnvironment(
8881
authManger,
8982
connectParams,
9083
publicApi,
9184
Utils.observablePropertiesFactory
9285
)
93-
environmentMap[connectParams.uniqueID] = env
86+
environmentMap[connectParams.uniqueID] = Pair(it, env)
9487
if (connectParams.uniqueID == pendingConnectParams?.second?.uniqueID) {
9588
setEnvironmentVisibility(connectParams)
9689
pendingConnectParams = null
@@ -108,9 +101,6 @@ class GitpodRemoteProvider(
108101
}
109102

110103
override fun getOverrideUiPage(): UiPage? {
111-
val account = authManger.getCurrentAccount()
112-
account ?: return loginPage
113-
startup()
114104
authManger.addLoginListener {
115105
Utils.toolboxUi.showWindow()
116106
Utils.toolboxUi.showPluginEnvironmentsPage()
@@ -120,6 +110,16 @@ class GitpodRemoteProvider(
120110
Utils.toolboxUi.showWindow()
121111
Utils.toolboxUi.showPluginEnvironmentsPage()
122112
}
113+
val account = authManger.getCurrentAccount()
114+
account ?: return loginPage
115+
startup()
116+
Utils.coroutineScope.launch {
117+
if (account.isValidate()) {
118+
return@launch
119+
}
120+
authManger.logout()
121+
Utils.toolboxUi.showPluginEnvironmentsPage()
122+
}
123123
return null
124124
}
125125

components/ide/jetbrains/toolbox/src/main/kotlin/io/gitpod/toolbox/gateway/GitpodUriHandler.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class GitpodOpenInToolboxUriHandler(val handler: (Pair<String, ConnectParams>) -
5656
throw IllegalArgumentException("invalid host: $host")
5757
}
5858
GitpodLogger.debug("parsed URI: $host, $workspaceId, $debugWorkspace")
59-
return Pair("https://$host", ConnectParams(workspaceId, debugWorkspace))
59+
val gitpodHost = "https://$host"
60+
return Pair(gitpodHost, ConnectParams(workspaceId, gitpodHost, debugWorkspace))
6061
}
6162
}

components/ide/jetbrains/toolbox/src/main/kotlin/io/gitpod/toolbox/service/DataManager.kt

+37-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,42 @@
44

55
package io.gitpod.toolbox.service
66

7-
import io.gitpod.publicapi.experimental.v1.Workspaces
7+
import io.gitpod.publicapi.experimental.v1.Workspaces.Workspace
8+
import io.gitpod.toolbox.utils.await
9+
import kotlinx.serialization.Serializable
10+
import kotlinx.serialization.decodeFromString
11+
import kotlinx.serialization.json.Json
12+
import okhttp3.Request
13+
import java.net.URL
814

9-
fun Workspaces.Workspace.getConnectParams(): ConnectParams {
10-
return ConnectParams(workspaceId, false)
15+
fun Workspace.getConnectParams(): ConnectParams {
16+
return ConnectParams(workspaceId, getGitpodHost(), false)
1117
}
18+
19+
fun Workspace.getIDEUrl(): String {
20+
return status.instance.status.url
21+
}
22+
23+
fun Workspace.getGitpodHost(): String {
24+
val ideUrl = URL(getIDEUrl())
25+
val hostSegments = ideUrl.host.split(".")
26+
return hostSegments.takeLast(2).joinToString(".")
27+
}
28+
29+
@Serializable
30+
class JoinLink2Response(val appPid: Int, val joinLink: String, val ideVersion: String, val projectPath: String)
31+
32+
suspend fun Workspace.fetchJoinLink2Info(ownerToken: String): JoinLink2Response {
33+
val backendUrl = "https://24000-${URL(getIDEUrl()).host}/joinLink2"
34+
val client = Utils.httpClient
35+
val req = Request.Builder().url(backendUrl).header("x-gitpod-owner-token", ownerToken)
36+
val response = client.newCall(req.build()).await()
37+
if (!response.isSuccessful) {
38+
throw IllegalStateException("Failed to get join link $backendUrl info: ${response.code} ${response.message}")
39+
}
40+
if (response.body == null) {
41+
throw IllegalStateException("Failed to get join link $backendUrl info: no body")
42+
}
43+
return Json.decodeFromString<JoinLink2Response>(response.body!!.string())
44+
}
45+

components/ide/jetbrains/toolbox/src/main/kotlin/io/gitpod/toolbox/service/GitpodConnectionProvider.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ class GitpodConnectionProvider(
2424
val ownerToken = publicApi.getWorkspaceOwnerToken(workspaceId)
2525
val account = authManager.getCurrentAccount() ?: throw Exception("No account found")
2626

27-
// TODO: debug workspace
28-
val connectParams = ConnectParams(workspaceId, false)
29-
3027
val (serverPort, cancel) = tunnelWithWebSocket(workspace, connectParams, ownerToken)
3128

3229
val connInfo = GitpodWebSocketSshConnectionInfo(
@@ -82,6 +79,7 @@ class GitpodWebSocketSshConnectionInfo(
8279

8380
data class ConnectParams(
8481
val workspaceId: String,
82+
val host: String,
8583
val debugWorkspace: Boolean = false,
8684
) {
8785
val resolvedWorkspaceId = "${if (debugWorkspace) "debug-" else ""}$workspaceId"

components/ide/jetbrains/toolbox/src/main/kotlin/io/gitpod/toolbox/utils/GitpodLogger.kt

+10-9
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,42 @@
44

55
package io.gitpod.toolbox.utils
66

7-
import org.slf4j.Logger
87
import org.slf4j.LoggerFactory
8+
import org.slf4j.spi.LocationAwareLogger
99

1010
object GitpodLogger {
11-
private val logger: Logger = LoggerFactory.getLogger(javaClass)
11+
private val logger: LocationAwareLogger = LoggerFactory.getLogger(javaClass) as LocationAwareLogger
12+
private val FQCN = GitpodLogger::class.java.name
1213

1314
private fun formatMessage(msg: String): String {
1415
return "[gitpod] $msg"
1516
}
1617

1718
fun info(message: String) {
18-
logger.info(formatMessage(message))
19+
logger.log(null, FQCN, LocationAwareLogger.INFO_INT, formatMessage(message), null, null)
1920
}
2021

2122
fun debug(message: String) {
22-
logger.debug(formatMessage(message))
23+
logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, formatMessage(message), null, null)
2324
}
2425

2526
fun warn(message: String) {
26-
logger.warn(formatMessage(message))
27+
logger.log(null, FQCN, LocationAwareLogger.WARN_INT, formatMessage(message), null, null)
2728
}
2829

2930
fun warn(message: String, throwable: Throwable?) {
30-
logger.warn(formatMessage(message), throwable)
31+
logger.log(null, FQCN, LocationAwareLogger.WARN_INT, formatMessage(message), null, throwable)
3132
}
3233

3334
fun error(message: String) {
34-
logger.error(formatMessage(message))
35+
logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, formatMessage(message), null, null)
3536
}
3637

3738
fun error(message: String, throwable: Throwable?) {
38-
logger.error(formatMessage(message), throwable)
39+
logger.log(null, FQCN, LocationAwareLogger.ERROR_INT, formatMessage(message), null, throwable)
3940
}
4041

4142
fun trace(message: String) {
42-
logger.trace(formatMessage(message))
43+
logger.log(null, FQCN, LocationAwareLogger.TRACE_INT, formatMessage(message), null, null)
4344
}
4445
}

0 commit comments

Comments
 (0)