Skip to content

Commit 3024bde

Browse files
authored
chore: support 'me' as the username for template author (#19204)
`author:me` to find my templates. Much nicer than knowing my own username
1 parent 5b80c47 commit 3024bde

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

coderd/searchquery/search.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ func Workspaces(ctx context.Context, db database.Store, query string, page coder
263263
return filter, parser.Errors
264264
}
265265

266-
func Templates(ctx context.Context, db database.Store, query string) (database.GetTemplatesWithFilterParams, []codersdk.ValidationError) {
266+
func Templates(ctx context.Context, db database.Store, actorID uuid.UUID, query string) (database.GetTemplatesWithFilterParams, []codersdk.ValidationError) {
267267
// Always lowercase for all searches.
268268
query = strings.ToLower(query)
269269
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
288288
AuthorUsername: parser.String(values, "", "author"),
289289
}
290290

291+
if filter.AuthorUsername == codersdk.Me {
292+
filter.AuthorID = actorID
293+
filter.AuthorUsername = ""
294+
}
295+
291296
parser.ErrorExcessParams(values)
292297
return filter, parser.Errors
293298
}

coderd/searchquery/search_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@ func TestSearchUsers(t *testing.T) {
640640

641641
func TestSearchTemplates(t *testing.T) {
642642
t.Parallel()
643+
userID := uuid.New()
643644
testCases := []struct {
644645
Name string
645646
Query string
@@ -688,6 +689,14 @@ func TestSearchTemplates(t *testing.T) {
688689
},
689690
},
690691
},
692+
{
693+
Name: "MyTemplates",
694+
Query: "author:me",
695+
Expected: database.GetTemplatesWithFilterParams{
696+
AuthorUsername: "",
697+
AuthorID: userID,
698+
},
699+
},
691700
}
692701

693702
for _, c := range testCases {
@@ -696,7 +705,7 @@ func TestSearchTemplates(t *testing.T) {
696705
// Do not use a real database, this is only used for an
697706
// organization lookup.
698707
db, _ := dbtestutil.NewDB(t)
699-
values, errs := searchquery.Templates(context.Background(), db, c.Query)
708+
values, errs := searchquery.Templates(context.Background(), db, userID, c.Query)
700709
if c.ExpectedErrorContains != "" {
701710
require.True(t, len(errs) > 0, "expect some errors")
702711
var s strings.Builder

coderd/templates.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,10 @@ func (api *API) templatesByOrganization() http.HandlerFunc {
544544
func (api *API) fetchTemplates(mutate func(r *http.Request, arg *database.GetTemplatesWithFilterParams)) http.HandlerFunc {
545545
return func(rw http.ResponseWriter, r *http.Request) {
546546
ctx := r.Context()
547+
key := httpmw.APIKey(r)
547548

548549
queryStr := r.URL.Query().Get("q")
549-
filter, errs := searchquery.Templates(ctx, api.Database, queryStr)
550+
filter, errs := searchquery.Templates(ctx, api.Database, key.UserID, queryStr)
550551
if len(errs) > 0 {
551552
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
552553
Message: "Invalid template search query.",

coderd/templates_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ func TestTemplatesByOrganization(t *testing.T) {
820820
client := coderdtest.New(t, nil)
821821
owner := coderdtest.CreateFirstUser(t, client)
822822
adminAlpha, adminAlphaData := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin())
823-
adminBravo, adminBravoData := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin())
823+
adminBravo, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin())
824824
adminCharlie, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin())
825825

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

849849
// List bravo
850-
bravo, err := client.Templates(ctx, codersdk.TemplateFilter{
851-
AuthorUsername: adminBravoData.Username,
850+
bravo, err := adminBravo.Templates(ctx, codersdk.TemplateFilter{
851+
AuthorUsername: codersdk.Me,
852852
})
853853
require.NoError(t, err)
854854
require.Len(t, bravo, 1)

0 commit comments

Comments
 (0)