Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
1a14a8c
feat: add support for template in workspace filter
f0ssel Jun 7, 2022
e3bd559
get working
f0ssel Jun 8, 2022
91d7d84
make gen
f0ssel Jun 8, 2022
2941b58
feat: Implement workspace search filter to support names
Emyrk Jun 10, 2022
70f4304
Use new query param parser for pagination fields
Emyrk Jun 10, 2022
0f7bb21
Fix fake db implementation
Emyrk Jun 10, 2022
5687dbe
Add unit test for parsing query params
Emyrk Jun 10, 2022
18e5247
Fix linting
Emyrk Jun 10, 2022
9a1b182
Fix search
Emyrk Jun 10, 2022
fab5d8c
Maintain old behavior
Emyrk Jun 10, 2022
15754c5
Linting
Emyrk Jun 10, 2022
f3123fe
Merge remote-tracking branch 'origin/main' into stevenmasley/workspac…
Emyrk Jun 10, 2022
d1c6319
Remove excessive calls, use filters on a single query
Emyrk Jun 10, 2022
e5ec365
Remove unused code
Emyrk Jun 10, 2022
21683d2
Add unit test to keep fake db clean
Emyrk Jun 10, 2022
fc48397
Fix typo
Emyrk Jun 10, 2022
5310164
Drop like name search on template
Emyrk Jun 10, 2022
c64ed18
Fix linting
Emyrk Jun 10, 2022
c6e3a57
Move WorkspaceSearchQuery to workspaces.go
Emyrk Jun 10, 2022
7821aa4
Search all templates with name
Emyrk Jun 10, 2022
d3091f0
Add more complex filter unit test
Emyrk Jun 10, 2022
ebcb831
Fix unit test to not violate pg constraint
Emyrk Jun 10, 2022
9368c48
Drop owner_id from query params
Emyrk Jun 10, 2022
b5f5705
Remove unused code/params
Emyrk Jun 10, 2022
9c9a5e2
Merge remote-tracking branch 'origin/main' into stevenmasley/workspac…
Emyrk Jun 10, 2022
4512a9b
Remove field from ts
Emyrk Jun 10, 2022
e9e913d
Address PR comments
Emyrk Jun 10, 2022
2aac32b
Fix js test
Emyrk Jun 10, 2022
c8813cc
Fix unit test
Emyrk Jun 10, 2022
ee35935
PR feedback
Emyrk Jun 13, 2022
4ad585e
Rename vague function name
Emyrk Jun 13, 2022
6251493
WorkspaceSearchQuery now returns db filter
Emyrk Jun 13, 2022
ac8dddd
Correct unit test and typescript
Emyrk Jun 13, 2022
727239e
Merge remote-tracking branch 'origin/main' into stevenmasley/workspac…
Emyrk Jun 13, 2022
92dcbc8
fmt
Emyrk Jun 13, 2022
0a51e02
Use !== over !=
Emyrk Jun 13, 2022
5a1272c
Fix js unit test
Emyrk Jun 13, 2022
5b5039b
Js linting
Emyrk Jun 13, 2022
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
Prev Previous commit
Next Next commit
PR feedback
- Unexport workspace search query function
- move code into workspaces.go
- use strings.SplitFields
  • Loading branch information
Emyrk committed Jun 13, 2022
commit ee3593500e82fcd2f9a7adfbd26fe4d3ee41410e
2 changes: 1 addition & 1 deletion coderd/httpapi/queryparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewQueryParamParser() *QueryParamParser {
}
}

func (p *QueryParamParser) Integer(r *http.Request, def int, queryParam string) int {
func (p *QueryParamParser) Int(r *http.Request, def int, queryParam string) int {
v, err := parse(r, strconv.Atoi, def, queryParam)
if err != nil {
p.Errors = append(p.Errors, Error{
Expand Down
4 changes: 2 additions & 2 deletions coderd/httpapi/queryparams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestParseQueryParams(t *testing.T) {
testQueryParams(t, expParams, parser, parser.String)
})

t.Run("Integer", func(t *testing.T) {
t.Run("Int", func(t *testing.T) {
t.Parallel()
expParams := []queryParamTestCase[int]{
{
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestParseQueryParams(t *testing.T) {
}

parser := httpapi.NewQueryParamParser()
testQueryParams(t, expParams, parser, parser.Integer)
testQueryParams(t, expParams, parser, parser.Int)
})

t.Run("UUIDArray", func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions coderd/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func parsePagination(w http.ResponseWriter, r *http.Request) (p codersdk.Paginat
params := codersdk.Pagination{
AfterID: parser.UUID(r, uuid.Nil, "after_id"),
// Limit default to "-1" which returns all results
Limit: parser.Integer(r, -1, "limit"),
Offset: parser.Integer(r, 0, "offset"),
Limit: parser.Int(r, -1, "limit"),
Offset: parser.Int(r, 0, "offset"),
}
if len(parser.Errors) > 0 {
httpapi.Write(w, http.StatusBadRequest, httpapi.Response{
Expand Down
66 changes: 65 additions & 1 deletion coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"net/http"
"strconv"
"strings"
"time"

"github.com/go-chi/chi/v5"
Expand Down Expand Up @@ -104,7 +105,7 @@ func (api *API) workspaces(rw http.ResponseWriter, r *http.Request) {
apiKey := httpmw.APIKey(r)

queryStr := r.URL.Query().Get("q")
values, err := WorkspaceSearchQuery(queryStr)
values, err := workspaceSearchQuery(queryStr)
if err != nil {
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
Message: "Invalid workspace search query.",
Expand Down Expand Up @@ -976,3 +977,66 @@ func validWorkspaceSchedule(s *string, min time.Duration) (sql.NullString, error
String: *s,
}, nil
}

// workspaceSearchQuery takes a query string and breaks it into its queryparams
// as a set of key=value.
func workspaceSearchQuery(query string) (map[string]string, error) {
searchParams := make(map[string]string)
if query == "" {
return searchParams, nil
}
// Because we do this in 2 passes, we want to maintain quotes on the first
// pass.Further splitting occurs on the second pass and quotes will be
// dropped.
elements := splitQueryParameterByDelimiter(query, ' ', true)
for _, element := range elements {
parts := splitQueryParameterByDelimiter(element, ':', false)
switch len(parts) {
case 1:
// No key:value pair. It is a workspace name, and maybe includes an owner
parts = splitQueryParameterByDelimiter(element, '/', false)
switch len(parts) {
case 1:
searchParams["name"] = parts[0]
case 2:
searchParams["owner"] = parts[0]
searchParams["name"] = parts[1]
default:
return nil, xerrors.Errorf("Query element %q can only contain 1 '/'", element)
}
case 2:
searchParams[parts[0]] = parts[1]
default:
return nil, xerrors.Errorf("Query element %q can only contain 1 ':'", element)
}
}

return searchParams, nil
}

// splitQueryParameterByDelimiter takes a query string and splits it into the individual elements
// of the query. Each element is separated by a delimiter. All quoted strings are
// kept as a single element.
//
// Although all our names cannot have spaces, that is a validation error.
// We should still parse the quoted string as a single value so that validation
// can properly fail on the space. If we do not, a value of `template:"my name"`
// will search `template:"my name:name"`, which produces an empty list instead of
// an error.
// nolint:revive
func splitQueryParameterByDelimiter(query string, delimiter rune, maintainQuotes bool) []string {
quoted := false
parts := strings.FieldsFunc(query, func(r rune) bool {
if r == '"' {
quoted = !quoted
}
return !quoted && r == delimiter
})
if !maintainQuotes {
for i, part := range parts {
parts[i] = strings.Trim(part, "\"")
}
}

return parts
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package coderd_test
package coderd

import (
"testing"

"github.com/coder/coder/coderd"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -124,7 +123,7 @@ func TestSearchWorkspace(t *testing.T) {
c := c
t.Run(c.Name, func(t *testing.T) {
t.Parallel()
values, err := coderd.WorkspaceSearchQuery(c.Query)
values, err := workspaceSearchQuery(c.Query)
if c.ExpectedErrorContains != "" {
require.Error(t, err, "expected error")
require.ErrorContains(t, err, c.ExpectedErrorContains)
Expand Down
2 changes: 1 addition & 1 deletion coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ func TestWorkspaceFilter(t *testing.T) {
{
Name: "Owner",
Filter: codersdk.WorkspaceFilter{
Owner: users[must(cryptorand.Intn(len(users)))].User.Username,
Owner: users[2].User.Username,
},
FilterF: func(f codersdk.WorkspaceFilter, workspace madeWorkspace) bool {
return workspace.Owner.Username == f.Owner
Expand Down
80 changes: 0 additions & 80 deletions coderd/workspacesearch.go

This file was deleted.