Skip to content

Commit

Permalink
Merge pull request #849 from hashicorp/jspiker/search-projects-and-teams
Browse files Browse the repository at this point in the history
Allow searching by name on team and project list
  • Loading branch information
JarrettSpiker authored Feb 27, 2024
2 parents 2d5c976 + f1f4168 commit eedd1e2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Unreleased

## Enhancements
* Adds `Query` field to `Project` and `Team` list options, to allow projects and teams to be searched by name by @JarrettSpiker [#849](https://github.com/hashicorp/go-tfe/pull/849)

# v1.45.0

## Enhancements
Expand Down
5 changes: 4 additions & 1 deletion project.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ type Project struct {
type ProjectListOptions struct {
ListOptions

// Optional: String (partial project name) used to filter the results.
// Optional: String (complete project name) used to filter the results.
// If multiple, comma separated values are specified, projects matching
// any of the names are returned.
Name string `url:"filter[names],omitempty"`

// Optional: A query string to search projects by names.
Query string `url:"q,omitempty"`
}

// ProjectCreateOptions represents the options for creating a project
Expand Down
11 changes: 10 additions & 1 deletion projects_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestProjectsList(t *testing.T) {
assert.Equal(t, 3, pl.TotalCount)
})

t.Run("with list options", func(t *testing.T) {
t.Run("with pagination list options", func(t *testing.T) {
pl, err := client.Projects.List(ctx, orgTest.Name, &ProjectListOptions{
ListOptions: ListOptions{
PageNumber: 1,
Expand All @@ -48,6 +48,15 @@ func TestProjectsList(t *testing.T) {
assert.Equal(t, 3, len(pl.Items))
})

t.Run("with query list option", func(t *testing.T) {
pl, err := client.Projects.List(ctx, orgTest.Name, &ProjectListOptions{
Query: "Default",
})
require.NoError(t, err)
assert.Equal(t, true, containsProject(pl.Items, "Default Project"))
assert.Equal(t, 1, len(pl.Items))
})

t.Run("without a valid organization", func(t *testing.T) {
pl, err := client.Projects.List(ctx, badIdentifier, nil)
assert.Nil(t, pl)
Expand Down
3 changes: 3 additions & 0 deletions team.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ type TeamListOptions struct {

// Optional: A list of team names to filter by.
Names []string `url:"filter[names],omitempty"`

// Optional: A query string to search teams by names.
Query string `url:"q,omitempty"`
}

// TeamCreateOptions represents the options for creating a team.
Expand Down
19 changes: 13 additions & 6 deletions team_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ func TestTeamsList(t *testing.T) {
require.NoError(t, err)
assert.Contains(t, tl.Items, tmTest1)
assert.Contains(t, tl.Items, tmTest2)
assert.Contains(t, tl.Items, tmTest3)

t.Skip("paging not supported yet in API")
assert.Equal(t, 1, tl.CurrentPage)
assert.Equal(t, 2, tl.TotalCount)
assert.Equal(t, 4, tl.TotalCount)
})

t.Run("with list options", func(t *testing.T) {
t.Skip("paging not supported yet in API")
// Request a page number which is out of range. The result should
// be successful, but return no results if the paging options are
// properly passed along.
Expand All @@ -53,16 +52,24 @@ func TestTeamsList(t *testing.T) {
require.NoError(t, err)
assert.Empty(t, tl.Items)
assert.Equal(t, 999, tl.CurrentPage)
assert.Equal(t, 2, tl.TotalCount)
assert.Equal(t, 4, tl.TotalCount)

tl, err = client.Teams.List(ctx, orgTest.Name, &TeamListOptions{
Names: []string{tmTest2.Name, tmTest3.Name},
})

assert.Equal(t, tl.Items, 2)
require.NoError(t, err)
assert.Equal(t, len(tl.Items), 2)
assert.Contains(t, tl.Items, tmTest2)
assert.Contains(t, tl.Items, tmTest3)

tl, err = client.Teams.List(ctx, orgTest.Name, &TeamListOptions{
Query: tmTest1.Name[:len(tmTest1.Name)-2],
})
require.NoError(t, err)
assert.Equal(t, len(tl.Items), 1)
assert.Equal(t, 1, tl.TotalCount)
assert.Contains(t, tl.Items, tmTest1)

t.Run("with invalid names query param", func(t *testing.T) {
// should return an error because we've included an empty string
tl, err = client.Teams.List(ctx, orgTest.Name, &TeamListOptions{
Expand Down

0 comments on commit eedd1e2

Please sign in to comment.