Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
vendor
.idea
.env
17 changes: 12 additions & 5 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/url"
"path"
Expand Down Expand Up @@ -440,11 +441,17 @@ func (r *Repository) ListRefs(rbo *RepositoryRefOptions) (*RepositoryRefs, error
return decodeRepositoryRefs(bodyString)
}

// ListBranches gets all branches in the Bitbucket repository and returns them as a RepositoryBranches.
// You can pass query parameter to filter branches by name.
//
// Bitbucket API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-refs/#api-repositories-workspace-repo-slug-refs-get
func (r *Repository) ListBranches(rbo *RepositoryBranchOptions) (*RepositoryBranches, error) {

params := url.Values{}

if rbo.Query != "" {
params.Add("q", rbo.Query)
// https://developer.atlassian.com/cloud/bitbucket/rest/intro/#operators
query := fmt.Sprintf("name ~ \"%s\"", rbo.Query)
params.Set("q", query)
}

if rbo.Sort != "" {
Expand All @@ -468,12 +475,12 @@ func (r *Repository) ListBranches(rbo *RepositoryBranchOptions) (*RepositoryBran
if err != nil {
return nil, err
}
bodyBytes, err := ioutil.ReadAll(response)
bodyBytes, err := io.ReadAll(response)
if err != nil {
return nil, err
}
bodyString := string(bodyBytes)
return decodeRepositoryBranches(bodyString)

return decodeRepositoryBranches(string(bodyBytes))
}

func (r *Repository) GetBranch(rbo *RepositoryBranchOptions) (*RepositoryBranch, error) {
Expand Down
22 changes: 22 additions & 0 deletions tests/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,3 +737,25 @@ func TestSetRepositoryUserPermissions(t *testing.T) {
}

}

func TestListBranches(t *testing.T) {
client := setup(t)

opts := &bitbucket.RepositoryBranchOptions{
Owner: owner,
RepoSlug: repo,
Pagelen: 10,
Query: "develop",
}

response, err := client.Repositories.Repository.ListBranches(opts)
if err != nil {
t.Fatalf("ListBranches() returned an error: %v", err)
}
if response == nil {
t.Fatal("Cannot get list branches")
}
if response.Size == 0 {
t.Fatalf("Expected to find at least one branch for query '%s', but found none", opts.Query)
}
}