Skip to content

Commit df6d5a4

Browse files
mfb2ktrysmt
authored andcommitted
Add branch listing (#66)
* Add branch listing This commit adds the ability to fetch a list of branches from Bitbucket via pagination. * Convert branches pointer to proper object This commit converts a pointer to branches to an actual object reference in order to keep the response immutable. * Add pagelen query parameter Adds ability to specify number of records to be returned on each page.
1 parent c69b646 commit df6d5a4

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

bitbucket.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type repository interface {
4747
AddPipelineKeyPair(opt RepositoryPipelineKeyPairOptions) (*PipelineKeyPair, error)
4848
ListFiles(opt RepositoryFilesOptions) (*[]RepositoryFile, error)
4949
GetFileBlob(opt RepositoryBlobOptions) (*RepositoryBlob, error)
50+
ListBranches(opt RepositoryBranchOptions) (*RepositoryBranches, error)
5051
}
5152

5253
type repositories interface {
@@ -129,6 +130,15 @@ type RepositoryBlobOptions struct {
129130
Path string `json:"path"`
130131
}
131132

133+
type RepositoryBranchOptions struct {
134+
Owner string `json:"owner"`
135+
RepoSlug string `json:"repo_slug"`
136+
Query string `json:"q"`
137+
Sort string `json:"sort"`
138+
PageNum int `json:"page"`
139+
Pagelen int `json:"pagelen"`
140+
}
141+
132142
type PullRequestsOptions struct {
133143
ID string `json:"id"`
134144
CommentID string `json:"comment_id"`

repository.go

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

33
import (
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+
4464
type 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+
110158
func (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+
277372
func decodePipelineRepository(repoResponse interface{}) (*Pipeline, error) {
278373
repoMap := repoResponse.(map[string]interface{})
279374

0 commit comments

Comments
 (0)