Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: implement fuzzy name matching for templates #14211

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 3 commits into from
Aug 9, 2024
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
5 changes: 5 additions & 0 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -9647,6 +9647,11 @@ func (q *FakeQuerier) GetAuthorizedTemplates(ctx context.Context, arg database.G
if arg.Deprecated.Valid && arg.Deprecated.Bool == (template.Deprecated != "") {
continue
}
if arg.FuzzyName != "" {
if !strings.Contains(strings.ToLower(template.Name), strings.ToLower(arg.FuzzyName)) {
continue
}
}

if len(arg.IDs) > 0 {
match := false
Expand Down
1 change: 1 addition & 0 deletions coderd/database/modelqueries.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (q *sqlQuerier) GetAuthorizedTemplates(ctx context.Context, arg GetTemplate
arg.Deleted,
arg.OrganizationID,
arg.ExactName,
arg.FuzzyName,
pq.Array(arg.IDs),
arg.Deprecated,
)
Expand Down
16 changes: 12 additions & 4 deletions coderd/database/queries.sql.go

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

6 changes: 6 additions & 0 deletions coderd/database/queries/templates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ WHERE
LOWER("name") = LOWER(@exact_name)
ELSE true
END
-- Filter by name, matching on substring
AND CASE
WHEN @fuzzy_name :: text != '' THEN
lower(name) ILIKE '%' || lower(@fuzzy_name) || '%'
ELSE true
END
-- Filter by ids
AND CASE
WHEN array_length(@ids :: uuid[], 1) > 0 THEN
Expand Down
1 change: 1 addition & 0 deletions coderd/searchquery/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ func Templates(ctx context.Context, db database.Store, query string) (database.G

parser := httpapi.NewQueryParamParser()
filter := database.GetTemplatesWithFilterParams{
FuzzyName: parser.String(values, "", "name"),
Deleted: parser.Boolean(values, false, "deleted"),
ExactName: parser.String(values, "", "exact_name"),
IDs: parser.UUIDs(values, []uuid.UUID{}, "ids"),
Expand Down
7 changes: 7 additions & 0 deletions coderd/searchquery/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,13 @@ func TestSearchTemplates(t *testing.T) {
Query: "",
Expected: database.GetTemplatesWithFilterParams{},
},
{
Name: "OnlyName",
Query: "foobar",
Expected: database.GetTemplatesWithFilterParams{
FuzzyName: "foobar",
},
},
}

for _, c := range testCases {
Expand Down
29 changes: 27 additions & 2 deletions coderd/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,12 @@ func TestTemplatesByOrganization(t *testing.T) {
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
version2 := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
coderdtest.CreateTemplate(t, client, user.OrganizationID, version2.ID)
foo := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(request *codersdk.CreateTemplateRequest) {
request.Name = "foobar"
})
bar := coderdtest.CreateTemplate(t, client, user.OrganizationID, version2.ID, func(request *codersdk.CreateTemplateRequest) {
request.Name = "barbaz"
})

ctx := testutil.Context(t, testutil.WaitLong)

Expand All @@ -460,6 +464,27 @@ func TestTemplatesByOrganization(t *testing.T) {
require.Equal(t, tmpl.OrganizationDisplayName, org.DisplayName, "organization display name")
require.Equal(t, tmpl.OrganizationIcon, org.Icon, "organization display name")
}

// Check fuzzy name matching
templates, err = client.Templates(ctx, codersdk.TemplateFilter{
FuzzyName: "bar",
})
require.NoError(t, err)
require.Len(t, templates, 2)

templates, err = client.Templates(ctx, codersdk.TemplateFilter{
FuzzyName: "foo",
})
require.NoError(t, err)
require.Len(t, templates, 1)
require.Equal(t, foo.ID, templates[0].ID)

templates, err = client.Templates(ctx, codersdk.TemplateFilter{
FuzzyName: "baz",
})
require.NoError(t, err)
require.Len(t, templates, 1)
require.Equal(t, bar.ID, templates[0].ID)
})
}

Expand Down
13 changes: 11 additions & 2 deletions codersdk/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,10 @@ func (c *Client) TemplatesByOrganization(ctx context.Context, organizationID uui
}

type TemplateFilter struct {
OrganizationID uuid.UUID
ExactName string
OrganizationID uuid.UUID `typescript:"-"`
ExactName string `typescript:"-"`
FuzzyName string `typescript:"-"`
SearchQuery string `json:"q,omitempty"`
}

// asRequestOption returns a function that can be used in (*Client).Request.
Expand All @@ -424,6 +426,13 @@ func (f TemplateFilter) asRequestOption() RequestOption {
params = append(params, fmt.Sprintf("exact_name:%q", f.ExactName))
}

if f.FuzzyName != "" {
params = append(params, fmt.Sprintf("name:%q", f.FuzzyName))
}
if f.SearchQuery != "" {
params = append(params, f.SearchQuery)
}

q := r.URL.Query()
q.Set("q", strings.Join(params, " "))
r.URL.RawQuery = q.Encode()
Expand Down
3 changes: 1 addition & 2 deletions site/src/api/typesGenerated.ts

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

Loading