Skip to content

Commit 02078dc

Browse files
Mya Pitzerusektrysmt
authored andcommitted
gh-57: added support for paginating ListForAccount and ListForTeam (#58)
* changes: - add .idea folder to .gitignore - rename pull_request.go * Changes: - NewOAuthToken func - go fmt * Changes: - implement Issues functions to interact with issues endpoint: List, Get, Create * remove traces of pp * port some common http methods * quick hack to get query strings in * more fixes * add PullRequest Struct * update deps * Unmarshall and decode PR#create * Changes: - change to use Service based structure - consolidate pull_request func * update issues.go to use IssuesService * add more fields to PullRequest struct * fix oops * Refactoring client and http to be less tedious * Refactoring client and http to be less tedious * Revert "Fix Issues CRUD" * update github.com/ktrysmt/go-bitbucket to github.com/davidji99/go-bitbucket * update PullRequest struct * break out pull request activities and commits * oops on pullrequests.go * comment out a bunch of code for now * temp * fix PR create * gh-57: added support for paginating ListForAccount and ListForTeam * gh-57: added json annotations for ListOptions * gh-57: moved over to using url.Values instead of encoding them manually
1 parent 1f1c5e7 commit 02078dc

24 files changed

+729
-266
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
vendor
2+
.idea

Gopkg.lock

Lines changed: 39 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# go-bitbucket
22

3-
<a class="repo-badge" href="https://godoc.org/github.com/ktrysmt/go-bitbucket"><img src="https://godoc.org/github.com/ktrysmt/go-bitbucket?status.svg" alt="go-bitbucket?status"></a>
4-
<a href="https://goreportcard.com/report/github.com/ktrysmt/go-bitbucket"><img class="badge" tag="github.com/ktrysmt/go-bitbucket" src="https://goreportcard.com/badge/github.com/ktrysmt/go-bitbucket"></a>
3+
<a class="repo-badge" href="https://godoc.org/github.com/davidji99/go-bitbucket"><img src="https://godoc.org/github.com/davidji99/go-bitbucket?status.svg" alt="go-bitbucket?status"></a>
4+
<a href="https://goreportcard.com/report/github.com/davidji99/go-bitbucket"><img class="badge" tag="github.com/davidji99/go-bitbucket" src="https://goreportcard.com/badge/github.com/davidji99/go-bitbucket"></a>
55

66
> Bitbucket-API library for golang.
77
@@ -15,7 +15,7 @@ And the response type is json format defined Bitbucket API.
1515
## Install
1616

1717
```sh
18-
go get github.com/ktrysmt/go-bitbucket
18+
go get github.com/davidji99/go-bitbucket
1919
```
2020

2121
## Usage
@@ -26,7 +26,7 @@ package main
2626
import (
2727
"fmt"
2828

29-
"github.com/ktrysmt/go-bitbucket"
29+
"github.com/davidji99/go-bitbucket"
3030
)
3131

3232
func main() {
@@ -71,7 +71,7 @@ It's using dep.
7171

7272
```sh
7373
go get github.com/golang/dep/...
74-
git clone https://github.com/ktrysmt/go-bitbucket
74+
git clone https://github.com/davidji99/go-bitbucket
7575
cd ./go-bitbucket
7676
dep ensure
7777
```

bitbucket.go

Lines changed: 108 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
package bitbucket
22

3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"net/http"
8+
"net/url"
9+
"sort"
10+
"strings"
11+
)
12+
313
var apiBaseURL = "https://api.bitbucket.org/2.0"
414

515
func GetApiBaseURL() string {
@@ -23,17 +33,17 @@ type user interface {
2333
}
2434

2535
type pullrequests interface {
26-
Create(opt PullRequestsOptions) (interface{}, error)
27-
Update(opt PullRequestsOptions) (interface{}, error)
28-
List(opt PullRequestsOptions) (interface{}, error)
29-
Get(opt PullRequestsOptions) (interface{}, error)
30-
Activities(opt PullRequestsOptions) (interface{}, error)
31-
Activity(opt PullRequestsOptions) (interface{}, error)
32-
Commits(opt PullRequestsOptions) (interface{}, error)
33-
Patch(opt PullRequestsOptions) (interface{}, error)
34-
Diff(opt PullRequestsOptions) (interface{}, error)
35-
Merge(opt PullRequestsOptions) (interface{}, error)
36-
Decline(opt PullRequestsOptions) (interface{}, error)
36+
Create(opt CreatePullRequestOpts) (interface{}, error)
37+
Update(opt CreatePullRequestOpts) (interface{}, error)
38+
List(opt CreatePullRequestOpts) (interface{}, error)
39+
Get(opt CreatePullRequestOpts) (interface{}, error)
40+
Activities(opt CreatePullRequestOpts) (interface{}, error)
41+
Activity(opt CreatePullRequestOpts) (interface{}, error)
42+
Commits(opt CreatePullRequestOpts) (interface{}, error)
43+
Patch(opt CreatePullRequestOpts) (interface{}, error)
44+
Diff(opt CreatePullRequestOpts) (interface{}, error)
45+
Merge(opt CreatePullRequestOpts) (interface{}, error)
46+
Decline(opt CreatePullRequestOpts) (interface{}, error)
3747
}
3848

3949
type repository interface {
@@ -95,9 +105,15 @@ type teams interface {
95105
Projects(teamname string) (interface{}, error)
96106
}
97107

108+
type ListOptions struct {
109+
Page uint64 `json:"page"`
110+
PageLen uint64 `json:"pagelen"`
111+
}
112+
98113
type RepositoriesOptions struct {
99-
Owner string `json:"owner"`
100-
Role string `json:"role"` // role=[owner|admin|contributor|member]
114+
ListOptions *ListOptions `json:"list_options"`
115+
Owner string `json:"owner"`
116+
Role string `json:"role"` // role=[owner|admin|contributor|member]
101117
}
102118

103119
type RepositoryOptions struct {
@@ -114,22 +130,6 @@ type RepositoryOptions struct {
114130
Project string `json:"project"`
115131
}
116132

117-
type PullRequestsOptions struct {
118-
ID string `json:"id"`
119-
CommentID string `json:"comment_id"`
120-
Owner string `json:"owner"`
121-
RepoSlug string `json:"repo_slug"`
122-
Title string `json:"title"`
123-
Description string `json:"description"`
124-
CloseSourceBranch bool `json:"close_source_branch"`
125-
SourceBranch string `json:"source_branch"`
126-
SourceRepository string `json:"source_repository"`
127-
DestinationBranch string `json:"destination_branch"`
128-
DestinationCommit string `json:"destination_repository"`
129-
Message string `json:"message"`
130-
Reviewers []string `json:"reviewers"`
131-
}
132-
133133
type CommitsOptions struct {
134134
Owner string `json:"owner"`
135135
RepoSlug string `json:"repo_slug"`
@@ -141,12 +141,11 @@ type CommitsOptions struct {
141141
}
142142

143143
type CommitStatusOptions struct {
144-
Key string `json:"key"`
145-
Url string `json:"url"`
146-
State string `json:"state"`
147-
Name string `json:"name"`
144+
Key string `json:"key"`
145+
Url string `json:"url"`
146+
State string `json:"state"`
147+
Name string `json:"name"`
148148
Description string `json:"description"`
149-
150149
}
151150

152151
type BranchRestrictionsOptions struct {
@@ -201,8 +200,78 @@ type RepositoryPipelineKeyPairOptions struct {
201200
}
202201

203202
type DownloadsOptions struct {
204-
Owner string `json:"owner"`
205-
RepoSlug string `json:"repo_slug"`
206-
FilePath string `json:"filepath"`
207-
FileName string `json:"filename"`
208-
}
203+
Owner string `json:"owner"`
204+
RepoSlug string `json:"repo_slug"`
205+
FilePath string `json:"filepath"`
206+
FileName string `json:"filename"`
207+
}
208+
209+
type Response struct {
210+
*http.Response
211+
}
212+
213+
// newResponse creates a new Response for the provided http.Response.
214+
func newResponse(r *http.Response) *Response {
215+
response := &Response{Response: r}
216+
return response
217+
}
218+
219+
type ErrorResponse struct {
220+
Body []byte
221+
Response *http.Response
222+
Message string
223+
}
224+
225+
func (e *ErrorResponse) Error() string {
226+
path, _ := url.QueryUnescape(e.Response.Request.URL.Path)
227+
u := fmt.Sprintf("%s://%s%s", e.Response.Request.URL.Scheme, e.Response.Request.URL.Host, path)
228+
return fmt.Sprintf("%s %s: %d %s", e.Response.Request.Method, u, e.Response.StatusCode, e.Message)
229+
}
230+
231+
// CheckResponse checks the API response for errors, and returns them if present.
232+
func CheckResponse(r *http.Response) error {
233+
switch r.StatusCode {
234+
case 200, 201, 202, 204, 304:
235+
return nil
236+
}
237+
238+
errorResponse := &ErrorResponse{Response: r}
239+
data, err := ioutil.ReadAll(r.Body)
240+
if err == nil && data != nil {
241+
errorResponse.Body = data
242+
243+
var raw interface{}
244+
if err := json.Unmarshal(data, &raw); err != nil {
245+
errorResponse.Message = "failed to parse unknown error format"
246+
} else {
247+
errorResponse.Message = parseError(raw)
248+
}
249+
}
250+
251+
return errorResponse
252+
}
253+
254+
func parseError(raw interface{}) string {
255+
switch raw := raw.(type) {
256+
case string:
257+
return raw
258+
259+
case []interface{}:
260+
var errs []string
261+
for _, v := range raw {
262+
errs = append(errs, parseError(v))
263+
}
264+
return fmt.Sprintf("[%s]", strings.Join(errs, ", "))
265+
266+
case map[string]interface{}:
267+
var errs []string
268+
for k, v := range raw {
269+
errs = append(errs, fmt.Sprintf("{%s: %s}", k, parseError(v)))
270+
}
271+
sort.Strings(errs)
272+
return strings.Join(errs, ", ")
273+
274+
default:
275+
return fmt.Sprintf("failed to parse unexpected error type: %T", raw)
276+
}
277+
}

branchrestrictions.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ package bitbucket
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"os"
6-
7-
"github.com/k0kubun/pp"
87
)
98

109
type BranchRestrictions struct {
@@ -13,29 +12,29 @@ type BranchRestrictions struct {
1312

1413
func (b *BranchRestrictions) Gets(bo *BranchRestrictionsOptions) (interface{}, error) {
1514
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions", bo.Owner, bo.RepoSlug)
16-
return b.c.execute("GET", urlStr, "")
15+
return b.c.execute("GET", urlStr, "", "")
1716
}
1817

1918
func (b *BranchRestrictions) Create(bo *BranchRestrictionsOptions) (interface{}, error) {
2019
data := b.buildBranchRestrictionsBody(bo)
2120
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions", bo.Owner, bo.RepoSlug)
22-
return b.c.execute("POST", urlStr, data)
21+
return b.c.execute("POST", urlStr, data, "")
2322
}
2423

2524
func (b *BranchRestrictions) Get(bo *BranchRestrictionsOptions) (interface{}, error) {
2625
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions/%s", bo.Owner, bo.RepoSlug, bo.ID)
27-
return b.c.execute("GET", urlStr, "")
26+
return b.c.execute("GET", urlStr, "", "")
2827
}
2928

3029
func (b *BranchRestrictions) Update(bo *BranchRestrictionsOptions) (interface{}, error) {
3130
data := b.buildBranchRestrictionsBody(bo)
3231
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions/%s", bo.Owner, bo.RepoSlug, bo.ID)
33-
return b.c.execute("PUT", urlStr, data)
32+
return b.c.execute("PUT", urlStr, data, "")
3433
}
3534

3635
func (b *BranchRestrictions) Delete(bo *BranchRestrictionsOptions) (interface{}, error) {
3736
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions/%s", bo.Owner, bo.RepoSlug, bo.ID)
38-
return b.c.execute("DELETE", urlStr, "")
37+
return b.c.execute("DELETE", urlStr, "", "")
3938
}
4039

4140
type branchRestrictionsBody struct {
@@ -122,7 +121,7 @@ func (b *BranchRestrictions) buildBranchRestrictionsBody(bo *BranchRestrictionsO
122121

123122
data, err := json.Marshal(body)
124123
if err != nil {
125-
pp.Println(err)
124+
fmt.Println(err)
126125
os.Exit(9)
127126
}
128127

0 commit comments

Comments
 (0)