1
1
package bitbucket
2
2
3
+ import (
4
+ "encoding/json"
5
+ "fmt"
6
+ "io/ioutil"
7
+ "net/http"
8
+ "net/url"
9
+ "sort"
10
+ "strings"
11
+ )
12
+
3
13
var apiBaseURL = "https://api.bitbucket.org/2.0"
4
14
5
15
func GetApiBaseURL () string {
@@ -23,17 +33,17 @@ type user interface {
23
33
}
24
34
25
35
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 )
37
47
}
38
48
39
49
type repository interface {
@@ -95,9 +105,15 @@ type teams interface {
95
105
Projects (teamname string ) (interface {}, error )
96
106
}
97
107
108
+ type ListOptions struct {
109
+ Page uint64 `json:"page"`
110
+ PageLen uint64 `json:"pagelen"`
111
+ }
112
+
98
113
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]
101
117
}
102
118
103
119
type RepositoryOptions struct {
@@ -114,22 +130,6 @@ type RepositoryOptions struct {
114
130
Project string `json:"project"`
115
131
}
116
132
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
-
133
133
type CommitsOptions struct {
134
134
Owner string `json:"owner"`
135
135
RepoSlug string `json:"repo_slug"`
@@ -141,12 +141,11 @@ type CommitsOptions struct {
141
141
}
142
142
143
143
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"`
148
148
Description string `json:"description"`
149
-
150
149
}
151
150
152
151
type BranchRestrictionsOptions struct {
@@ -201,8 +200,78 @@ type RepositoryPipelineKeyPairOptions struct {
201
200
}
202
201
203
202
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
+ }
0 commit comments