@@ -11,6 +11,64 @@ import com.intellij.openapi.diagnostic.thisLogger
11
11
import io.gitpod.gitpodprotocol.api.entities.WorkspaceTimeoutDuration
12
12
import io.gitpod.jetbrains.remote.GitpodManager
13
13
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
+ }
14
72
15
73
class ExtendWorkspaceTimeoutAction : AnAction () {
16
74
private val manager = service<GitpodManager >()
@@ -21,26 +79,25 @@ class ExtendWorkspaceTimeoutAction : AnAction() {
21
79
" action" to " extend-timeout"
22
80
))
23
81
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)
36
93
} else {
37
- message = " Workspace timeout has been extended to three hours ."
94
+ message = " Workspace timeout has been extended to ${duration} ."
38
95
notificationType = NotificationType .INFORMATION
39
96
}
40
- }
41
97
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
+ }
44
101
}
45
102
}
46
103
}
0 commit comments