Skip to content

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

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 1 commit into from
Aug 6, 2025
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
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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy