@@ -2,8 +2,10 @@ package bitbucket
22
33import (
44 "encoding/json"
5+ "net/url"
56 "os"
67 "path"
8+ "strconv"
79
810 "github.com/k0kubun/pp"
911 "github.com/mitchellh/mapstructure"
@@ -41,6 +43,24 @@ type RepositoryBlob struct {
4143 Content []byte
4244}
4345
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+
4464type Pipeline struct {
4565 Type string
4666 Enabled bool
@@ -107,6 +127,34 @@ func (r *Repository) GetFileBlob(ro *RepositoryBlobOptions) (*RepositoryBlob, er
107127 return & blob , nil
108128}
109129
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+
110158func (r * Repository ) Delete (ro * RepositoryOptions ) (interface {}, error ) {
111159 urlStr := r .c .requestUrl ("/repositories/%s/%s" , ro .Owner , ro .RepoSlug )
112160 return r .c .execute ("DELETE" , urlStr , "" )
@@ -274,6 +322,53 @@ func decodeRepositoryFiles(repoResponse interface{}) ([]RepositoryFile, error) {
274322 return * repositoryFiles , nil
275323}
276324
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+
277372func decodePipelineRepository (repoResponse interface {}) (* Pipeline , error ) {
278373 repoMap := repoResponse .(map [string ]interface {})
279374
0 commit comments