@@ -2,8 +2,10 @@ package bitbucket
2
2
3
3
import (
4
4
"encoding/json"
5
+ "net/url"
5
6
"os"
6
7
"path"
8
+ "strconv"
7
9
8
10
"github.com/k0kubun/pp"
9
11
"github.com/mitchellh/mapstructure"
@@ -41,6 +43,24 @@ type RepositoryBlob struct {
41
43
Content []byte
42
44
}
43
45
46
+ type RepositoryBranches struct {
47
+ Page int
48
+ Pagelen int
49
+ Size int
50
+ Next string
51
+ Branches []RepositoryBranch
52
+ }
53
+
54
+ type RepositoryBranch struct {
55
+ Type string
56
+ Name string
57
+ Default_Merge_Strategy string
58
+ Merge_Strategies []string
59
+ Links map [string ]interface {}
60
+ Target map [string ]interface {}
61
+ Heads []map [string ]interface {}
62
+ }
63
+
44
64
type Pipeline struct {
45
65
Type string
46
66
Enabled bool
@@ -107,6 +127,34 @@ func (r *Repository) GetFileBlob(ro *RepositoryBlobOptions) (*RepositoryBlob, er
107
127
return & blob , nil
108
128
}
109
129
130
+ func (r * Repository ) ListBranches (rbo * RepositoryBranchOptions ) (* RepositoryBranches , error ) {
131
+
132
+ params := url.Values {}
133
+ if rbo .Query != "" {
134
+ params .Add ("q" , rbo .Query )
135
+ }
136
+
137
+ if rbo .Sort != "" {
138
+ params .Add ("sort" , rbo .Sort )
139
+ }
140
+
141
+ if rbo .PageNum > 0 {
142
+ params .Add ("page" , strconv .Itoa (rbo .PageNum ))
143
+ }
144
+
145
+ if rbo .Pagelen > 0 {
146
+ params .Add ("pagelen" , strconv .Itoa (rbo .Pagelen ))
147
+ }
148
+
149
+ urlStr := r .c .requestUrl ("/repositories/%s/%s/refs/branches?%s" , rbo .Owner , rbo .RepoSlug , params .Encode ())
150
+ response , err := r .c .executeRaw ("GET" , urlStr , "" )
151
+ if err != nil {
152
+ return nil , err
153
+ }
154
+
155
+ return decodeRepositoryBranches (response )
156
+ }
157
+
110
158
func (r * Repository ) Delete (ro * RepositoryOptions ) (interface {}, error ) {
111
159
urlStr := r .c .requestUrl ("/repositories/%s/%s" , ro .Owner , ro .RepoSlug )
112
160
return r .c .execute ("DELETE" , urlStr , "" )
@@ -274,6 +322,53 @@ func decodeRepositoryFiles(repoResponse interface{}) ([]RepositoryFile, error) {
274
322
return * repositoryFiles , nil
275
323
}
276
324
325
+ func decodeRepositoryBranches (branchResponse interface {}) (* RepositoryBranches , error ) {
326
+
327
+ var branchResponseMap map [string ]interface {}
328
+ err := json .Unmarshal (branchResponse .([]byte ), & branchResponseMap )
329
+ if err != nil {
330
+ return nil , err
331
+ }
332
+
333
+ branchArray := branchResponseMap ["values" ].([]interface {})
334
+ var branches []RepositoryBranch
335
+ for _ , branchEntry := range branchArray {
336
+ var branch RepositoryBranch
337
+ err = mapstructure .Decode (branchEntry , & branch )
338
+ if err == nil {
339
+ branches = append (branches , branch )
340
+ }
341
+ }
342
+
343
+ page , ok := branchResponseMap ["page" ].(float64 )
344
+ if ! ok {
345
+ page = 0
346
+ }
347
+
348
+ pagelen , ok := branchResponseMap ["pagelen" ].(float64 )
349
+ if ! ok {
350
+ pagelen = 0
351
+ }
352
+ size , ok := branchResponseMap ["size" ].(float64 )
353
+ if ! ok {
354
+ size = 0
355
+ }
356
+
357
+ next , ok := branchResponseMap ["next" ].(string )
358
+ if ! ok {
359
+ next = ""
360
+ }
361
+
362
+ repositoryBranches := RepositoryBranches {
363
+ Page : int (page ),
364
+ Pagelen : int (pagelen ),
365
+ Size : int (size ),
366
+ Next : next ,
367
+ Branches : branches ,
368
+ }
369
+ return & repositoryBranches , nil
370
+ }
371
+
277
372
func decodePipelineRepository (repoResponse interface {}) (* Pipeline , error ) {
278
373
repoMap := repoResponse .(map [string ]interface {})
279
374
0 commit comments