@@ -2,6 +2,7 @@ package bitbucket
22
33import (
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
221243func (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+
541578func decodeRepositoryTags (tagResponse interface {}) (* RepositoryTags , error ) {
542579
543580 var tagResponseMap map [string ]interface {}
0 commit comments