Skip to content
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
chore: support 'me' as the username for template author
  • Loading branch information
Emyrk committed Aug 6, 2025
commit 64487da7bb0d8630f8051f8c23feafa3952bd705
7 changes: 6 additions & 1 deletion coderd/searchquery/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func Workspaces(ctx context.Context, db database.Store, query string, page coder
return filter, parser.Errors
}

func Templates(ctx context.Context, db database.Store, query string) (database.GetTemplatesWithFilterParams, []codersdk.ValidationError) {
func Templates(ctx context.Context, db database.Store, actorID uuid.UUID, query string) (database.GetTemplatesWithFilterParams, []codersdk.ValidationError) {
// Always lowercase for all searches.
query = strings.ToLower(query)
values, errors := searchTerms(query, func(term string, values url.Values) error {
Expand All @@ -288,6 +288,11 @@ func Templates(ctx context.Context, db database.Store, query string) (database.G
AuthorUsername: parser.String(values, "", "author"),
}

if filter.AuthorUsername == codersdk.Me {
filter.AuthorID = actorID
filter.AuthorUsername = ""
}

parser.ErrorExcessParams(values)
return filter, parser.Errors
}
Expand Down
11 changes: 10 additions & 1 deletion coderd/searchquery/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ func TestSearchUsers(t *testing.T) {

func TestSearchTemplates(t *testing.T) {
t.Parallel()
userID := uuid.New()
testCases := []struct {
Name string
Query string
Expand Down Expand Up @@ -688,6 +689,14 @@ func TestSearchTemplates(t *testing.T) {
},
},
},
{
Name: "MyTemplates",
Query: "author:me",
Expected: database.GetTemplatesWithFilterParams{
AuthorUsername: "",
AuthorID: userID,
},
},
}

for _, c := range testCases {
Expand All @@ -696,7 +705,7 @@ func TestSearchTemplates(t *testing.T) {
// Do not use a real database, this is only used for an
// organization lookup.
db, _ := dbtestutil.NewDB(t)
values, errs := searchquery.Templates(context.Background(), db, c.Query)
values, errs := searchquery.Templates(context.Background(), db, userID, c.Query)
if c.ExpectedErrorContains != "" {
require.True(t, len(errs) > 0, "expect some errors")
var s strings.Builder
Expand Down
3 changes: 2 additions & 1 deletion coderd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,10 @@ func (api *API) templatesByOrganization() http.HandlerFunc {
func (api *API) fetchTemplates(mutate func(r *http.Request, arg *database.GetTemplatesWithFilterParams)) http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
key := httpmw.APIKey(r)

queryStr := r.URL.Query().Get("q")
filter, errs := searchquery.Templates(ctx, api.Database, queryStr)
filter, errs := searchquery.Templates(ctx, api.Database, key.UserID, queryStr)
if len(errs) > 0 {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Invalid template search query.",
Expand Down
6 changes: 3 additions & 3 deletions coderd/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ func TestTemplatesByOrganization(t *testing.T) {
client := coderdtest.New(t, nil)
owner := coderdtest.CreateFirstUser(t, client)
adminAlpha, adminAlphaData := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin())
adminBravo, adminBravoData := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin())
adminBravo, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin())
adminCharlie, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin())

versionA := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil)
Expand All @@ -847,8 +847,8 @@ func TestTemplatesByOrganization(t *testing.T) {
require.Equal(t, foo.ID, alpha[0].ID)

// List bravo
bravo, err := client.Templates(ctx, codersdk.TemplateFilter{
AuthorUsername: adminBravoData.Username,
bravo, err := adminBravo.Templates(ctx, codersdk.TemplateFilter{
AuthorUsername: codersdk.Me,
})
require.NoError(t, err)
require.Len(t, bravo, 1)
Expand Down
Loading