Skip to content

Commit f3ac86c

Browse files
authored
Create RepositoryRefsOptions and ListRefs method with helper funds (#155)
* Create Rep Ref Options type * Create list refs functions This commit creates a ListRefs method of the Repository type. Specifically, it creates a RepositoryRefs struct type that is returned from the decoodeRepositoryRefs function that is a helper function for decoding the reponse from the refs GET API Bitbucket endpoint. * Correct Options type and add documentation * Create test for ListRefs This commit creates a test case for ListRefs based on setting up the test repo with a new branch and then listing the refs and making sure properly created the test repo. So, this test case technically also tests a portion of the functionality of the CreateBranch function.
1 parent 23fc175 commit f3ac86c

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{}
@@ -911,6 +958,58 @@ func decodeRepositoryFiles(repoResponse interface{}) ([]RepositoryFile, error) {
911958
return *repositoryFiles, nil
912959
}
913960

961+
func decodeRepositoryRefs(refResponseStr string) (*RepositoryRefs, error) {
962+
963+
var refResponseMap map[string]interface{}
964+
err := json.Unmarshal([]byte(refResponseStr), &refResponseMap)
965+
if err != nil {
966+
return nil, err
967+
}
968+
969+
refArray := refResponseMap["values"].([]interface{})
970+
var refs []map[string]interface{}
971+
for _, refEntry := range refArray {
972+
var ref map[string]interface{}
973+
err = mapstructure.Decode(refEntry, &ref)
974+
if err == nil {
975+
refs = append(refs, ref)
976+
}
977+
}
978+
979+
page, ok := refResponseMap["page"].(float64)
980+
if !ok {
981+
page = 0
982+
}
983+
984+
pagelen, ok := refResponseMap["pagelen"].(float64)
985+
if !ok {
986+
pagelen = 0
987+
}
988+
max_depth, ok := refResponseMap["max_depth"].(float64)
989+
if !ok {
990+
max_depth = 0
991+
}
992+
size, ok := refResponseMap["size"].(float64)
993+
if !ok {
994+
size = 0
995+
}
996+
997+
next, ok := refResponseMap["next"].(string)
998+
if !ok {
999+
next = ""
1000+
}
1001+
1002+
repositoryBranches := RepositoryRefs{
1003+
Page: int(page),
1004+
Pagelen: int(pagelen),
1005+
MaxDepth: int(max_depth),
1006+
Size: int(size),
1007+
Next: next,
1008+
Refs: refs,
1009+
}
1010+
return &repositoryBranches, nil
1011+
}
1012+
9141013
func decodeRepositoryBranches(branchResponseStr string) (*RepositoryBranches, error) {
9151014

9161015
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)