Skip to content

fix: support substring search on workspace name #2096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func (q *fakeQuerier) GetWorkspacesWithFilter(_ context.Context, arg database.Ge
if !arg.Deleted && workspace.Deleted {
continue
}
if arg.Name != "" && workspace.Name != arg.Name {
if arg.Name != "" && !strings.Contains(workspace.Name, arg.Name) {
continue
}
workspaces = append(workspaces, workspace)
Expand Down
4 changes: 2 additions & 2 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions coderd/database/queries/workspaces.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ WHERE
owner_id = @owner_id
ELSE true
END
-- Filter by name
-- Filter by name, matching on substring
AND CASE
WHEN @name :: text != '' THEN
LOWER(name) = LOWER(@name)
LOWER(name) LIKE '%' || LOWER(@name) || '%'
ELSE true
END
;
Expand Down
36 changes: 36 additions & 0 deletions coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,42 @@ func TestWorkspaceByOwnerAndName(t *testing.T) {
})
}

func TestWorkspaceFilter(t *testing.T) {
t.Parallel()
t.Run("Name", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)

// full match
ws, err := client.Workspaces(context.Background(), codersdk.WorkspaceFilter{
Name: workspace.Name,
})
require.NoError(t, err)
require.Len(t, ws, 1, workspace.Name)
require.Equal(t, workspace.ID, ws[0].ID)

// partial match
ws, err = client.Workspaces(context.Background(), codersdk.WorkspaceFilter{
Name: workspace.Name[1 : len(workspace.Name)-2],
})
require.NoError(t, err)
require.Len(t, ws, 1)
require.Equal(t, workspace.ID, ws[0].ID)

// no match
ws, err = client.Workspaces(context.Background(), codersdk.WorkspaceFilter{
Name: "$$$$",
})
require.NoError(t, err)
require.Len(t, ws, 0)
})
}

func TestPostWorkspaceBuild(t *testing.T) {
t.Parallel()
t.Run("NoTemplateVersion", func(t *testing.T) {
Expand Down