Skip to content

Commit c674128

Browse files
authored
chore: allow search by build params in workspace search filter (coder#12694)
* chore: workspace search filter allow search by params * has_param will return all workspaces with the param existance * exact matching
1 parent b4fd819 commit c674128

File tree

10 files changed

+485
-47
lines changed

10 files changed

+485
-47
lines changed

coderd/database/dbmem/dbmem.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8739,6 +8739,55 @@ func (q *FakeQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg database.
87398739
continue
87408740
}
87418741

8742+
if len(arg.HasParam) > 0 || len(arg.ParamNames) > 0 {
8743+
build, err := q.getLatestWorkspaceBuildByWorkspaceIDNoLock(ctx, workspace.ID)
8744+
if err != nil {
8745+
return nil, xerrors.Errorf("get latest build: %w", err)
8746+
}
8747+
8748+
params := make([]database.WorkspaceBuildParameter, 0)
8749+
for _, param := range q.workspaceBuildParameters {
8750+
if param.WorkspaceBuildID != build.ID {
8751+
continue
8752+
}
8753+
params = append(params, param)
8754+
}
8755+
8756+
var innerErr error
8757+
index := slices.IndexFunc(params, func(buildParam database.WorkspaceBuildParameter) bool {
8758+
// If hasParam matches, then we are done. This is a good match.
8759+
if slices.ContainsFunc(arg.HasParam, func(name string) bool {
8760+
return strings.EqualFold(buildParam.Name, name)
8761+
}) {
8762+
return true
8763+
}
8764+
8765+
// Check name + value
8766+
match := false
8767+
for i := range arg.ParamNames {
8768+
matchName := arg.ParamNames[i]
8769+
if !strings.EqualFold(matchName, buildParam.Name) {
8770+
continue
8771+
}
8772+
8773+
matchValue := arg.ParamValues[i]
8774+
if !strings.EqualFold(matchValue, buildParam.Value) {
8775+
continue
8776+
}
8777+
match = true
8778+
break
8779+
}
8780+
8781+
return match
8782+
})
8783+
if innerErr != nil {
8784+
return nil, xerrors.Errorf("error searching workspace build params: %w", innerErr)
8785+
}
8786+
if index < 0 {
8787+
continue
8788+
}
8789+
}
8790+
87428791
if arg.OwnerUsername != "" {
87438792
owner, err := q.getUserByIDNoLock(workspace.OwnerID)
87448793
if err == nil && !strings.EqualFold(arg.OwnerUsername, owner.Username) {

coderd/database/modelqueries.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,12 @@ func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspa
213213
// The name comment is for metric tracking
214214
query := fmt.Sprintf("-- name: GetAuthorizedWorkspaces :many\n%s", filtered)
215215
rows, err := q.db.QueryContext(ctx, query,
216+
pq.Array(arg.ParamNames),
217+
pq.Array(arg.ParamValues),
216218
arg.Deleted,
217219
arg.Status,
218220
arg.OwnerID,
221+
pq.Array(arg.HasParam),
219222
arg.OwnerUsername,
220223
arg.TemplateName,
221224
pq.Array(arg.TemplateIDs),

coderd/database/querier.go

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

coderd/database/queries.sql.go

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

0 commit comments

Comments
 (0)