Skip to content

Commit 3a692a6

Browse files
authored
fix: Sort ComputedValue according to parameter schema index (coder#3022)
This fixes a test-flake in TestTemplateVersionParameters/List and gives us consistent sorting for parameters.
1 parent c0d19eb commit 3a692a6

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

coderd/parameter/compute.go

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77

88
"github.com/google/uuid"
9+
"golang.org/x/exp/slices"
910
"golang.org/x/xerrors"
1011

1112
"github.com/coder/coder/coderd/database"
@@ -32,6 +33,7 @@ type ComputedValue struct {
3233
database.ParameterValue
3334
SchemaID uuid.UUID `json:"schema_id"`
3435
DefaultSourceValue bool `json:"default_source_value"`
36+
index int32 // Track parameter schema index for sorting.
3537
}
3638

3739
// Compute accepts a scope in which parameter values are sourced.
@@ -137,6 +139,9 @@ func Compute(ctx context.Context, db database.Store, scope ComputeScope, options
137139
for _, value := range compute.computedParameterByName {
138140
values = append(values, value)
139141
}
142+
slices.SortFunc(values, func(a, b ComputedValue) bool {
143+
return a.index < b.index
144+
})
140145
return values, nil
141146
}
142147

@@ -188,6 +193,7 @@ func (c *compute) injectSingle(scopedParameter database.ParameterValue, defaultV
188193
ParameterValue: scopedParameter,
189194
SchemaID: parameterSchema.ID,
190195
DefaultSourceValue: defaultValue,
196+
index: parameterSchema.Index,
191197
}
192198
if c.options.HideRedisplayValues && !parameterSchema.RedisplayValue {
193199
value.SourceValue = ""

0 commit comments

Comments
 (0)