Skip to content

Commit

Permalink
fix ListBranches, add GetBranch (#93)
Browse files Browse the repository at this point in the history
* fix ListBranches, add GetBranch

* update repo branch options query

Co-authored-by: Kotaro Yoshimatsu <[email protected]>

Co-authored-by: Kotaro Yoshimatsu <[email protected]>
  • Loading branch information
KshamaG and ktrysmt authored Jul 30, 2020
1 parent c020169 commit fdf03c4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
15 changes: 8 additions & 7 deletions bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,14 @@ type RepositoryBlobOptions struct {
}

type RepositoryBranchOptions struct {
Owner string `json:"owner"`
RepoSlug string `json:"repo_slug"`
Query string `json:"q"`
Sort string `json:"sort"`
PageNum int `json:"page"`
Pagelen int `json:"pagelen"`
MaxDepth int `json:"max_depth"`
Owner string `json:"owner"`
RepoSlug string `json:"repo_slug"`
Query string `json:"query"`
Sort string `json:"sort"`
PageNum int `json:"page"`
Pagelen int `json:"pagelen"`
MaxDepth int `json:"max_depth"`
BranchName string `json:"branch_name"`
}

type RepositoryTagOptions struct {
Expand Down
43 changes: 40 additions & 3 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bitbucket

import (
"encoding/json"
"errors"
"io/ioutil"
"net/url"
"os"
Expand Down Expand Up @@ -214,8 +215,29 @@ func (r *Repository) ListBranches(rbo *RepositoryBranchOptions) (*RepositoryBran
if err != nil {
return nil, err
}
bodyBytes, err := ioutil.ReadAll(response)
if err != nil {
return nil, err
}
bodyString := string(bodyBytes)
return decodeRepositoryBranches(bodyString)
}

return decodeRepositoryBranches(response)
func (r *Repository) GetBranch(rbo *RepositoryBranchOptions) (*RepositoryBranch, error) {
if rbo.BranchName == "" {
return nil, errors.New("Error: Branch Name is empty")
}
urlStr := r.c.requestUrl("/repositories/%s/%s/refs/branches/%s", rbo.Owner, rbo.RepoSlug, rbo.BranchName)
response, err := r.c.executeRaw("GET", urlStr, "")
if err != nil {
return nil, err
}
bodyBytes, err := ioutil.ReadAll(response)
if err != nil {
return nil, err
}
bodyString := string(bodyBytes)
return decodeRepositoryBranch(bodyString)
}

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

func decodeRepositoryBranches(branchResponse interface{}) (*RepositoryBranches, error) {
func decodeRepositoryBranches(branchResponseStr string) (*RepositoryBranches, error) {

var branchResponseMap map[string]interface{}
err := json.Unmarshal(branchResponse.([]byte), &branchResponseMap)
err := json.Unmarshal([]byte(branchResponseStr), &branchResponseMap)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -538,6 +560,21 @@ func decodeRepositoryBranches(branchResponse interface{}) (*RepositoryBranches,
return &repositoryBranches, nil
}

func decodeRepositoryBranch(branchResponseStr string) (*RepositoryBranch, error) {

var branchResponseMap map[string]interface{}
err := json.Unmarshal([]byte(branchResponseStr), &branchResponseMap)
if err != nil {
return nil, err
}
var repositoryBranch RepositoryBranch
err = mapstructure.Decode(branchResponseMap, &repositoryBranch)
if err != nil {
return nil, err
}
return &repositoryBranch, nil
}

func decodeRepositoryTags(tagResponse interface{}) (*RepositoryTags, error) {

var tagResponseMap map[string]interface{}
Expand Down

0 comments on commit fdf03c4

Please sign in to comment.