Skip to content

Commit 7aba5c0

Browse files
committed
Merge branch 'ddansby/153-create-refs-options-and-methods' of https://github.com/DataDavD/go-bitbucket into ddansby/ISSUE-154-add-delete-branch-and-tag-methods
2 parents a8ef079 + 6dd40ec commit 7aba5c0

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed

bitbucket.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,21 @@ type RepositoryBlobWriteOptions struct {
199199
Branch string `json:"branch"`
200200
}
201201

202+
// RepositoryRefOptions represents the options for describing a repository's refs (i.e.
203+
// tags and branches). The field BranchFlg is a boolean that is indicates whether a specific
204+
// RepositoryRefOptions instance is meant for Branch specific set of api methods.
205+
type RepositoryRefOptions struct {
206+
Owner string `json:"owner"`
207+
RepoSlug string `json:"repo_slug"`
208+
Query string `json:"query"`
209+
Sort string `json:"sort"`
210+
PageNum int `json:"page"`
211+
Pagelen int `json:"pagelen"`
212+
MaxDepth int `json:"max_depth"`
213+
Name string `json:"name"`
214+
BranchFlg bool
215+
}
216+
202217
type RepositoryBranchOptions struct {
203218
Owner string `json:"owner"`
204219
RepoSlug string `json:"repo_slug"`

repository.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ type RepositoryBlob struct {
4949
Content []byte
5050
}
5151

52+
type RepositoryRefs struct {
53+
Page int
54+
Pagelen int
55+
MaxDepth int
56+
Size int
57+
Next string
58+
Refs []map[string]interface{}
59+
}
60+
5261
type RepositoryBranches struct {
5362
Page int
5463
Pagelen int
@@ -278,6 +287,44 @@ func (r *Repository) WriteFileBlob(ro *RepositoryBlobWriteOptions) error {
278287
return err
279288
}
280289

290+
// ListRefs gets all refs in the Bitbucket repository and returns them as a RepositoryRefs.
291+
// It takes in a RepositoryRefOptions instance as its only parameter.
292+
func (r *Repository) ListRefs(rbo *RepositoryRefOptions) (*RepositoryRefs, error) {
293+
294+
params := url.Values{}
295+
if rbo.Query != "" {
296+
params.Add("q", rbo.Query)
297+
}
298+
299+
if rbo.Sort != "" {
300+
params.Add("sort", rbo.Sort)
301+
}
302+
303+
if rbo.PageNum > 0 {
304+
params.Add("page", strconv.Itoa(rbo.PageNum))
305+
}
306+
307+
if rbo.Pagelen > 0 {
308+
params.Add("pagelen", strconv.Itoa(rbo.Pagelen))
309+
}
310+
311+
if rbo.MaxDepth > 0 {
312+
params.Add("max_depth", strconv.Itoa(rbo.MaxDepth))
313+
}
314+
315+
urlStr := r.c.requestUrl("/repositories/%s/%s/refs?%s", rbo.Owner, rbo.RepoSlug, params.Encode())
316+
response, err := r.c.executeRaw("GET", urlStr, "")
317+
if err != nil {
318+
return nil, err
319+
}
320+
bodyBytes, err := ioutil.ReadAll(response)
321+
if err != nil {
322+
return nil, err
323+
}
324+
bodyString := string(bodyBytes)
325+
return decodeRepositoryRefs(bodyString)
326+
}
327+
281328
func (r *Repository) ListBranches(rbo *RepositoryBranchOptions) (*RepositoryBranches, error) {
282329

283330
params := url.Values{}
@@ -923,6 +970,58 @@ func decodeRepositoryFiles(repoResponse interface{}) ([]RepositoryFile, error) {
923970
return *repositoryFiles, nil
924971
}
925972

973+
func decodeRepositoryRefs(refResponseStr string) (*RepositoryRefs, error) {
974+
975+
var refResponseMap map[string]interface{}
976+
err := json.Unmarshal([]byte(refResponseStr), &refResponseMap)
977+
if err != nil {
978+
return nil, err
979+
}
980+
981+
refArray := refResponseMap["values"].([]interface{})
982+
var refs []map[string]interface{}
983+
for _, refEntry := range refArray {
984+
var ref map[string]interface{}
985+
err = mapstructure.Decode(refEntry, &ref)
986+
if err == nil {
987+
refs = append(refs, ref)
988+
}
989+
}
990+
991+
page, ok := refResponseMap["page"].(float64)
992+
if !ok {
993+
page = 0
994+
}
995+
996+
pagelen, ok := refResponseMap["pagelen"].(float64)
997+
if !ok {
998+
pagelen = 0
999+
}
1000+
max_depth, ok := refResponseMap["max_depth"].(float64)
1001+
if !ok {
1002+
max_depth = 0
1003+
}
1004+
size, ok := refResponseMap["size"].(float64)
1005+
if !ok {
1006+
size = 0
1007+
}
1008+
1009+
next, ok := refResponseMap["next"].(string)
1010+
if !ok {
1011+
next = ""
1012+
}
1013+
1014+
repositoryBranches := RepositoryRefs{
1015+
Page: int(page),
1016+
Pagelen: int(pagelen),
1017+
MaxDepth: int(max_depth),
1018+
Size: int(size),
1019+
Next: next,
1020+
Refs: refs,
1021+
}
1022+
return &repositoryBranches, nil
1023+
}
1024+
9261025
func decodeRepositoryBranches(branchResponseStr string) (*RepositoryBranches, error) {
9271026

9281027
var branchResponseMap map[string]interface{}

tests/repository_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,74 @@ func TestDeleteRepositoryPipelineVariables(t *testing.T) {
129129
t.Error(err)
130130
}
131131
}
132+
133+
func TestGetRepositoryRefs(t *testing.T) {
134+
135+
user := os.Getenv("BITBUCKET_TEST_USERNAME")
136+
pass := os.Getenv("BITBUCKET_TEST_PASSWORD")
137+
owner := os.Getenv("BITBUCKET_TEST_OWNER")
138+
repo := os.Getenv("BITBUCKET_TEST_REPOSLUG")
139+
140+
if user == "" {
141+
t.Error("BITBUCKET_TEST_USERNAME is empty.")
142+
}
143+
if pass == "" {
144+
t.Error("BITBUCKET_TEST_PASSWORD is empty.")
145+
}
146+
if owner == "" {
147+
t.Error("BITBUCKET_TEST_OWNER is empty.")
148+
}
149+
if repo == "" {
150+
t.Error("BITBUCKET_TEST_REPOSLUG is empty.")
151+
}
152+
153+
c := bitbucket.NewBasicAuth(user, pass)
154+
155+
opt := &bitbucket.RepositoryBranchCreationOptions{
156+
Owner: owner,
157+
RepoSlug: repo,
158+
Name: "TestGetRepoRefsBranch",
159+
Target: bitbucket.RepositoryBranchTarget{Hash: "master"},
160+
}
161+
162+
_, err := c.Repositories.Repository.CreateBranch(opt)
163+
if err != nil {
164+
t.Error("Could not create new branch", err)
165+
}
166+
167+
refOpts := &bitbucket.RepositoryRefOptions{
168+
Owner: owner,
169+
RepoSlug: repo,
170+
}
171+
172+
resRefs, err := c.Repositories.Repository.ListRefs(refOpts)
173+
if err != nil {
174+
t.Error("The refs is not found.")
175+
}
176+
177+
expected := struct {
178+
n string
179+
t string
180+
}{}
181+
182+
for _, ref := range resRefs.Refs {
183+
for k, v := range ref {
184+
// kCopy := k
185+
vCopy := v
186+
if val, ok := vCopy.(string); ok {
187+
if k == "name" && val == "TestGetRepoRefsBranch" {
188+
expected.n = val
189+
}
190+
}
191+
if val, ok := vCopy.(string); ok {
192+
if k == "type" && val == "branch" {
193+
expected.t = val
194+
}
195+
}
196+
}
197+
}
198+
199+
if !(expected.n == "TestGetRepoRefsBranch" && expected.t == "branch") {
200+
t.Error("Could not list refs/branch that was created in test setup")
201+
}
202+
}

0 commit comments

Comments
 (0)