From 1a5965fba362c052764bd74515d558c53c188816 Mon Sep 17 00:00:00 2001 From: Chingis Sandanov Date: Thu, 24 Oct 2019 13:02:00 +0700 Subject: [PATCH] Add repository list tags method --- bitbucket.go | 16 ++++++-- repository.go | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 3 deletions(-) diff --git a/bitbucket.go b/bitbucket.go index 323ce33..c4fdd27 100644 --- a/bitbucket.go +++ b/bitbucket.go @@ -141,6 +141,16 @@ type RepositoryBranchOptions struct { MaxDepth int `json:"max_depth"` } +type RepositoryTagOptions struct { + Owner string `json:"owner"` + RepoSlug string `json:"repo_slug"` + Query string `json:"q"` + Sort string `json:"sort"` + PageNum int `json:"page"` + Pagelen int `json:"pagelen"` + MaxDepth int `json:"max_depth"` +} + type PullRequestsOptions struct { ID string `json:"id"` CommentID string `json:"comment_id"` @@ -237,8 +247,8 @@ type DownloadsOptions struct { } type PageRes struct { - Page int32 `json:"page"` - PageLen int32 `json:"pagelen"` + Page int32 `json:"page"` + PageLen int32 `json:"pagelen"` MaxDepth int32 `json:"max_depth"` - Size int32 `json:"size"` + Size int32 `json:"size"` } diff --git a/repository.go b/repository.go index f2deccd..9a5f8cd 100644 --- a/repository.go +++ b/repository.go @@ -62,6 +62,23 @@ type RepositoryBranch struct { Heads []map[string]interface{} } +type RepositoryTags struct { + Page int + Pagelen int + MaxDepth int + Size int + Next string + Tags []RepositoryTag +} + +type RepositoryTag struct { + Type string + Name string + Links map[string]interface{} + Target map[string]interface{} + Heads []map[string]interface{} +} + type Pipeline struct { Type string Enabled bool @@ -160,6 +177,38 @@ func (r *Repository) ListBranches(rbo *RepositoryBranchOptions) (*RepositoryBran return decodeRepositoryBranches(response) } +func (r *Repository) ListTags(rbo *RepositoryTagOptions) (*RepositoryTags, error) { + + params := url.Values{} + if rbo.Query != "" { + params.Add("q", rbo.Query) + } + + if rbo.Sort != "" { + params.Add("sort", rbo.Sort) + } + + if rbo.PageNum > 0 { + params.Add("page", strconv.Itoa(rbo.PageNum)) + } + + if rbo.Pagelen > 0 { + params.Add("pagelen", strconv.Itoa(rbo.Pagelen)) + } + + if rbo.MaxDepth > 0 { + params.Add("max_depth", strconv.Itoa(rbo.MaxDepth)) + } + + urlStr := r.c.requestUrl("/repositories/%s/%s/refs/tags?%s", rbo.Owner, rbo.RepoSlug, params.Encode()) + response, err := r.c.executeRaw("GET", urlStr, "") + if err != nil { + return nil, err + } + + return decodeRepositoryTags(response) +} + func (r *Repository) Delete(ro *RepositoryOptions) (interface{}, error) { urlStr := r.c.requestUrl("/repositories/%s/%s", ro.Owner, ro.RepoSlug) return r.c.execute("DELETE", urlStr, "") @@ -379,6 +428,58 @@ func decodeRepositoryBranches(branchResponse interface{}) (*RepositoryBranches, return &repositoryBranches, nil } +func decodeRepositoryTags(tagResponse interface{}) (*RepositoryTags, error) { + + var tagResponseMap map[string]interface{} + err := json.Unmarshal(tagResponse.([]byte), &tagResponseMap) + if err != nil { + return nil, err + } + + tagArray := tagResponseMap["values"].([]interface{}) + var tags []RepositoryTag + for _, tagEntry := range tagArray { + var tag RepositoryTag + err = mapstructure.Decode(tagEntry, &tag) + if err == nil { + tags = append(tags, tag) + } + } + + page, ok := tagResponseMap["page"].(float64) + if !ok { + page = 0 + } + + pagelen, ok := tagResponseMap["pagelen"].(float64) + if !ok { + pagelen = 0 + } + max_depth, ok := tagResponseMap["max_depth"].(float64) + if !ok { + max_depth = 0 + } + size, ok := tagResponseMap["size"].(float64) + if !ok { + size = 0 + } + + next, ok := tagResponseMap["next"].(string) + if !ok { + next = "" + } + + repositoryTags := RepositoryTags{ + Page: int(page), + Pagelen: int(pagelen), + MaxDepth: int(max_depth), + Size: int(size), + Next: next, + Tags: tags, + } + return &repositoryTags, nil +} + func decodePipelineRepository(repoResponse interface{}) (*Pipeline, error) { repoMap := repoResponse.(map[string]interface{})