mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat(coderd): expose parameter description and type (#8944)
This commit is contained in:
@@ -3,6 +3,7 @@ package db2sdk
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sort"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
@@ -29,20 +30,10 @@ func WorkspaceBuildParameter(p database.WorkspaceBuildParameter) codersdk.Worksp
|
||||
}
|
||||
|
||||
func TemplateVersionParameter(param database.TemplateVersionParameter) (codersdk.TemplateVersionParameter, error) {
|
||||
var protoOptions []*proto.RichParameterOption
|
||||
err := json.Unmarshal(param.Options, &protoOptions)
|
||||
options, err := templateVersionParameterOptions(param.Options)
|
||||
if err != nil {
|
||||
return codersdk.TemplateVersionParameter{}, err
|
||||
}
|
||||
options := make([]codersdk.TemplateVersionParameterOption, 0)
|
||||
for _, option := range protoOptions {
|
||||
options = append(options, codersdk.TemplateVersionParameterOption{
|
||||
Name: option.Name,
|
||||
Description: option.Description,
|
||||
Value: option.Value,
|
||||
Icon: option.Icon,
|
||||
})
|
||||
}
|
||||
|
||||
descriptionPlaintext, err := parameter.Plaintext(param.Description)
|
||||
if err != nil {
|
||||
@@ -132,3 +123,61 @@ func Role(role rbac.Role) codersdk.Role {
|
||||
Name: role.Name,
|
||||
}
|
||||
}
|
||||
|
||||
func TemplateInsightsParameters(parameterRows []database.GetTemplateParameterInsightsRow) ([]codersdk.TemplateParameterUsage, error) {
|
||||
parametersByNum := make(map[int64]*codersdk.TemplateParameterUsage)
|
||||
for _, param := range parameterRows {
|
||||
if _, ok := parametersByNum[param.Num]; !ok {
|
||||
var opts []codersdk.TemplateVersionParameterOption
|
||||
err := json.Unmarshal(param.Options, &opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
plaintextDescription, err := parameter.Plaintext(param.Description)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
parametersByNum[param.Num] = &codersdk.TemplateParameterUsage{
|
||||
TemplateIDs: param.TemplateIDs,
|
||||
Name: param.Name,
|
||||
Type: param.Type,
|
||||
DisplayName: param.DisplayName,
|
||||
Description: plaintextDescription,
|
||||
Options: opts,
|
||||
}
|
||||
}
|
||||
parametersByNum[param.Num].Values = append(parametersByNum[param.Num].Values, codersdk.TemplateParameterValue{
|
||||
Value: param.Value,
|
||||
Count: param.Count,
|
||||
})
|
||||
}
|
||||
parametersUsage := []codersdk.TemplateParameterUsage{}
|
||||
for _, param := range parametersByNum {
|
||||
parametersUsage = append(parametersUsage, *param)
|
||||
}
|
||||
|
||||
sort.Slice(parametersUsage, func(i, j int) bool {
|
||||
return parametersUsage[i].Name < parametersUsage[j].Name
|
||||
})
|
||||
return parametersUsage, nil
|
||||
}
|
||||
|
||||
func templateVersionParameterOptions(rawOptions json.RawMessage) ([]codersdk.TemplateVersionParameterOption, error) {
|
||||
var protoOptions []*proto.RichParameterOption
|
||||
err := json.Unmarshal(rawOptions, &protoOptions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var options []codersdk.TemplateVersionParameterOption
|
||||
for _, option := range protoOptions {
|
||||
options = append(options, codersdk.TemplateVersionParameterOption{
|
||||
Name: option.Name,
|
||||
Description: option.Description,
|
||||
Value: option.Value,
|
||||
Icon: option.Icon,
|
||||
})
|
||||
}
|
||||
return options, nil
|
||||
}
|
||||
|
||||
@@ -2186,6 +2186,7 @@ func (q *FakeQuerier) GetTemplateParameterInsights(ctx context.Context, arg data
|
||||
uniqueTemplateParams[key] = &database.GetTemplateParameterInsightsRow{
|
||||
Num: num,
|
||||
Name: tvp.Name,
|
||||
Type: tvp.Type,
|
||||
DisplayName: tvp.DisplayName,
|
||||
Description: tvp.Description,
|
||||
Options: tvp.Options,
|
||||
@@ -2220,6 +2221,7 @@ func (q *FakeQuerier) GetTemplateParameterInsights(ctx context.Context, arg data
|
||||
TemplateIDs: uniqueSortedUUIDs(utp.TemplateIDs),
|
||||
Name: utp.Name,
|
||||
DisplayName: utp.DisplayName,
|
||||
Type: utp.Type,
|
||||
Description: utp.Description,
|
||||
Options: utp.Options,
|
||||
Value: value,
|
||||
|
||||
@@ -1582,16 +1582,18 @@ WITH latest_workspace_builds AS (
|
||||
tvp.name,
|
||||
tvp.display_name,
|
||||
tvp.description,
|
||||
tvp.options
|
||||
tvp.options,
|
||||
tvp.type
|
||||
FROM latest_workspace_builds wb
|
||||
JOIN template_version_parameters tvp ON (tvp.template_version_id = wb.template_version_id)
|
||||
GROUP BY tvp.name, tvp.display_name, tvp.description, tvp.options
|
||||
GROUP BY tvp.name, tvp.display_name, tvp.description, tvp.options, tvp.type
|
||||
)
|
||||
|
||||
SELECT
|
||||
utp.num,
|
||||
utp.template_ids,
|
||||
utp.name,
|
||||
utp.type,
|
||||
utp.display_name,
|
||||
utp.description,
|
||||
utp.options,
|
||||
@@ -1599,7 +1601,7 @@ SELECT
|
||||
COUNT(wbp.value) AS count
|
||||
FROM unique_template_params utp
|
||||
JOIN workspace_build_parameters wbp ON (utp.workspace_build_ids @> ARRAY[wbp.workspace_build_id] AND utp.name = wbp.name)
|
||||
GROUP BY utp.num, utp.name, utp.display_name, utp.description, utp.options, utp.template_ids, wbp.value
|
||||
GROUP BY utp.num, utp.name, utp.display_name, utp.description, utp.options, utp.template_ids, utp.type, wbp.value
|
||||
`
|
||||
|
||||
type GetTemplateParameterInsightsParams struct {
|
||||
@@ -1612,6 +1614,7 @@ type GetTemplateParameterInsightsRow struct {
|
||||
Num int64 `db:"num" json:"num"`
|
||||
TemplateIDs []uuid.UUID `db:"template_ids" json:"template_ids"`
|
||||
Name string `db:"name" json:"name"`
|
||||
Type string `db:"type" json:"type"`
|
||||
DisplayName string `db:"display_name" json:"display_name"`
|
||||
Description string `db:"description" json:"description"`
|
||||
Options json.RawMessage `db:"options" json:"options"`
|
||||
@@ -1636,6 +1639,7 @@ func (q *sqlQuerier) GetTemplateParameterInsights(ctx context.Context, arg GetTe
|
||||
&i.Num,
|
||||
pq.Array(&i.TemplateIDs),
|
||||
&i.Name,
|
||||
&i.Type,
|
||||
&i.DisplayName,
|
||||
&i.Description,
|
||||
&i.Options,
|
||||
|
||||
@@ -149,16 +149,18 @@ WITH latest_workspace_builds AS (
|
||||
tvp.name,
|
||||
tvp.display_name,
|
||||
tvp.description,
|
||||
tvp.options
|
||||
tvp.options,
|
||||
tvp.type
|
||||
FROM latest_workspace_builds wb
|
||||
JOIN template_version_parameters tvp ON (tvp.template_version_id = wb.template_version_id)
|
||||
GROUP BY tvp.name, tvp.display_name, tvp.description, tvp.options
|
||||
GROUP BY tvp.name, tvp.display_name, tvp.description, tvp.options, tvp.type
|
||||
)
|
||||
|
||||
SELECT
|
||||
utp.num,
|
||||
utp.template_ids,
|
||||
utp.name,
|
||||
utp.type,
|
||||
utp.display_name,
|
||||
utp.description,
|
||||
utp.options,
|
||||
@@ -166,4 +168,4 @@ SELECT
|
||||
COUNT(wbp.value) AS count
|
||||
FROM unique_template_params utp
|
||||
JOIN workspace_build_parameters wbp ON (utp.workspace_build_ids @> ARRAY[wbp.workspace_build_id] AND utp.name = wbp.name)
|
||||
GROUP BY utp.num, utp.name, utp.display_name, utp.description, utp.options, utp.template_ids, wbp.value;
|
||||
GROUP BY utp.num, utp.name, utp.display_name, utp.description, utp.options, utp.template_ids, utp.type, wbp.value;
|
||||
|
||||
Reference in New Issue
Block a user