Skip to content

Commit 294a5aa

Browse files
Merge pull request #293 from drone/stash_pr_commits_pagination
Stash pr commits pagination
2 parents 18b3b41 + 4f7b529 commit 294a5aa

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

scm/driver/stash/pr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (s *pullService) ListComments(context.Context, string, int, scm.ListOptions
6767

6868
func (s *pullService) ListCommits(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Commit, *scm.Response, error) {
6969
namespace, name := scm.Split(repo)
70-
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/pull-requests/%d/commits", namespace, name, number)
70+
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/pull-requests/%d/commits?%s", namespace, name, number, encodeListOptionsV2(opts))
7171
out := new(commits)
7272
res, err := s.client.do(ctx, "GET", path, nil, out)
7373
if !out.pagination.LastPage.Bool {

scm/driver/stash/util.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import (
1212
"github.com/drone/go-scm/scm"
1313
)
1414

15+
const (
16+
defaultLimit = 25
17+
)
18+
1519
func encodeListOptions(opts scm.ListOptions) string {
1620
params := url.Values{}
1721
if opts.Page > 1 {
@@ -25,6 +29,22 @@ func encodeListOptions(opts scm.ListOptions) string {
2529
return params.Encode()
2630
}
2731

32+
func encodeListOptionsV2(opts scm.ListOptions) string {
33+
params := url.Values{}
34+
limit := defaultLimit
35+
if opts.Size != 0 {
36+
limit = opts.Size
37+
}
38+
params.Set("limit", strconv.Itoa(limit))
39+
40+
if opts.Page > 0 {
41+
params.Set("start", strconv.Itoa(
42+
(opts.Page-1)*limit),
43+
)
44+
}
45+
return params.Encode()
46+
}
47+
2848
func encodeBranchListOptions(opts scm.BranchListOptions) string {
2949
params := url.Values{}
3050
if opts.SearchTerm != "" {

scm/driver/stash/util_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,28 @@ func Test_encodeListOptions(t *testing.T) {
3232
}
3333
}
3434

35+
func Test_encodeListOptionsV2(t *testing.T) {
36+
tests := []struct {
37+
page int
38+
size int
39+
text string
40+
}{
41+
{page: 0, size: 30, text: "limit=30"},
42+
{page: 1, size: 30, text: "limit=30&start=0"},
43+
{page: 5, size: 30, text: "limit=30&start=120"},
44+
{page: 2, size: 5, text: "limit=5&start=5"},
45+
}
46+
for _, test := range tests {
47+
opts := scm.ListOptions{
48+
Page: test.page,
49+
Size: test.size,
50+
}
51+
if got, want := encodeListOptionsV2(opts), test.text; got != want {
52+
t.Errorf("Want encoded list options %q, got %q", want, got)
53+
}
54+
}
55+
}
56+
3557
func Test_encodePullRequestListOptions(t *testing.T) {
3658
t.Parallel()
3759
opts := scm.PullRequestListOptions{

0 commit comments

Comments
 (0)