Skip to content

Commit 95252d6

Browse files
committed
Fix pagination being ignored for Gitea
1 parent a3a4721 commit 95252d6

File tree

7 files changed

+153
-16
lines changed

7 files changed

+153
-16
lines changed

scm/driver/gitea/git.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ func (s *gitService) FindTag(ctx context.Context, repo, name string) (*scm.Refer
3636
return nil, nil, scm.ErrNotSupported
3737
}
3838

39-
func (s *gitService) ListBranches(ctx context.Context, repo string, _ scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
40-
path := fmt.Sprintf("api/v1/repos/%s/branches", repo)
39+
func (s *gitService) ListBranches(ctx context.Context, repo string, opts scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
40+
path := fmt.Sprintf("api/v1/repos/%s/branches?%s", repo, encodeListOptions(opts))
4141
out := []*branch{}
4242
res, err := s.client.do(ctx, "GET", path, nil, &out)
4343
return convertBranchList(out), res, err

scm/driver/gitea/issue.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ func (s *issueService) FindComment(ctx context.Context, repo string, index, id i
2727
return nil, nil, scm.ErrNotSupported
2828
}
2929

30-
func (s *issueService) List(ctx context.Context, repo string, _ scm.IssueListOptions) ([]*scm.Issue, *scm.Response, error) {
31-
path := fmt.Sprintf("api/v1/repos/%s/issues", repo)
30+
func (s *issueService) List(ctx context.Context, repo string, opts scm.IssueListOptions) ([]*scm.Issue, *scm.Response, error) {
31+
path := fmt.Sprintf("api/v1/repos/%s/issues?%s", repo, encodeIssueListOptions(opts))
3232
out := []*issue{}
3333
res, err := s.client.do(ctx, "GET", path, nil, &out)
3434
return convertIssueList(out), res, err
3535
}
3636

37-
func (s *issueService) ListComments(ctx context.Context, repo string, index int, _ scm.ListOptions) ([]*scm.Comment, *scm.Response, error) {
38-
path := fmt.Sprintf("api/v1/repos/%s/issues/%d/comments", repo, index)
37+
func (s *issueService) ListComments(ctx context.Context, repo string, index int, opts scm.ListOptions) ([]*scm.Comment, *scm.Response, error) {
38+
path := fmt.Sprintf("api/v1/repos/%s/issues/%d/comments?%s", repo, index, encodeListOptions(opts))
3939
out := []*issueComment{}
4040
res, err := s.client.do(ctx, "GET", path, nil, &out)
4141
return convertIssueCommentList(out), res, err

scm/driver/gitea/org.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ func (s *organizationService) FindMembership(ctx context.Context, name, username
2626
return nil, nil, scm.ErrNotSupported
2727
}
2828

29-
func (s *organizationService) List(ctx context.Context, _ scm.ListOptions) ([]*scm.Organization, *scm.Response, error) {
30-
var out []*org
31-
res, err := s.client.do(ctx, "GET", "api/v1/user/orgs", nil, &out)
29+
func (s *organizationService) List(ctx context.Context, opts scm.ListOptions) ([]*scm.Organization, *scm.Response, error) {
30+
path := fmt.Sprintf("api/v1/user/orgs?%s", encodeListOptions(opts))
31+
out := []*org{}
32+
res, err := s.client.do(ctx, "GET", path, nil, &out)
3233
return convertOrgList(out), res, err
3334
}
3435

scm/driver/gitea/pr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (s *pullService) FindComment(context.Context, string, int, int) (*scm.Comme
2828
}
2929

3030
func (s *pullService) List(ctx context.Context, repo string, opts scm.PullRequestListOptions) ([]*scm.PullRequest, *scm.Response, error) {
31-
path := fmt.Sprintf("api/v1/repos/%s/pulls", repo)
31+
path := fmt.Sprintf("api/v1/repos/%s/pulls?%s", repo, encodePullRequestListOptions(opts))
3232
out := []*pr{}
3333
res, err := s.client.do(ctx, "GET", path, nil, &out)
3434
return convertPullRequests(out), res, err

scm/driver/gitea/repo.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,22 @@ func (s *repositoryService) FindPerms(ctx context.Context, repo string) (*scm.Pe
3939
return convertRepository(out).Perm, res, err
4040
}
4141

42-
func (s *repositoryService) List(ctx context.Context, _ scm.ListOptions) ([]*scm.Repository, *scm.Response, error) {
43-
path := fmt.Sprintf("api/v1/user/repos")
42+
func (s *repositoryService) List(ctx context.Context, opts scm.ListOptions) ([]*scm.Repository, *scm.Response, error) {
43+
path := fmt.Sprintf("api/v1/user/repos?%s", encodeListOptions(opts))
4444
out := []*repository{}
4545
res, err := s.client.do(ctx, "GET", path, nil, &out)
4646
return convertRepositoryList(out), res, err
4747
}
4848

49-
func (s *repositoryService) ListHooks(ctx context.Context, repo string, _ scm.ListOptions) ([]*scm.Hook, *scm.Response, error) {
50-
path := fmt.Sprintf("api/v1/repos/%s/hooks", repo)
49+
func (s *repositoryService) ListHooks(ctx context.Context, repo string, opts scm.ListOptions) ([]*scm.Hook, *scm.Response, error) {
50+
path := fmt.Sprintf("api/v1/repos/%s/hooks?%s", repo, encodeListOptions(opts))
5151
out := []*hook{}
5252
res, err := s.client.do(ctx, "GET", path, nil, &out)
5353
return convertHookList(out), res, err
5454
}
5555

56-
func (s *repositoryService) ListStatus(ctx context.Context, repo string, ref string, _ scm.ListOptions) ([]*scm.Status, *scm.Response, error) {
57-
path := fmt.Sprintf("api/v1/repos/%s/statuses/%s", repo, ref)
56+
func (s *repositoryService) ListStatus(ctx context.Context, repo string, ref string, opts scm.ListOptions) ([]*scm.Status, *scm.Response, error) {
57+
path := fmt.Sprintf("api/v1/repos/%s/statuses/%s?%s", repo, ref, encodeListOptions(opts))
5858
out := []*status{}
5959
res, err := s.client.do(ctx, "GET", path, nil, &out)
6060
return convertStatusList(out), res, err

scm/driver/gitea/util.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2017 Drone.IO Inc. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package gitea
6+
7+
import (
8+
"net/url"
9+
"strconv"
10+
11+
"github.com/drone/go-scm/scm"
12+
)
13+
14+
func encodeListOptions(opts scm.ListOptions) string {
15+
params := url.Values{}
16+
if opts.Page != 0 {
17+
params.Set("page", strconv.Itoa(opts.Page))
18+
}
19+
if opts.Size != 0 {
20+
params.Set("per_page", strconv.Itoa(opts.Size))
21+
}
22+
return params.Encode()
23+
}
24+
25+
func encodeIssueListOptions(opts scm.IssueListOptions) string {
26+
params := url.Values{}
27+
if opts.Page != 0 {
28+
params.Set("page", strconv.Itoa(opts.Page))
29+
}
30+
if opts.Size != 0 {
31+
params.Set("per_page", strconv.Itoa(opts.Size))
32+
}
33+
if opts.Open && opts.Closed {
34+
params.Set("state", "all")
35+
} else if opts.Closed {
36+
params.Set("state", "closed")
37+
}
38+
return params.Encode()
39+
}
40+
41+
func encodePullRequestListOptions(opts scm.PullRequestListOptions) string {
42+
params := url.Values{}
43+
if opts.Page != 0 {
44+
params.Set("page", strconv.Itoa(opts.Page))
45+
}
46+
if opts.Size != 0 {
47+
params.Set("per_page", strconv.Itoa(opts.Size))
48+
}
49+
if opts.Open && opts.Closed {
50+
params.Set("state", "all")
51+
} else if opts.Closed {
52+
params.Set("state", "closed")
53+
}
54+
return params.Encode()
55+
}

scm/driver/gitea/util_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2017 Drone.IO Inc. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package gitea
6+
7+
import (
8+
"testing"
9+
10+
"github.com/drone/go-scm/scm"
11+
)
12+
13+
func Test_encodeListOptions(t *testing.T) {
14+
opts := scm.ListOptions{
15+
Page: 10,
16+
Size: 30,
17+
}
18+
want := "page=10&per_page=30"
19+
got := encodeListOptions(opts)
20+
if got != want {
21+
t.Errorf("Want encoded list options %q, got %q", want, got)
22+
}
23+
}
24+
25+
func Test_encodeIssueListOptions(t *testing.T) {
26+
opts := scm.IssueListOptions{
27+
Page: 10,
28+
Size: 30,
29+
Open: true,
30+
Closed: true,
31+
}
32+
want := "page=10&per_page=30&state=all"
33+
got := encodeIssueListOptions(opts)
34+
if got != want {
35+
t.Errorf("Want encoded issue list options %q, got %q", want, got)
36+
}
37+
}
38+
39+
func Test_encodeIssueListOptions_Closed(t *testing.T) {
40+
opts := scm.IssueListOptions{
41+
Page: 10,
42+
Size: 30,
43+
Open: false,
44+
Closed: true,
45+
}
46+
want := "page=10&per_page=30&state=closed"
47+
got := encodeIssueListOptions(opts)
48+
if got != want {
49+
t.Errorf("Want encoded issue list options %q, got %q", want, got)
50+
}
51+
}
52+
53+
func Test_encodePullRequestListOptions(t *testing.T) {
54+
t.Parallel()
55+
opts := scm.PullRequestListOptions{
56+
Page: 10,
57+
Size: 30,
58+
Open: true,
59+
Closed: true,
60+
}
61+
want := "page=10&per_page=30&state=all"
62+
got := encodePullRequestListOptions(opts)
63+
if got != want {
64+
t.Errorf("Want encoded pr list options %q, got %q", want, got)
65+
}
66+
}
67+
68+
func Test_encodePullRequestListOptions_Closed(t *testing.T) {
69+
t.Parallel()
70+
opts := scm.PullRequestListOptions{
71+
Page: 10,
72+
Size: 30,
73+
Open: false,
74+
Closed: true,
75+
}
76+
want := "page=10&per_page=30&state=closed"
77+
got := encodePullRequestListOptions(opts)
78+
if got != want {
79+
t.Errorf("Want encoded pr list options %q, got %q", want, got)
80+
}
81+
}

0 commit comments

Comments
 (0)