Skip to content

Commit 3ec404e

Browse files
authored
Fix list branch query (#316)
* fix: uses correct approach for query params * refactor: slight corrections * feat: add tests for ListBranches * docs: updates method description * feat: improves test
1 parent 49541fd commit 3ec404e

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
vendor
2+
.idea
3+
.env

repository.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"io"
78
"io/ioutil"
89
"net/url"
910
"path"
@@ -440,11 +441,17 @@ func (r *Repository) ListRefs(rbo *RepositoryRefOptions) (*RepositoryRefs, error
440441
return decodeRepositoryRefs(bodyString)
441442
}
442443

444+
// ListBranches gets all branches in the Bitbucket repository and returns them as a RepositoryBranches.
445+
// You can pass query parameter to filter branches by name.
446+
//
447+
// Bitbucket API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-refs/#api-repositories-workspace-repo-slug-refs-get
443448
func (r *Repository) ListBranches(rbo *RepositoryBranchOptions) (*RepositoryBranches, error) {
444-
445449
params := url.Values{}
450+
446451
if rbo.Query != "" {
447-
params.Add("q", rbo.Query)
452+
// https://developer.atlassian.com/cloud/bitbucket/rest/intro/#operators
453+
query := fmt.Sprintf("name ~ \"%s\"", rbo.Query)
454+
params.Set("q", query)
448455
}
449456

450457
if rbo.Sort != "" {
@@ -468,12 +475,12 @@ func (r *Repository) ListBranches(rbo *RepositoryBranchOptions) (*RepositoryBran
468475
if err != nil {
469476
return nil, err
470477
}
471-
bodyBytes, err := ioutil.ReadAll(response)
478+
bodyBytes, err := io.ReadAll(response)
472479
if err != nil {
473480
return nil, err
474481
}
475-
bodyString := string(bodyBytes)
476-
return decodeRepositoryBranches(bodyString)
482+
483+
return decodeRepositoryBranches(string(bodyBytes))
477484
}
478485

479486
func (r *Repository) GetBranch(rbo *RepositoryBranchOptions) (*RepositoryBranch, error) {

tests/repository_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,3 +737,25 @@ func TestSetRepositoryUserPermissions(t *testing.T) {
737737
}
738738

739739
}
740+
741+
func TestListBranches(t *testing.T) {
742+
client := setup(t)
743+
744+
opts := &bitbucket.RepositoryBranchOptions{
745+
Owner: owner,
746+
RepoSlug: repo,
747+
Pagelen: 10,
748+
Query: "develop",
749+
}
750+
751+
response, err := client.Repositories.Repository.ListBranches(opts)
752+
if err != nil {
753+
t.Fatalf("ListBranches() returned an error: %v", err)
754+
}
755+
if response == nil {
756+
t.Fatal("Cannot get list branches")
757+
}
758+
if response.Size == 0 {
759+
t.Fatalf("Expected to find at least one branch for query '%s', but found none", opts.Query)
760+
}
761+
}

0 commit comments

Comments
 (0)