Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stash pr commits pagination #293

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scm/driver/stash/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (s *pullService) ListComments(context.Context, string, int, scm.ListOptions

func (s *pullService) ListCommits(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Commit, *scm.Response, error) {
namespace, name := scm.Split(repo)
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/pull-requests/%d/commits", namespace, name, number)
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/pull-requests/%d/commits?%s", namespace, name, number, encodeListOptionsV2(opts))
out := new(commits)
res, err := s.client.do(ctx, "GET", path, nil, out)
if !out.pagination.LastPage.Bool {
Expand Down
20 changes: 20 additions & 0 deletions scm/driver/stash/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
"github.com/drone/go-scm/scm"
)

const (
defaultLimit = 25
)

func encodeListOptions(opts scm.ListOptions) string {
params := url.Values{}
if opts.Page > 1 {
Expand All @@ -25,6 +29,22 @@ func encodeListOptions(opts scm.ListOptions) string {
return params.Encode()
}

func encodeListOptionsV2(opts scm.ListOptions) string {
params := url.Values{}
limit := defaultLimit
if opts.Size != 0 {
limit = opts.Size
}
params.Set("limit", strconv.Itoa(limit))

if opts.Page > 0 {
params.Set("start", strconv.Itoa(
(opts.Page-1)*limit),
)
}
return params.Encode()
}

func encodeBranchListOptions(opts scm.BranchListOptions) string {
params := url.Values{}
if opts.SearchTerm != "" {
Expand Down
22 changes: 22 additions & 0 deletions scm/driver/stash/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ func Test_encodeListOptions(t *testing.T) {
}
}

func Test_encodeListOptionsV2(t *testing.T) {
tests := []struct {
page int
size int
text string
}{
{page: 0, size: 30, text: "limit=30"},
{page: 1, size: 30, text: "limit=30&start=0"},
{page: 5, size: 30, text: "limit=30&start=120"},
{page: 2, size: 5, text: "limit=5&start=5"},
}
for _, test := range tests {
opts := scm.ListOptions{
Page: test.page,
Size: test.size,
}
if got, want := encodeListOptionsV2(opts), test.text; got != want {
t.Errorf("Want encoded list options %q, got %q", want, got)
}
}
}

func Test_encodePullRequestListOptions(t *testing.T) {
t.Parallel()
opts := scm.PullRequestListOptions{
Expand Down