From 468d48c978d64da85819dc2f6cc5a4b1c8b95303 Mon Sep 17 00:00:00 2001 From: Gusted Date: Wed, 27 Apr 2022 03:21:25 +0800 Subject: [PATCH] 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 Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/577 Reviewed-by: Andrew Thornton Reviewed-by: John Olheiser Co-authored-by: Gusted Co-committed-by: Gusted --- gitea/org_team.go | 39 +++++++++++++++++++++++++++++++++++++++ gitea/org_team_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/gitea/org_team.go b/gitea/org_team.go index 4b1b27b..0285afd 100644 --- a/gitea/org_team.go +++ b/gitea/org_team.go @@ -8,6 +8,7 @@ import ( "bytes" "encoding/json" "fmt" + "net/url" ) // Team represents a team in an organization @@ -75,6 +76,44 @@ func (c *Client) GetTeam(id int64) (*Team, *Response, error) { 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 type CreateTeamOption struct { Name string `json:"name"` diff --git a/gitea/org_team_test.go b/gitea/org_team_test.go index 53da9af..1869182 100644 --- a/gitea/org_team_test.go +++ b/gitea/org_team_test.go @@ -5,6 +5,7 @@ package gitea import ( + "log" "testing" "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) 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) +}