Skip to content

Commit 1eadef6

Browse files
committed
has_param will return all workspaces with the param existance
1 parent 4cba6cb commit 1eadef6

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

coderd/database/queries.sql.go

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaces.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,17 @@ WHERE
186186
ELSE true
187187
END
188188
-- Filter by build parameter
189-
AND CASE WHEN array_length(@has_param :: uuid[], 1) > 0 THEN
189+
-- @has_param will match any build that includes the parameter.
190+
AND CASE WHEN array_length(@has_param :: text[], 1) > 0 THEN
190191
EXISTS (
191192
SELECT
192193
1
193194
FROM
194195
workspace_build_parameters
195196
WHERE
196197
workspace_build_parameters.workspace_build_id = latest_build.id AND
197-
workspace_build_parameters.name = ANY(@has_param)
198+
-- ILIKE is case insensitive
199+
workspace_build_parameters.name ILIKE ANY(@has_param)
198200
)
199201
ELSE true
200202
END

coderd/searchquery/search.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,35 @@ func Workspaces(query string, page codersdk.Pagination, agentInactiveDisconnectT
121121
Valid: values.Has("outdated"),
122122
}
123123

124+
type paramMatch struct {
125+
name string
126+
value *string
127+
}
128+
// parameter matching takes the form of:
129+
// `param:<name>[=<value>]`
130+
// If the value is omitted, then we match on the presence of the parameter.
131+
// If the value is provided, then we match on the parameter and value.
132+
params := httpapi.ParseCustomList(parser, values, []paramMatch{}, "param", func(v string) (paramMatch, error) {
133+
// Ignore excess spaces
134+
v = strings.TrimSpace(v)
135+
parts := strings.Split(v, "=")
136+
if len(parts) == 1 {
137+
// Only match on the presence of the parameter
138+
return paramMatch{name: parts[0], value: nil}, nil
139+
}
140+
if len(parts) == 2 {
141+
// Match on the parameter and value
142+
return paramMatch{name: parts[0], value: &parts[1]}, nil
143+
}
144+
return paramMatch{}, xerrors.Errorf("query element %q can only contain 1 '='", v)
145+
})
146+
for _, p := range params {
147+
if p.value == nil {
148+
filter.HasParam = append(filter.HasParam, p.name)
149+
continue
150+
}
151+
}
152+
124153
parser.ErrorExcessParams(values)
125154
return filter, parser.Errors
126155
}

coderd/searchquery/search_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ func TestSearchWorkspace(t *testing.T) {
137137
},
138138
},
139139
},
140+
{
141+
Name: "ParamName",
142+
Query: "param:foo",
143+
Expected: database.GetWorkspacesParams{
144+
HasParam: []string{"foo"},
145+
},
146+
},
140147
// Failures
141148
{
142149
Name: "NoPrefix",
@@ -182,6 +189,10 @@ func TestSearchWorkspace(t *testing.T) {
182189
// nil slice vs 0 len slice is equivalent for our purposes.
183190
c.Expected.WorkspaceIds = values.WorkspaceIds
184191
}
192+
if len(c.Expected.HasParam) == len(values.HasParam) {
193+
// nil slice vs 0 len slice is equivalent for our purposes.
194+
c.Expected.HasParam = values.HasParam
195+
}
185196
assert.Len(t, errs, 0, "expected no error")
186197
assert.Equal(t, c.Expected, values, "expected values")
187198
}

0 commit comments

Comments
 (0)