mirror of
https://github.com/coder/coder.git
synced 2026-09-22 05:05:20 +08:00
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:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user