@@ -21,6 +21,7 @@ import (
21
21
"github.com/coder/coder/v2/coderd/rbac"
22
22
"github.com/coder/coder/v2/coderd/rbac/policy"
23
23
"github.com/coder/coder/v2/coderd/render"
24
+ "github.com/coder/coder/v2/coderd/util/ptr"
24
25
"github.com/coder/coder/v2/coderd/workspaceapps/appurl"
25
26
"github.com/coder/coder/v2/codersdk"
26
27
"github.com/coder/coder/v2/provisionersdk/proto"
@@ -96,6 +97,49 @@ func TemplateVersionParameters(params []database.TemplateVersionParameter) ([]co
96
97
return out , nil
97
98
}
98
99
100
+ func TemplateVersionParameterFromPreview (param previewtypes.Parameter ) (codersdk.TemplateVersionParameter , error ) {
101
+ descriptionPlaintext , err := render .PlaintextFromMarkdown (param .Description )
102
+ if err != nil {
103
+ return codersdk.TemplateVersionParameter {}, err
104
+ }
105
+
106
+ sdkParam := codersdk.TemplateVersionParameter {
107
+ Name : param .Name ,
108
+ DisplayName : param .DisplayName ,
109
+ Description : param .Description ,
110
+ DescriptionPlaintext : descriptionPlaintext ,
111
+ Type : string (param .Type ),
112
+ FormType : string (param .FormType ),
113
+ Mutable : param .Mutable ,
114
+ DefaultValue : param .DefaultValue .AsString (),
115
+ Icon : param .Icon ,
116
+ Required : param .Required ,
117
+ Ephemeral : param .Ephemeral ,
118
+ Options : List (param .Options , TemplateVersionParameterOptionFromPreview ),
119
+ // Validation set after
120
+ }
121
+ if len (param .Validations ) > 0 {
122
+ validation := param .Validations [0 ]
123
+ sdkParam .ValidationError = validation .Error
124
+ if validation .Monotonic != nil {
125
+ sdkParam .ValidationMonotonic = codersdk .ValidationMonotonicOrder (* validation .Monotonic )
126
+ }
127
+ if validation .Regex != nil {
128
+ sdkParam .ValidationRegex = * validation .Regex
129
+ }
130
+ if validation .Min != nil {
131
+ //nolint:gosec // No other choice
132
+ sdkParam .ValidationMin = ptr .Ref (int32 (* validation .Min ))
133
+ }
134
+ if validation .Max != nil {
135
+ //nolint:gosec // No other choice
136
+ sdkParam .ValidationMax = ptr .Ref (int32 (* validation .Max ))
137
+ }
138
+ }
139
+
140
+ return sdkParam , nil
141
+ }
142
+
99
143
func TemplateVersionParameter (param database.TemplateVersionParameter ) (codersdk.TemplateVersionParameter , error ) {
100
144
options , err := templateVersionParameterOptions (param .Options )
101
145
if err != nil {
@@ -299,6 +343,15 @@ func templateVersionParameterOptions(rawOptions json.RawMessage) ([]codersdk.Tem
299
343
return options , nil
300
344
}
301
345
346
+ func TemplateVersionParameterOptionFromPreview (option * previewtypes.ParameterOption ) codersdk.TemplateVersionParameterOption {
347
+ return codersdk.TemplateVersionParameterOption {
348
+ Name : option .Name ,
349
+ Description : option .Description ,
350
+ Value : option .Value .AsString (),
351
+ Icon : option .Icon ,
352
+ }
353
+ }
354
+
302
355
func OAuth2ProviderApp (accessURL * url.URL , dbApp database.OAuth2ProviderApp ) codersdk.OAuth2ProviderApp {
303
356
return codersdk.OAuth2ProviderApp {
304
357
ID : dbApp .ID ,
0 commit comments