Skip to content

feat: set 'jetbrains_connection' as build reason on workspace start #561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/main/kotlin/com/coder/gateway/cli/CoderCLIManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
val disableAutostart: Boolean = false,
val reportWorkspaceUsage: Boolean = false,
val wildcardSSH: Boolean = false,
val buildReason: Boolean = false,
)

/**
Expand Down Expand Up @@ -479,13 +480,21 @@
*
* Throws if the command execution fails.
*/
fun startWorkspace(workspaceOwner: String, workspaceName: String): String = exec(
"--global-config",
coderConfigPath.toString(),
"start",
"--yes",
workspaceOwner + "/" + workspaceName,
)
fun startWorkspace(workspaceOwner: String, workspaceName: String, feats: Features = features): String {
val args = mutableListOf(
"--global-config",
coderConfigPath.toString(),
"start",
"--yes",
workspaceOwner + "/" + workspaceName

Check notice on line 489 in src/main/kotlin/com/coder/gateway/cli/CoderCLIManager.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

String concatenation that can be converted to string template

'String' concatenation can be converted to a template
)

if (feats.buildReason) {
args.addAll(listOf("--reason", "jetbrains_connection"))
}

return exec(*args.toTypedArray())
}

private fun exec(vararg args: String): String {
val stdout =
Expand All @@ -511,6 +520,7 @@
disableAutostart = version >= SemVer(2, 5, 0),
reportWorkspaceUsage = version >= SemVer(2, 13, 0),
wildcardSSH = version >= SemVer(2, 19, 0),
buildReason = version >= SemVer(2, 25, 0),
)
}
}
Expand All @@ -518,7 +528,7 @@
/*
* This function returns the ssh-host-prefix used for Host entries.
*/
fun getHostPrefix(): String = "coder-jetbrains-${deploymentURL.safeHost()}"

Check notice on line 531 in src/main/kotlin/com/coder/gateway/cli/CoderCLIManager.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Class member can have 'private' visibility

Function 'getHostPrefix' could be private

/**
* This function returns the ssh host name generated for connecting to the workspace.
Expand Down Expand Up @@ -574,7 +584,7 @@
}
// non-wildcard case
if (parts[0] == "coder-jetbrains") {
return hostname + "--bg"

Check notice on line 587 in src/main/kotlin/com/coder/gateway/cli/CoderCLIManager.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

String concatenation that can be converted to string template

'String' concatenation can be converted to a template
}
// wildcard case
parts[0] += "-bg"
Expand Down
9 changes: 7 additions & 2 deletions src/main/kotlin/com/coder/gateway/sdk/CoderRestClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.coder.gateway.sdk.v2.models.User
import com.coder.gateway.sdk.v2.models.Workspace
import com.coder.gateway.sdk.v2.models.WorkspaceAgent
import com.coder.gateway.sdk.v2.models.WorkspaceBuild
import com.coder.gateway.sdk.v2.models.WorkspaceBuildReason
import com.coder.gateway.sdk.v2.models.WorkspaceResource
import com.coder.gateway.sdk.v2.models.WorkspaceStatus
import com.coder.gateway.sdk.v2.models.WorkspaceTransition
Expand Down Expand Up @@ -244,7 +245,7 @@ open class CoderRestClient(
* @throws [APIResponseException].
*/
fun stopWorkspace(workspace: Workspace): WorkspaceBuild {
val buildRequest = CreateWorkspaceBuildRequest(null, WorkspaceTransition.STOP)
val buildRequest = CreateWorkspaceBuildRequest(null, WorkspaceTransition.STOP, null)
val buildResponse = retroRestClient.createWorkspaceBuild(workspace.id, buildRequest).execute()
if (buildResponse.code() != HttpURLConnection.HTTP_CREATED) {
throw APIResponseException("stop workspace ${workspace.name}", url, buildResponse)
Expand All @@ -265,7 +266,11 @@ open class CoderRestClient(
fun updateWorkspace(workspace: Workspace): WorkspaceBuild {
val template = template(workspace.templateID)
val buildRequest =
CreateWorkspaceBuildRequest(template.activeVersionID, WorkspaceTransition.START)
CreateWorkspaceBuildRequest(
template.activeVersionID,
WorkspaceTransition.START,
WorkspaceBuildReason.JETBRAINS_CONNECTION
)
val buildResponse = retroRestClient.createWorkspaceBuild(workspace.id, buildRequest).execute()
if (buildResponse.code() != HttpURLConnection.HTTP_CREATED) {
throw APIResponseException("update workspace ${workspace.name}", url, buildResponse)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ data class CreateWorkspaceBuildRequest(
@Json(name = "template_version_id") val templateVersionID: UUID?,
// Use to start and stop the workspace.
@Json(name = "transition") val transition: WorkspaceTransition,
// Use to set build reason for a workspace.
@Json(name = "reason") val reason: WorkspaceBuildReason?,
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand All @@ -19,13 +21,15 @@ data class CreateWorkspaceBuildRequest(

if (templateVersionID != other.templateVersionID) return false
if (transition != other.transition) return false
if (reason != other.reason) return false

return true
}

override fun hashCode(): Int {
var result = templateVersionID?.hashCode() ?: 0
result = 31 * result + transition.hashCode()
result = 31 * result + (reason?.hashCode() ?: 0)
return result
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.coder.gateway.sdk.v2.models

import com.squareup.moshi.Json

enum class WorkspaceBuildReason {
@Json(name = "jetbrains_connection") JETBRAINS_CONNECTION,
}
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ internal class CoderCLIManagerTest {
listOf(
Pair("2.5.0", Features(true)),
Pair("2.13.0", Features(true, true)),
Pair("4.9.0", Features(true, true, true)),
Pair("4.9.0", Features(true, true, true, true)),
Pair("2.4.9", Features(false)),
Pair("1.0.1", Features(false)),
)
Expand Down
Loading