Skip to content

Commit 388db12

Browse files
csandanovktrysmt
authored andcommitted
Add repository list tags method (#77)
1 parent e8d45e2 commit 388db12

File tree

2 files changed

+114
-3
lines changed

2 files changed

+114
-3
lines changed

bitbucket.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ type RepositoryBranchOptions struct {
141141
MaxDepth int `json:"max_depth"`
142142
}
143143

144+
type RepositoryTagOptions struct {
145+
Owner string `json:"owner"`
146+
RepoSlug string `json:"repo_slug"`
147+
Query string `json:"q"`
148+
Sort string `json:"sort"`
149+
PageNum int `json:"page"`
150+
Pagelen int `json:"pagelen"`
151+
MaxDepth int `json:"max_depth"`
152+
}
153+
144154
type PullRequestsOptions struct {
145155
ID string `json:"id"`
146156
CommentID string `json:"comment_id"`
@@ -237,8 +247,8 @@ type DownloadsOptions struct {
237247
}
238248

239249
type PageRes struct {
240-
Page int32 `json:"page"`
241-
PageLen int32 `json:"pagelen"`
250+
Page int32 `json:"page"`
251+
PageLen int32 `json:"pagelen"`
242252
MaxDepth int32 `json:"max_depth"`
243-
Size int32 `json:"size"`
253+
Size int32 `json:"size"`
244254
}

repository.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,23 @@ type RepositoryBranch struct {
6262
Heads []map[string]interface{}
6363
}
6464

65+
type RepositoryTags struct {
66+
Page int
67+
Pagelen int
68+
MaxDepth int
69+
Size int
70+
Next string
71+
Tags []RepositoryTag
72+
}
73+
74+
type RepositoryTag struct {
75+
Type string
76+
Name string
77+
Links map[string]interface{}
78+
Target map[string]interface{}
79+
Heads []map[string]interface{}
80+
}
81+
6582
type Pipeline struct {
6683
Type string
6784
Enabled bool
@@ -160,6 +177,38 @@ func (r *Repository) ListBranches(rbo *RepositoryBranchOptions) (*RepositoryBran
160177
return decodeRepositoryBranches(response)
161178
}
162179

180+
func (r *Repository) ListTags(rbo *RepositoryTagOptions) (*RepositoryTags, error) {
181+
182+
params := url.Values{}
183+
if rbo.Query != "" {
184+
params.Add("q", rbo.Query)
185+
}
186+
187+
if rbo.Sort != "" {
188+
params.Add("sort", rbo.Sort)
189+
}
190+
191+
if rbo.PageNum > 0 {
192+
params.Add("page", strconv.Itoa(rbo.PageNum))
193+
}
194+
195+
if rbo.Pagelen > 0 {
196+
params.Add("pagelen", strconv.Itoa(rbo.Pagelen))
197+
}
198+
199+
if rbo.MaxDepth > 0 {
200+
params.Add("max_depth", strconv.Itoa(rbo.MaxDepth))
201+
}
202+
203+
urlStr := r.c.requestUrl("/repositories/%s/%s/refs/tags?%s", rbo.Owner, rbo.RepoSlug, params.Encode())
204+
response, err := r.c.executeRaw("GET", urlStr, "")
205+
if err != nil {
206+
return nil, err
207+
}
208+
209+
return decodeRepositoryTags(response)
210+
}
211+
163212
func (r *Repository) Delete(ro *RepositoryOptions) (interface{}, error) {
164213
urlStr := r.c.requestUrl("/repositories/%s/%s", ro.Owner, ro.RepoSlug)
165214
return r.c.execute("DELETE", urlStr, "")
@@ -379,6 +428,58 @@ func decodeRepositoryBranches(branchResponse interface{}) (*RepositoryBranches,
379428
return &repositoryBranches, nil
380429
}
381430

431+
func decodeRepositoryTags(tagResponse interface{}) (*RepositoryTags, error) {
432+
433+
var tagResponseMap map[string]interface{}
434+
err := json.Unmarshal(tagResponse.([]byte), &tagResponseMap)
435+
if err != nil {
436+
return nil, err
437+
}
438+
439+
tagArray := tagResponseMap["values"].([]interface{})
440+
var tags []RepositoryTag
441+
for _, tagEntry := range tagArray {
442+
var tag RepositoryTag
443+
err = mapstructure.Decode(tagEntry, &tag)
444+
if err == nil {
445+
tags = append(tags, tag)
446+
}
447+
}
448+
449+
page, ok := tagResponseMap["page"].(float64)
450+
if !ok {
451+
page = 0
452+
}
453+
454+
pagelen, ok := tagResponseMap["pagelen"].(float64)
455+
if !ok {
456+
pagelen = 0
457+
}
458+
max_depth, ok := tagResponseMap["max_depth"].(float64)
459+
if !ok {
460+
max_depth = 0
461+
}
462+
size, ok := tagResponseMap["size"].(float64)
463+
if !ok {
464+
size = 0
465+
}
466+
467+
next, ok := tagResponseMap["next"].(string)
468+
if !ok {
469+
next = ""
470+
}
471+
472+
repositoryTags := RepositoryTags{
473+
Page: int(page),
474+
Pagelen: int(pagelen),
475+
MaxDepth: int(max_depth),
476+
Size: int(size),
477+
Next: next,
478+
Tags: tags,
479+
}
480+
return &repositoryTags, nil
481+
}
482+
382483
func decodePipelineRepository(repoResponse interface{}) (*Pipeline, error) {
383484
repoMap := repoResponse.(map[string]interface{})
384485

0 commit comments

Comments
 (0)