Skip to content

Commit 840225b

Browse files
committed
merge changes from the forked repo
2 parents 9f3883a + 012db82 commit 840225b

14 files changed

+162
-59
lines changed

bitbucket.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,10 @@ type pipelines interface {
139139
}
140140

141141
type RepositoriesOptions struct {
142-
Owner string `json:"owner"`
143-
Role string `json:"role"` // role=[owner|admin|contributor|member]
142+
Owner string `json:"owner"`
143+
Role string `json:"role"` // role=[owner|admin|contributor|member]
144+
Page *int `json:"page"`
145+
Keyword *string `json:"keyword"`
144146
}
145147

146148
type RepositoryOptions struct {
@@ -339,6 +341,7 @@ type CommitsOptions struct {
339341
Include string `json:"include"`
340342
Exclude string `json:"exclude"`
341343
CommentID string `json:"comment_id"`
344+
Page *int `json:"page"`
342345
}
343346

344347
type CommitStatusOptions struct {

branchrestrictions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type BranchRestrictions struct {
1717

1818
func (b *BranchRestrictions) Gets(bo *BranchRestrictionsOptions) (interface{}, error) {
1919
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions", bo.Owner, bo.RepoSlug)
20-
return b.c.executePaginated("GET", urlStr, "")
20+
return b.c.executePaginated("GET", urlStr, "", nil)
2121
}
2222

2323
func (b *BranchRestrictions) Create(bo *BranchRestrictionsOptions) (*BranchRestrictions, error) {

client.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func (c *Client) execute(method string, urlStr string, text string) (interface{}
249249
return result, nil
250250
}
251251

252-
func (c *Client) executePaginated(method string, urlStr string, text string) (interface{}, error) {
252+
func (c *Client) executePaginated(method string, urlStr string, text string, page *int) (interface{}, error) {
253253
if c.Pagelen != DEFAULT_PAGE_LENGTH {
254254
urlObj, err := url.Parse(urlStr)
255255
if err != nil {
@@ -271,7 +271,7 @@ func (c *Client) executePaginated(method string, urlStr string, text string) (in
271271
}
272272

273273
c.authenticateRequest(req)
274-
result, err := c.doPaginatedRequest(req, false)
274+
result, err := c.doPaginatedRequest(req, page, false)
275275
if err != nil {
276276
return nil, err
277277
}
@@ -358,7 +358,19 @@ func (c *Client) doRequest(req *http.Request, emptyResponse bool) (interface{},
358358
return result, nil
359359
}
360360

361-
func (c *Client) doPaginatedRequest(req *http.Request, emptyResponse bool) (interface{}, error) {
361+
func (c *Client) doPaginatedRequest(req *http.Request, page *int, emptyResponse bool) (interface{}, error) {
362+
disableAutoPaging := c.DisableAutoPaging
363+
curPage := 1
364+
if page != nil {
365+
disableAutoPaging = true
366+
curPage = *page
367+
q := req.URL.Query()
368+
q.Set("page", strconv.Itoa(curPage))
369+
req.URL.RawQuery = q.Encode()
370+
}
371+
// q.Encode() does not encode "~".
372+
req.URL.RawQuery = strings.ReplaceAll(req.URL.RawQuery, "~", "%7E")
373+
362374
resBody, err := c.doRawRequest(req, emptyResponse)
363375
if err != nil {
364376
return nil, err
@@ -375,18 +387,15 @@ func (c *Client) doPaginatedRequest(req *http.Request, emptyResponse bool) (inte
375387
}
376388

377389
responsePaginated := &Response{}
378-
var curPage int
379-
380390
err = json.Unmarshal(responseBytes, responsePaginated)
381391
if err == nil && len(responsePaginated.Values) > 0 {
382-
var values []interface{}
392+
values := responsePaginated.Values
383393
for {
384-
curPage++
385-
values = append(values, responsePaginated.Values...)
386-
if c.DisableAutoPaging || len(responsePaginated.Next) == 0 ||
394+
if disableAutoPaging || responsePaginated.Next == "" ||
387395
(curPage >= c.LimitPages && c.LimitPages != 0) {
388396
break
389397
}
398+
curPage++
390399
newReq, err := http.NewRequest(req.Method, responsePaginated.Next, nil)
391400
if err != nil {
392401
return resBody, err
@@ -399,6 +408,7 @@ func (c *Client) doPaginatedRequest(req *http.Request, emptyResponse bool) (inte
399408

400409
responsePaginated = &Response{}
401410
json.NewDecoder(resp).Decode(responsePaginated)
411+
values = append(values, responsePaginated.Values...)
402412
}
403413
responsePaginated.Values = values
404414
responseBytes, err = json.Marshal(responsePaginated)

commits.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type Commits struct {
1212
func (cm *Commits) GetCommits(cmo *CommitsOptions) (interface{}, error) {
1313
urlStr := cm.c.requestUrl("/repositories/%s/%s/commits/%s", cmo.Owner, cmo.RepoSlug, cmo.Branchortag)
1414
urlStr += cm.buildCommitsQuery(cmo.Include, cmo.Exclude)
15-
return cm.c.executePaginated("GET", urlStr, "")
15+
return cm.c.executePaginated("GET", urlStr, "", cmo.Page)
1616
}
1717

1818
func (cm *Commits) GetCommit(cmo *CommitsOptions) (interface{}, error) {
@@ -22,7 +22,7 @@ func (cm *Commits) GetCommit(cmo *CommitsOptions) (interface{}, error) {
2222

2323
func (cm *Commits) GetCommitComments(cmo *CommitsOptions) (interface{}, error) {
2424
urlStr := cm.c.requestUrl("/repositories/%s/%s/commit/%s/comments", cmo.Owner, cmo.RepoSlug, cmo.Revision)
25-
return cm.c.executePaginated("GET", urlStr, "")
25+
return cm.c.executePaginated("GET", urlStr, "", nil)
2626
}
2727

2828
func (cm *Commits) GetCommitComment(cmo *CommitsOptions) (interface{}, error) {
@@ -32,7 +32,7 @@ func (cm *Commits) GetCommitComment(cmo *CommitsOptions) (interface{}, error) {
3232

3333
func (cm *Commits) GetCommitStatuses(cmo *CommitsOptions) (interface{}, error) {
3434
urlStr := cm.c.requestUrl("/repositories/%s/%s/commit/%s/statuses", cmo.Owner, cmo.RepoSlug, cmo.Revision)
35-
return cm.c.executePaginated("GET", urlStr, "")
35+
return cm.c.executePaginated("GET", urlStr, "", nil)
3636
}
3737

3838
func (cm *Commits) GetCommitStatus(cmo *CommitsOptions, commitStatusKey string) (interface{}, error) {

downloads.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ func (dl *Downloads) Create(do *DownloadsOptions) (interface{}, error) {
1111

1212
func (dl *Downloads) List(do *DownloadsOptions) (interface{}, error) {
1313
urlStr := dl.c.requestUrl("/repositories/%s/%s/downloads", do.Owner, do.RepoSlug)
14-
return dl.c.executePaginated("GET", urlStr, "")
14+
return dl.c.executePaginated("GET", urlStr, "", nil)
1515
}

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
module github.com/diggerhq/go-bitbucket
1+
module github.com/ktrysmt/go-bitbucket
22

33
go 1.14
44

55
require (
6-
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
6+
github.com/diggerhq/go-bitbucket v0.0.6
77
github.com/k0kubun/pp v3.0.1+incompatible
8-
github.com/mattn/go-colorable v0.1.13 // indirect
98
github.com/mitchellh/mapstructure v1.5.0
109
github.com/stretchr/testify v1.8.1
1110
golang.org/x/net v0.7.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ cloud.google.com/go/compute/metadata v0.2.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1h
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/diggerhq/go-bitbucket v0.0.6 h1:bW4e3SCQVYYHlaa9BFpKc+3oDD1Pj9EwCygAkKpE92o=
6+
github.com/diggerhq/go-bitbucket v0.0.6/go.mod h1:IwCbd2t5rx6X3QlBAuE1H1b2KesJxncY3qFqquEqx3Y=
57
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
68
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
79
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=

issues.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (p *Issues) Gets(io *IssuesOptions) (interface{}, error) {
3737
url.RawQuery = query.Encode()
3838
}
3939

40-
return p.c.executePaginated("GET", url.String(), "")
40+
return p.c.executePaginated("GET", url.String(), "", nil)
4141
}
4242

4343
func (p *Issues) Get(io *IssuesOptions) (interface{}, error) {

pipelines.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (p *Pipelines) List(po *PipelinesOptions) (interface{}, error) {
4747
urlStr = parsed.String()
4848
}
4949

50-
return p.c.executePaginated("GET", urlStr, "")
50+
return p.c.executePaginated("GET", urlStr, "", nil)
5151
}
5252

5353
func (p *Pipelines) Get(po *PipelinesOptions) (interface{}, error) {
@@ -91,7 +91,7 @@ func (p *Pipelines) ListSteps(po *PipelinesOptions) (interface{}, error) {
9191
urlStr = parsed.String()
9292
}
9393

94-
return p.c.executePaginated("GET", urlStr, "")
94+
return p.c.executePaginated("GET", urlStr, "", nil)
9595
}
9696

9797
func (p *Pipelines) GetStep(po *PipelinesOptions) (interface{}, error) {

pullrequests.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (p *PullRequests) Gets(po *PullRequestsOptions) (interface{}, error) {
6565
urlStr = parsed.String()
6666
}
6767

68-
return p.c.executePaginated("GET", urlStr, "")
68+
return p.c.executePaginated("GET", urlStr, "", nil)
6969
}
7070

7171
func (p *PullRequests) Get(po *PullRequestsOptions) (interface{}, error) {
@@ -75,7 +75,7 @@ func (p *PullRequests) Get(po *PullRequestsOptions) (interface{}, error) {
7575

7676
func (p *PullRequests) Activities(po *PullRequestsOptions) (interface{}, error) {
7777
urlStr := p.c.GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/pullrequests/activity"
78-
return p.c.executePaginated("GET", urlStr, "")
78+
return p.c.executePaginated("GET", urlStr, "", nil)
7979
}
8080

8181
func (p *PullRequests) Activity(po *PullRequestsOptions) (interface{}, error) {
@@ -85,7 +85,7 @@ func (p *PullRequests) Activity(po *PullRequestsOptions) (interface{}, error) {
8585

8686
func (p *PullRequests) Commits(po *PullRequestsOptions) (interface{}, error) {
8787
urlStr := p.c.GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/pullrequests/" + po.ID + "/commits"
88-
return p.c.executePaginated("GET", urlStr, "")
88+
return p.c.executePaginated("GET", urlStr, "", nil)
8989
}
9090

9191
func (p *PullRequests) Patch(po *PullRequestsOptions) (interface{}, error) {
@@ -158,7 +158,7 @@ func (p *PullRequests) UpdateComment(co *PullRequestCommentOptions) (interface{}
158158

159159
func (p *PullRequests) GetComments(po *PullRequestsOptions) (interface{}, error) {
160160
urlStr := p.c.GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/pullrequests/" + po.ID + "/comments/"
161-
return p.c.executePaginated("GET", urlStr, "")
161+
return p.c.executePaginated("GET", urlStr, "", nil)
162162
}
163163

164164
func (p *PullRequests) GetComment(po *PullRequestsOptions) (interface{}, error) {
@@ -189,7 +189,7 @@ func (p *PullRequests) Statuses(po *PullRequestsOptions) (interface{}, error) {
189189
parsed.RawQuery = query.Encode()
190190
urlStr = parsed.String()
191191
}
192-
return p.c.executePaginated("GET", urlStr, "")
192+
return p.c.executePaginated("GET", urlStr, "", nil)
193193
}
194194

195195
func (p *PullRequests) buildPullRequestBody(po *PullRequestsOptions) (string, error) {

0 commit comments

Comments
 (0)