Skip to content

Commit 25df3f3

Browse files
JetBrains Timeout Input Dialog
Co-authored-by: Lou Bichard <[email protected]>
1 parent 04ab8dd commit 25df3f3

File tree

1 file changed

+73
-16
lines changed

1 file changed

+73
-16
lines changed

components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/actions/ExtendWorkspaceTimeoutAction.kt

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,64 @@ import com.intellij.openapi.diagnostic.thisLogger
1111
import io.gitpod.gitpodprotocol.api.entities.WorkspaceTimeoutDuration
1212
import io.gitpod.jetbrains.remote.GitpodManager
1313
import com.intellij.notification.NotificationType
14+
import com.intellij.openapi.ui.DialogWrapper
15+
import com.intellij.openapi.ui.ValidationInfo
16+
import javax.swing.JComponent
17+
import javax.swing.JTextField
18+
import javax.swing.JPanel
19+
import javax.swing.JComboBox
20+
import javax.swing.BoxLayout
21+
22+
// validation from https://github.com/gitpod-io/gitpod/blob/74ccaea38db8df2d1666161a073015485ebb90ca/components/gitpod-protocol/src/gitpod-service.ts#L361-L383
23+
const val WORKSPACE_MAXIMUM_TIMEOUT_HOURS = 24
24+
fun validate(duration: Int, unit: Char): String {
25+
if (duration <= 0) {
26+
throw IllegalArgumentException("Invalid timeout value: ${duration}${unit}")
27+
}
28+
if (
29+
(unit == 'h' && duration > WORKSPACE_MAXIMUM_TIMEOUT_HOURS) ||
30+
(unit == 'm' && duration > WORKSPACE_MAXIMUM_TIMEOUT_HOURS * 60)
31+
) {
32+
throw IllegalArgumentException("Workspace inactivity timeout cannot exceed 24h")
33+
}
34+
return "valid"
35+
}
36+
37+
class InputDurationDialog : DialogWrapper(null, true) {
38+
private val textField = JTextField(10)
39+
private val unitComboBox = JComboBox(arrayOf("minutes", "hours"))
40+
41+
init {
42+
init()
43+
title = "Set timeout duration"
44+
}
45+
46+
override fun createCenterPanel(): JComponent {
47+
val customComponent = JPanel()
48+
customComponent.layout = BoxLayout(customComponent, BoxLayout.X_AXIS)
49+
customComponent.add(textField)
50+
customComponent.add(unitComboBox)
51+
52+
textField.text = "180"
53+
54+
return customComponent
55+
}
56+
57+
override fun doValidate(): ValidationInfo? {
58+
try {
59+
val selectedUnit = unitComboBox.selectedItem.toString()
60+
validate(textField.text.toInt(), selectedUnit[0])
61+
return null
62+
} catch (e: IllegalArgumentException) {
63+
return ValidationInfo(e.message ?: "An unknown error has occurred", textField)
64+
}
65+
}
66+
67+
fun getDuration(): String {
68+
val selectedUnit = unitComboBox.selectedItem.toString()
69+
return "${textField.text}${selectedUnit[0]}"
70+
}
71+
}
1472

1573
class ExtendWorkspaceTimeoutAction : AnAction() {
1674
private val manager = service<GitpodManager>()
@@ -21,26 +79,25 @@ class ExtendWorkspaceTimeoutAction : AnAction() {
2179
"action" to "extend-timeout"
2280
))
2381

24-
manager.client.server.setWorkspaceTimeout(workspaceInfo.workspaceId, WorkspaceTimeoutDuration.DURATION_180M.toString()).whenComplete { result, e ->
25-
var message: String
26-
var notificationType: NotificationType
27-
28-
if (e != null) {
29-
message = "Cannot extend workspace timeout: ${e.message}"
30-
notificationType = NotificationType.ERROR
31-
thisLogger().error("gitpod: failed to extend workspace timeout", e)
32-
} else {
33-
if (result.resetTimeoutOnWorkspaces.isNotEmpty()) {
34-
message = "Workspace timeout has been extended to three hours. This reset the workspace timeout for other workspaces."
35-
notificationType = NotificationType.WARNING
82+
val dialog = InputDurationDialog()
83+
if (dialog.showAndGet()) {
84+
val duration = dialog.getDuration()
85+
manager.client.server.setWorkspaceTimeout(workspaceInfo.workspaceId, duration.toString()).whenComplete { _, e ->
86+
var message: String
87+
var notificationType: NotificationType
88+
89+
if (e != null) {
90+
message = "Cannot extend workspace timeout: ${e.message}"
91+
notificationType = NotificationType.ERROR
92+
thisLogger().error("gitpod: failed to extend workspace timeout", e)
3693
} else {
37-
message = "Workspace timeout has been extended to three hours."
94+
message = "Workspace timeout has been extended to ${duration}."
3895
notificationType = NotificationType.INFORMATION
3996
}
40-
}
4197

42-
val notification = manager.notificationGroup.createNotification(message, notificationType)
43-
notification.notify(null)
98+
val notification = manager.notificationGroup.createNotification(message, notificationType)
99+
notification.notify(null)
100+
}
44101
}
45102
}
46103
}

0 commit comments

Comments
 (0)