Skip to content

Commit

Permalink
Merge pull request drone#3 from nathan-fps/master
Browse files Browse the repository at this point in the history
Paginate by URL
  • Loading branch information
bradrydzewski authored Dec 5, 2018
2 parents e4326a4 + f08275c commit 67e85b1
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 14 deletions.
10 changes: 6 additions & 4 deletions scm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ type (
// Page represents parsed link rel values for
// pagination.
Page struct {
Next int
Last int
First int
Prev int
Next int
NextURL string
Last int
First int
Prev int
}

// Rate represents the rate limit for the current
Expand All @@ -69,6 +70,7 @@ type (
// ListOptions specifies optional pagination
// parameters.
ListOptions struct {
URL string
Page int
Size int
}
Expand Down
3 changes: 3 additions & 0 deletions scm/driver/bitbucket/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func (s *repositoryService) FindPerms(ctx context.Context, repo string) (*scm.Pe
// List returns the user repository list.
func (s *repositoryService) List(ctx context.Context, opts scm.ListOptions) ([]*scm.Repository, *scm.Response, error) {
path := fmt.Sprintf("2.0/repositories?%s", encodeListRoleOptions(opts))
if opts.URL != "" {
path = opts.URL
}
out := new(repositories)
res, err := s.client.do(ctx, "GET", path, nil, &out)
copyPagination(out.pagination, res)
Expand Down
33 changes: 26 additions & 7 deletions scm/driver/bitbucket/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,38 @@ func TestRepositoryList(t *testing.T) {

gock.New("https://api.bitbucket.org").
Get("/2.0/repositories").
MatchParam("page", "1").
MatchParam("pagelen", "30").
MatchParam("after", "PLACEHOLDER").
MatchParam("pagelen", "1").
MatchParam("role", "member").
Reply(200).
Type("application/json").
File("testdata/repos-2.json")

gock.New("https://api.bitbucket.org").
Get("/2.0/repositories").
MatchParam("pagelen", "1").
MatchParam("role", "member").
Reply(200).
Type("application/json").
File("testdata/repos.json")

got := []*scm.Repository{}
opts := scm.ListOptions{Size: 1}
client, _ := New("https://api.bitbucket.org")
got, res, err := client.Repositories.List(context.Background(), scm.ListOptions{Page: 1, Size: 30})
if err != nil {
t.Error(err)

for {
repos, res, err := client.Repositories.List(context.Background(), opts)
if err != nil {
t.Error(err)
}
got = append(got, repos...)

opts.Page = res.Page.Next
opts.URL = res.Page.NextURL

if opts.Page == 0 && opts.URL == "" {
break
}
}

want := []*scm.Repository{}
Expand All @@ -113,8 +134,6 @@ func TestRepositoryList(t *testing.T) {
t.Errorf("Unexpected Results")
t.Log(diff)
}

t.Run("Page", testPage(res))
}

func TestStatusList(t *testing.T) {
Expand Down
109 changes: 109 additions & 0 deletions scm/driver/bitbucket/testdata/repos-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"pagelen": 1,
"values": [
{
"scm": "git",
"website": "",
"has_wiki": false,
"uuid": "{7dd600e6-0d9c-4801-b967-cb4cc17359ff}",
"links": {
"watchers": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/watchers"
},
"branches": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/refs\/branches"
},
"tags": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/refs\/tags"
},
"commits": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/commits"
},
"clone": [
{
"href": "https:\/\/[email protected]\/atlassian\/stash-example-plugin.git",
"name": "https"
},
{
"href": "[email protected]:atlassian\/stash-example-plugin.git",
"name": "ssh"
}
],
"self": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin"
},
"source": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/src"
},
"html": {
"href": "https:\/\/bitbucket.org\/atlassian\/stash-example-plugin"
},
"avatar": {
"href": "https:\/\/bytebucket.org\/ravatar\/%7B7dd600e6-0d9c-4801-b967-cb4cc17359ff%7D?ts=default"
},
"hooks": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/hooks"
},
"forks": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/forks"
},
"downloads": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/downloads"
},
"pullrequests": {
"href": "https:\/\/api.bitbucket.org\/2.0\/repositories\/atlassian\/stash-example-plugin\/pullrequests"
}
},
"fork_policy": "allow_forks",
"name": "stash-example-plugin",
"project": {
"key": "PROJ",
"type": "project",
"uuid": "{8b56daff-dbc7-4cae-a7a3-1228c526906b}",
"links": {
"self": {
"href": "https:\/\/api.bitbucket.org\/2.0\/teams\/atlassian\/projects\/PROJ"
},
"html": {
"href": "https:\/\/bitbucket.org\/account\/user\/atlassian\/projects\/PROJ"
},
"avatar": {
"href": "https:\/\/bitbucket.org\/account\/user\/atlassian\/projects\/PROJ\/avatar\/32"
}
},
"name": "Project: Atlassian"
},
"language": "",
"created_on": "2013-04-15T03:05:05.595458+00:00",
"mainbranch": {
"type": "branch",
"name": "master"
},
"full_name": "atlassian\/stash-example-plugin",
"has_issues": false,
"owner": {
"username": "atlassian",
"display_name": "Atlassian",
"type": "team",
"uuid": "{02b941e3-cfaa-40f9-9a58-cec53e20bdc3}",
"links": {
"self": {
"href": "https:\/\/api.bitbucket.org\/2.0\/teams\/atlassian"
},
"html": {
"href": "https:\/\/bitbucket.org\/atlassian\/"
},
"avatar": {
"href": "https:\/\/bitbucket.org\/account\/atlassian\/avatar\/32\/"
}
}
},
"updated_on": "2018-04-01T16:36:35.970175+00:00",
"size": 1116345,
"type": "repository",
"slug": "stash-example-plugin",
"is_private": true,
"description": "Examples on how to decorate various pages around Stash."
}
]
}
5 changes: 2 additions & 3 deletions scm/driver/bitbucket/testdata/repos.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"pagelen": 30,
"pagelen": 1,
"values": [
{
"scm": "git",
Expand Down Expand Up @@ -106,6 +106,5 @@
"description": "Examples on how to decorate various pages around Stash."
}
],
"page": 1,
"next": "https:\/\/bitbucket.org\/v2.0\/repositories?pagelen=30&page=2"
"next": "https:\/\/api.bitbucket.org\/2.0\/repositories?pagelen=1&after=PLACEHOLDER&role=member"
}
13 changes: 13 additions & 0 deletions scm/driver/bitbucket/testdata/repos.json.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
[
{
"ID": "{7dd600e6-0d9c-4801-b967-cb4cc17359ff}",
"Namespace": "atlassian",
"Name": "stash-example-plugin",
"Perm": null,
"Branch": "master",
"Private": true,
"Clone": "https://bitbucket.org/atlassian/stash-example-plugin.git",
"CloneSSH": "[email protected]:atlassian/stash-example-plugin.git",
"Link": "https://bitbucket.org/atlassian/stash-example-plugin",
"Created": "2013-04-15T03:05:05.595458Z",
"Updated": "2018-04-01T16:36:35.970175Z"
},
{
"ID": "{7dd600e6-0d9c-4801-b967-cb4cc17359ff}",
"Namespace": "atlassian",
Expand Down
1 change: 1 addition & 0 deletions scm/driver/bitbucket/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func copyPagination(from pagination, to *scm.Response) error {
if to == nil {
return nil
}
to.Page.NextURL = from.Next
uri, err := url.Parse(from.Next)
if err != nil {
return err
Expand Down

0 comments on commit 67e85b1

Please sign in to comment.