Skip to content

Commit fdf03c4

Browse files
KshamaGktrysmt
andauthored
fix ListBranches, add GetBranch (#93)
* fix ListBranches, add GetBranch * update repo branch options query Co-authored-by: Kotaro Yoshimatsu <[email protected]> Co-authored-by: Kotaro Yoshimatsu <[email protected]>
1 parent c020169 commit fdf03c4

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

bitbucket.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,14 @@ type RepositoryBlobOptions struct {
140140
}
141141

142142
type RepositoryBranchOptions struct {
143-
Owner string `json:"owner"`
144-
RepoSlug string `json:"repo_slug"`
145-
Query string `json:"q"`
146-
Sort string `json:"sort"`
147-
PageNum int `json:"page"`
148-
Pagelen int `json:"pagelen"`
149-
MaxDepth int `json:"max_depth"`
143+
Owner string `json:"owner"`
144+
RepoSlug string `json:"repo_slug"`
145+
Query string `json:"query"`
146+
Sort string `json:"sort"`
147+
PageNum int `json:"page"`
148+
Pagelen int `json:"pagelen"`
149+
MaxDepth int `json:"max_depth"`
150+
BranchName string `json:"branch_name"`
150151
}
151152

152153
type RepositoryTagOptions struct {

repository.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bitbucket
22

33
import (
44
"encoding/json"
5+
"errors"
56
"io/ioutil"
67
"net/url"
78
"os"
@@ -214,8 +215,29 @@ func (r *Repository) ListBranches(rbo *RepositoryBranchOptions) (*RepositoryBran
214215
if err != nil {
215216
return nil, err
216217
}
218+
bodyBytes, err := ioutil.ReadAll(response)
219+
if err != nil {
220+
return nil, err
221+
}
222+
bodyString := string(bodyBytes)
223+
return decodeRepositoryBranches(bodyString)
224+
}
217225

218-
return decodeRepositoryBranches(response)
226+
func (r *Repository) GetBranch(rbo *RepositoryBranchOptions) (*RepositoryBranch, error) {
227+
if rbo.BranchName == "" {
228+
return nil, errors.New("Error: Branch Name is empty")
229+
}
230+
urlStr := r.c.requestUrl("/repositories/%s/%s/refs/branches/%s", rbo.Owner, rbo.RepoSlug, rbo.BranchName)
231+
response, err := r.c.executeRaw("GET", urlStr, "")
232+
if err != nil {
233+
return nil, err
234+
}
235+
bodyBytes, err := ioutil.ReadAll(response)
236+
if err != nil {
237+
return nil, err
238+
}
239+
bodyString := string(bodyBytes)
240+
return decodeRepositoryBranch(bodyString)
219241
}
220242

221243
func (r *Repository) ListTags(rbo *RepositoryTagOptions) (*RepositoryTags, error) {
@@ -486,10 +508,10 @@ func decodeRepositoryFiles(repoResponse interface{}) ([]RepositoryFile, error) {
486508
return *repositoryFiles, nil
487509
}
488510

489-
func decodeRepositoryBranches(branchResponse interface{}) (*RepositoryBranches, error) {
511+
func decodeRepositoryBranches(branchResponseStr string) (*RepositoryBranches, error) {
490512

491513
var branchResponseMap map[string]interface{}
492-
err := json.Unmarshal(branchResponse.([]byte), &branchResponseMap)
514+
err := json.Unmarshal([]byte(branchResponseStr), &branchResponseMap)
493515
if err != nil {
494516
return nil, err
495517
}
@@ -538,6 +560,21 @@ func decodeRepositoryBranches(branchResponse interface{}) (*RepositoryBranches,
538560
return &repositoryBranches, nil
539561
}
540562

563+
func decodeRepositoryBranch(branchResponseStr string) (*RepositoryBranch, error) {
564+
565+
var branchResponseMap map[string]interface{}
566+
err := json.Unmarshal([]byte(branchResponseStr), &branchResponseMap)
567+
if err != nil {
568+
return nil, err
569+
}
570+
var repositoryBranch RepositoryBranch
571+
err = mapstructure.Decode(branchResponseMap, &repositoryBranch)
572+
if err != nil {
573+
return nil, err
574+
}
575+
return &repositoryBranch, nil
576+
}
577+
541578
func decodeRepositoryTags(tagResponse interface{}) (*RepositoryTags, error) {
542579

543580
var tagResponseMap map[string]interface{}

0 commit comments

Comments
 (0)