Add SearchRepo API Call (#254)
Merge branch 'master' into search_repo IssueSubscribtion: Check http Status responce (#242) Merge branch 'master' into impruve-issueWatch Refactor RepoWatch (#241) Merge branch 'master' into refactor-repoWatch Add ListMilestoneOption to ListRepoMilestones (#244) use StateType use PageSize adjut test since gitea bug got fixed (#gitea/10047) add TestMilestones add optional ListMilestoneOption format Add ListOptions struct (#249) same struct as in models/list_options.go add mising license header add ListOptions struct Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/249 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: lafriks <lafriks@noreply.gitea.io> [README] add import path (#239) add import path add import path to readme Changelog v0.11.0 (#235) Co-authored-by: techknowlogick <techknowlogick@gitea.io> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/235 Reviewed-by: lafriks <lafriks@noreply.gitea.io> Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: John Olheiser <john.olheiser@gmail.com> Add TestMyUser (#237) Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/237 Reviewed-by: lafriks <lafriks@noreply.gitea.io> Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/239 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: John Olheiser <john.olheiser@gmail.com> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/244 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: Andrew Thornton <art27@cantab.net> Merge branch 'master' into refactor-repoWatch Add ListOptions struct (#249) same struct as in models/list_options.go add mising license header add ListOptions struct Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/249 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: lafriks <lafriks@noreply.gitea.io> add TEST fix GetMyWatchedRepos [README] add import path (#239) add import path add import path to readme Changelog v0.11.0 (#235) Co-authored-by: techknowlogick <techknowlogick@gitea.io> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/235 Reviewed-by: lafriks <lafriks@noreply.gitea.io> Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: John Olheiser <john.olheiser@gmail.com> Add TestMyUser (#237) Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/237 Reviewed-by: lafriks <lafriks@noreply.gitea.io> Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/239 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: John Olheiser <john.olheiser@gmail.com> refactor RepoWatch * add CheckRepoWatch * add GetMyWatchedRepos * minimize func_options on GetWatchedRepos * WatchRepo return nil/error * UnWatchRepo return nil/error Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/241 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Merge branch 'master' into impruve-issueWatch Add ListMilestoneOption to ListRepoMilestones (#244) use StateType use PageSize adjut test since gitea bug got fixed (#gitea/10047) add TestMilestones add optional ListMilestoneOption format Add ListOptions struct (#249) same struct as in models/list_options.go add mising license header add ListOptions struct Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/249 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: lafriks <lafriks@noreply.gitea.io> [README] add import path (#239) add import path add import path to readme Changelog v0.11.0 (#235) Co-authored-by: techknowlogick <techknowlogick@gitea.io> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/235 Reviewed-by: lafriks <lafriks@noreply.gitea.io> Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: John Olheiser <john.olheiser@gmail.com> Add TestMyUser (#237) Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/237 Reviewed-by: lafriks <lafriks@noreply.gitea.io> Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/239 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: John Olheiser <john.olheiser@gmail.com> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/244 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: Andrew Thornton <art27@cantab.net> Add ListOptions struct (#249) same struct as in models/list_options.go add mising license header add ListOptions struct Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/249 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: lafriks <la... Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: 6543 <6543@noreply.gitea.io> Co-authored-by: Hilário Coelho <hilario.coelho@securityside.com> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/254 Reviewed-by: 6543 <6543@noreply.gitea.io> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
parent
aa8d0c04d4
commit
f8390e6ca8
2 changed files with 118 additions and 0 deletions
|
@ -8,6 +8,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -74,6 +75,74 @@ func (c *Client) ListOrgRepos(org string) ([]*Repository, error) {
|
||||||
return repos, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/repos", org), nil, nil, &repos)
|
return repos, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/repos", org), nil, nil, &repos)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SearchRepoOptions options for searching repositories
|
||||||
|
type SearchRepoOptions struct {
|
||||||
|
Keyword string
|
||||||
|
Topic bool
|
||||||
|
IncludeDesc bool
|
||||||
|
UID int64
|
||||||
|
PriorityOwnerID int64
|
||||||
|
StarredBy int64
|
||||||
|
Private bool
|
||||||
|
Template bool
|
||||||
|
Mode string
|
||||||
|
Exclusive bool
|
||||||
|
Sort string
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryEncode turns options into querystring argument
|
||||||
|
func (opt *SearchRepoOptions) QueryEncode() string {
|
||||||
|
query := make(url.Values)
|
||||||
|
if opt.Keyword != "" {
|
||||||
|
query.Add("q", opt.Keyword)
|
||||||
|
}
|
||||||
|
|
||||||
|
query.Add("topic", fmt.Sprintf("%t", opt.Topic))
|
||||||
|
query.Add("includeDesc", fmt.Sprintf("%t", opt.IncludeDesc))
|
||||||
|
|
||||||
|
if opt.UID > 0 {
|
||||||
|
query.Add("uid", fmt.Sprintf("%d", opt.UID))
|
||||||
|
}
|
||||||
|
|
||||||
|
if opt.PriorityOwnerID > 0 {
|
||||||
|
query.Add("priority_owner_id", fmt.Sprintf("%d", opt.PriorityOwnerID))
|
||||||
|
}
|
||||||
|
|
||||||
|
if opt.StarredBy > 0 {
|
||||||
|
query.Add("starredBy", fmt.Sprintf("%d", opt.StarredBy))
|
||||||
|
}
|
||||||
|
|
||||||
|
query.Add("private", fmt.Sprintf("%t", opt.Private))
|
||||||
|
query.Add("template", fmt.Sprintf("%t", opt.Template))
|
||||||
|
|
||||||
|
if opt.Mode != "" {
|
||||||
|
query.Add("mode", opt.Mode)
|
||||||
|
}
|
||||||
|
|
||||||
|
query.Add("exclusive", fmt.Sprintf("%t", opt.Exclusive))
|
||||||
|
|
||||||
|
if opt.Sort != "" {
|
||||||
|
query.Add("sort", opt.Sort)
|
||||||
|
}
|
||||||
|
|
||||||
|
return query.Encode()
|
||||||
|
}
|
||||||
|
|
||||||
|
type searchRepoResponse struct {
|
||||||
|
Repos []*Repository `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// SearchRepos searches for repositories matching the given filters
|
||||||
|
func (c *Client) SearchRepos(opt SearchRepoOptions) ([]*Repository, error) {
|
||||||
|
resp := new(searchRepoResponse)
|
||||||
|
|
||||||
|
link, _ := url.Parse("/repos/search")
|
||||||
|
link.RawQuery = opt.QueryEncode()
|
||||||
|
|
||||||
|
err := c.getParsedResponse("GET", link.String(), nil, nil, &resp)
|
||||||
|
return resp.Repos, err
|
||||||
|
}
|
||||||
|
|
||||||
// CreateRepoOption options when creating repository
|
// CreateRepoOption options when creating repository
|
||||||
type CreateRepoOption struct {
|
type CreateRepoOption struct {
|
||||||
// Name of the repository to create
|
// Name of the repository to create
|
||||||
|
|
|
@ -31,6 +31,55 @@ func TestCreateRepo(t *testing.T) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSearchRepo(t *testing.T) {
|
||||||
|
log.Println("== TestSearchRepo ==")
|
||||||
|
c := newTestClient()
|
||||||
|
|
||||||
|
repo, err := createTestRepo(t, "RepoSearch1", c)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NoError(t, c.AddRepoTopic(repo.Owner.UserName, repo.Name, "TestTopic1"))
|
||||||
|
assert.NoError(t, c.AddRepoTopic(repo.Owner.UserName, repo.Name, "TestTopic2"))
|
||||||
|
|
||||||
|
repo, err = createTestRepo(t, "RepoSearch2", c)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NoError(t, c.AddRepoTopic(repo.Owner.UserName, repo.Name, "TestTopic1"))
|
||||||
|
|
||||||
|
repos, err := c.SearchRepos(SearchRepoOptions{
|
||||||
|
Keyword: "Search1",
|
||||||
|
IncludeDesc: true,
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, repos)
|
||||||
|
assert.Len(t, repos, 1)
|
||||||
|
|
||||||
|
repos, err = c.SearchRepos(SearchRepoOptions{
|
||||||
|
Keyword: "Search",
|
||||||
|
IncludeDesc: true,
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, repos)
|
||||||
|
assert.Len(t, repos, 2)
|
||||||
|
|
||||||
|
repos, err = c.SearchRepos(SearchRepoOptions{
|
||||||
|
Keyword: "TestTopic1",
|
||||||
|
Topic: true,
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, repos)
|
||||||
|
assert.Len(t, repos, 2)
|
||||||
|
|
||||||
|
repos, err = c.SearchRepos(SearchRepoOptions{
|
||||||
|
Keyword: "TestTopic2",
|
||||||
|
Topic: true,
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, repos)
|
||||||
|
assert.Len(t, repos, 1)
|
||||||
|
|
||||||
|
err = c.DeleteRepo(repo.Owner.UserName, repo.Name)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
func TestDeleteRepo(t *testing.T) {
|
func TestDeleteRepo(t *testing.T) {
|
||||||
log.Println("== TestDeleteRepo ==")
|
log.Println("== TestDeleteRepo ==")
|
||||||
c := newTestClient()
|
c := newTestClient()
|
||||||
|
|
Loading…
Reference in a new issue