chore: support 'me' as the username for template author (#19204)

`author:me` to find my templates. Much nicer than knowing my own
username
This commit is contained in:
Steven Masley
2025-08-06 11:45:07 -05:00
committed by GitHub
parent 5b80c47e8c
commit 3024bdebcb
4 changed files with 21 additions and 6 deletions
+6 -1
View File
@@ -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 {
@@ -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
}
+10 -1
View File
@@ -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
@@ -688,6 +689,14 @@ func TestSearchTemplates(t *testing.T) {
},
},
},
{
Name: "MyTemplates",
Query: "author:me",
Expected: database.GetTemplatesWithFilterParams{
AuthorUsername: "",
AuthorID: userID,
},
},
}
for _, c := range testCases {
@@ -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