Add Pagination Options for List Requests #205

Merged
lunny merged 36 commits from spawn2kill/go-sdk:pagination into master 2020-02-05 08:00:00 +00:00
2 changed files with 119 additions and 0 deletions
Showing only changes of commit da61989e67 - Show all commits

View File

@ -8,6 +8,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"time"
)
@ -74,6 +75,75 @@ func (c *Client) ListOrgRepos(org string) ([]*Repository, error) {
return repos, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/repos", org), nil, nil, &repos)
}
// SearchRepoOptions options for searching repositories
type SearchRepoOptions struct {
// https://try.gitea.io/api/swagger#/repository/repoSearch
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
type CreateRepoOption struct {
// Name of the repository to create

View File

@ -31,6 +31,55 @@ func TestCreateRepo(t *testing.T) {
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) {
log.Println("== TestDeleteRepo ==")
c := newTestClient()