stash: enable pagination

feature/refresh
Brad Rydzewski 6 years ago
parent 41e8153dc5
commit 69005888d7

@ -64,7 +64,10 @@ func (s *gitService) ListBranches(ctx context.Context, repo string, opts scm.Lis
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/branches?%s", namespace, name, encodeListOptions(opts))
out := new(branches)
res, err := s.client.do(ctx, "GET", path, nil, out)
copyPagination(out.pagination, res)
if !out.pagination.LastPage.Bool {
res.Page.First = 1
res.Page.Next = opts.Page + 1
}
return convertBranchList(out), res, err
}
@ -77,7 +80,10 @@ func (s *gitService) ListTags(ctx context.Context, repo string, opts scm.ListOpt
path := fmt.Sprintf("/rest/api/1.0/projects/%s/repos/%s/tags?%s", namespace, name, encodeListOptions(opts))
out := new(branches)
res, err := s.client.do(ctx, "GET", path, nil, &out)
copyPagination(out.pagination, res)
if !out.pagination.LastPage.Bool {
res.Page.First = 1
res.Page.Next = opts.Page + 1
}
return convertTagList(out), res, err
}
@ -86,7 +92,10 @@ func (s *gitService) ListChanges(ctx context.Context, repo, ref string, opts scm
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/commits/%s/changes?%s", namespace, name, ref, encodeListOptions(opts))
out := new(diffstats)
res, err := s.client.do(ctx, "GET", path, nil, &out)
copyPagination(out.pagination, res)
if !out.pagination.LastPage.Bool {
res.Page.First = 1
res.Page.Next = opts.Page + 1
}
return convertDiffstats(out), res, err
}

@ -37,7 +37,10 @@ func (s *pullService) List(ctx context.Context, repo string, opts scm.PullReques
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/pull-requests", namespace, name)
out := new(pullRequests)
res, err := s.client.do(ctx, "GET", path, nil, out)
copyPagination(out.pagination, res)
if !out.pagination.LastPage.Bool {
res.Page.First = 1
res.Page.Next = opts.Page + 1
}
return convertPullRequests(out), res, err
}
@ -46,7 +49,10 @@ func (s *pullService) ListChanges(ctx context.Context, repo string, number int,
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/pull-requests/%d/changes", namespace, name, number)
out := new(diffstats)
res, err := s.client.do(ctx, "GET", path, nil, out)
copyPagination(out.pagination, res)
if !out.pagination.LastPage.Bool {
res.Page.First = 1
res.Page.Next = opts.Page + 1
}
return convertDiffstats(out), res, err
}

@ -147,7 +147,10 @@ func (s *repositoryService) List(ctx context.Context, opts scm.ListOptions) ([]*
path := fmt.Sprintf("rest/api/1.0/repos?%s", encodeListRoleOptions(opts))
out := new(repositories)
res, err := s.client.do(ctx, "GET", path, nil, &out)
copyPagination(out.pagination, res)
if !out.pagination.LastPage.Bool {
res.Page.First = 1
res.Page.Next = opts.Page + 1
}
return convertRepositoryList(out), res, err
}
@ -157,7 +160,10 @@ func (s *repositoryService) ListHooks(ctx context.Context, repo string, opts scm
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/webhooks?%s", namespace, name, encodeListOptions(opts))
out := new(hooks)
res, err := s.client.do(ctx, "GET", path, nil, out)
copyPagination(out.pagination, res)
if !out.pagination.LastPage.Bool {
res.Page.First = 1
res.Page.Next = opts.Page + 1
}
return convertHookList(out), res, err
}

@ -163,11 +163,18 @@ func TestRepositoryList(t *testing.T) {
File("testdata/repos.json")
client, _ := New("http://example.com:7990")
got, _, err := client.Repositories.List(context.Background(), scm.ListOptions{Page: 3, Size: 25})
got, res, err := client.Repositories.List(context.Background(), scm.ListOptions{Page: 3, Size: 25})
if err != nil {
t.Error(err)
}
if got, want := res.Page.First, 1; got != want {
t.Errorf("Want Page.First %d, got %d", want, got)
}
if got, want := res.Page.Next, 4; got != want {
t.Errorf("Want Page.Next %d, got %d", want, got)
}
want := []*scm.Repository{}
raw, _ := ioutil.ReadFile("testdata/repos.json.golden")
json.Unmarshal(raw, &want)

@ -112,19 +112,11 @@ func (c *wrapper) do(ctx context.Context, method, path string, in, out interface
// pagination represents Bitbucket pagination properties
// embedded in list responses.
type pagination struct {
PageLen int `json:"pagelen"`
Page int `json:"page"`
Size int `json:"size"`
Next string `json:"next"`
}
type paginated struct {
Start null.Int `json:"start"`
Size null.Int `json:"size"`
Limit null.Int `json:"limit"`
LastPage null.Bool `json:"isLastPage"`
NextPage null.Int `json:"nextPageStart"`
Values json.RawMessage `json:"values"`
Start null.Int `json:"start"`
Size null.Int `json:"size"`
Limit null.Int `json:"limit"`
LastPage null.Bool `json:"isLastPage"`
NextPage null.Int `json:"nextPageStart"`
}
// Error represents a Github error.

@ -6,8 +6,6 @@ package stash
import (
"testing"
"github.com/drone/go-scm/scm"
)
func TestClient(t *testing.T) {
@ -36,14 +34,3 @@ func TestClient_Error(t *testing.T) {
t.Errorf("Expect error when invalid URL")
}
}
func testPage(res *scm.Response) func(t *testing.T) {
return func(t *testing.T) {
if got, want := res.Page.Next, 2; got != want {
t.Errorf("Want next page %d, got %d", want, got)
}
if got, want := res.Page.First, 1; got != want {
t.Errorf("Want first page %d, got %d", want, got)
}
}
}

@ -1,7 +1,7 @@
{
"size": 1,
"limit": 1,
"isLastPage": true,
"size": 25,
"limit": 25,
"isLastPage": false,
"values": [
{
"slug": "my-repo",

@ -56,16 +56,14 @@ func encodePullRequestListOptions(opts scm.PullRequestListOptions) string {
return params.Encode()
}
func copyPagination(from pagination, to *scm.Response) error {
if to == nil {
return nil
}
uri, err := url.Parse(from.Next)
if err != nil {
return err
}
page := uri.Query().Get("page")
to.Page.First = 1
to.Page.Next, _ = strconv.Atoi(page)
return nil
}
// func copyPagination(from pagination, to *scm.Response) error {
// if to == nil {
// return nil
// }
// to.Page.First = 1
// if from.LastPage.Bool {
// return nil
// }
// to.Page.Next =
// return nil
// }

@ -40,7 +40,7 @@ func Test_encodePullRequestListOptions(t *testing.T) {
Open: true,
Closed: true,
}
want := "limit=30&start=300&state=all"
want := "limit=30&start=270&state=all"
got := encodePullRequestListOptions(opts)
if got != want {
t.Errorf("Want encoded pr list options %q, got %q", want, got)

Loading…
Cancel
Save