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
-
13
3
var apiBaseURL = "https://api.bitbucket.org/2.0"
14
4
15
5
func GetApiBaseURL () string {
@@ -33,17 +23,17 @@ type user interface {
33
23
}
34
24
35
25
type pullrequests interface {
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 )
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 )
47
37
}
48
38
49
39
type repository interface {
@@ -105,15 +95,9 @@ type teams interface {
105
95
Projects (teamname string ) (interface {}, error )
106
96
}
107
97
108
- type ListOptions struct {
109
- Page uint64 `json:"page"`
110
- PageLen uint64 `json:"pagelen"`
111
- }
112
-
113
98
type RepositoriesOptions struct {
114
- ListOptions * ListOptions `json:"list_options"`
115
- Owner string `json:"owner"`
116
- Role string `json:"role"` // role=[owner|admin|contributor|member]
99
+ Owner string `json:"owner"`
100
+ Role string `json:"role"` // role=[owner|admin|contributor|member]
117
101
}
118
102
119
103
type RepositoryOptions struct {
@@ -130,6 +114,22 @@ type RepositoryOptions struct {
130
114
Project string `json:"project"`
131
115
}
132
116
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,11 +141,12 @@ 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
+
149
150
}
150
151
151
152
type BranchRestrictionsOptions struct {
@@ -200,78 +201,8 @@ type RepositoryPipelineKeyPairOptions struct {
200
201
}
201
202
202
203
type DownloadsOptions struct {
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
- }
204
+ Owner string `json:"owner"`
205
+ RepoSlug string `json:"repo_slug"`
206
+ FilePath string `json:"filepath"`
207
+ FileName string `json:"filename"`
208
+ }
0 commit comments