Add search teams on org API (#577)
- Add the API to search for teams on a organization by the `/orgs/{org}/teams/search` API of Gitea. - The response body of the API is a bit weird because it the JSON can return three fields "data", "error", "ok", first check if there's a general HTTP error. Otherwise when ok is set to false, return a error with the given error message of the error field. When ok is set to true, simply return the data. CC @fnetx Co-authored-by: Andrew Thornton <art27@cantab.net> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/577 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: John Olheiser <john.olheiser@gmail.com> Co-authored-by: Gusted <williamzijl7@hotmail.com> Co-committed-by: Gusted <williamzijl7@hotmail.com>
This commit is contained in:
parent
223f0a75e0
commit
468d48c978
2 changed files with 70 additions and 0 deletions
|
@ -8,6 +8,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Team represents a team in an organization
|
// Team represents a team in an organization
|
||||||
|
@ -75,6 +76,44 @@ func (c *Client) GetTeam(id int64) (*Team, *Response, error) {
|
||||||
return t, resp, err
|
return t, resp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SearchTeamsOptions options for searching teams.
|
||||||
|
type SearchTeamsOptions struct {
|
||||||
|
ListOptions
|
||||||
|
Query string
|
||||||
|
IncludeDescription bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o SearchTeamsOptions) getURLQuery() url.Values {
|
||||||
|
query := make(url.Values)
|
||||||
|
query.Add("page", fmt.Sprintf("%d", o.Page))
|
||||||
|
query.Add("limit", fmt.Sprintf("%d", o.PageSize))
|
||||||
|
query.Add("q", o.Query)
|
||||||
|
query.Add("include_desc", fmt.Sprintf("%t", o.IncludeDescription))
|
||||||
|
|
||||||
|
return query
|
||||||
|
}
|
||||||
|
|
||||||
|
// TeamSearchResults is the JSON struct that is returned from Team search API.
|
||||||
|
type TeamSearchResults struct {
|
||||||
|
OK bool `json:"ok"`
|
||||||
|
Error string `json:"error"`
|
||||||
|
Data []*Team `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// SearchOrgTeams search for teams in a org.
|
||||||
|
func (c *Client) SearchOrgTeams(org string, opt *SearchTeamsOptions) ([]*Team, *Response, error) {
|
||||||
|
responseBody := TeamSearchResults{}
|
||||||
|
opt.setDefaults()
|
||||||
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/teams/search?%s", org, opt.getURLQuery().Encode()), nil, nil, &responseBody)
|
||||||
|
if err != nil {
|
||||||
|
return nil, resp, err
|
||||||
|
}
|
||||||
|
if !responseBody.OK {
|
||||||
|
return nil, resp, fmt.Errorf("gitea error: %v", responseBody.Error)
|
||||||
|
}
|
||||||
|
return responseBody.Data, resp, err
|
||||||
|
}
|
||||||
|
|
||||||
// CreateTeamOption options for creating a team
|
// CreateTeamOption options for creating a team
|
||||||
type CreateTeamOption struct {
|
type CreateTeamOption struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
package gitea
|
package gitea
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -23,3 +24,33 @@ func createTestOrgTeams(t *testing.T, c *Client, org, name string, accessMode Ac
|
||||||
assert.NotNil(t, team)
|
assert.NotNil(t, team)
|
||||||
return team, e
|
return team, e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTeamSearch(t *testing.T) {
|
||||||
|
log.Println("== TestTeamSearch ==")
|
||||||
|
c := newTestClient()
|
||||||
|
|
||||||
|
orgName := "TestTeamsOrg"
|
||||||
|
// prepare for test
|
||||||
|
_, _, err := c.CreateOrg(CreateOrgOption{
|
||||||
|
Name: orgName,
|
||||||
|
Visibility: VisibleTypePublic,
|
||||||
|
RepoAdminChangeTeamAccess: true,
|
||||||
|
})
|
||||||
|
defer func() {
|
||||||
|
_, _ = c.DeleteOrg(orgName)
|
||||||
|
}()
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
if _, err = createTestOrgTeams(t, c, orgName, "Admins", AccessModeAdmin, []RepoUnitType{RepoUnitCode, RepoUnitIssues, RepoUnitPulls, RepoUnitReleases}); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
teams, _, err := c.SearchOrgTeams(orgName, &SearchTeamsOptions{
|
||||||
|
Query: "Admins",
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, teams, 1)
|
||||||
|
|
||||||
|
assert.Equal(t, "Admins", teams[0].Name)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue