Skip to content

Commit 99c0de9

Browse files
Add method to create repository tag (#103)
1 parent 6411edb commit 99c0de9

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

bitbucket.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,17 @@ type RepositoryTagOptions struct {
160160
MaxDepth int `json:"max_depth"`
161161
}
162162

163+
type RepositoryTagCreationOptions struct {
164+
Owner string `json:"owner"`
165+
RepoSlug string `json:"repo_slug"`
166+
Name string `json:"name"`
167+
Target RepositoryTagTarget `json:"target"`
168+
}
169+
170+
type RepositoryTagTarget struct {
171+
Hash string `json:"hash"`
172+
}
173+
163174
type PullRequestsOptions struct {
164175
ID string `json:"id"`
165176
CommentID string `json:"comment_id"`

repository.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,24 @@ func (r *Repository) ListTags(rbo *RepositoryTagOptions) (*RepositoryTags, error
276276
return decodeRepositoryTags(bodyString)
277277
}
278278

279+
func (r *Repository) CreateTag(rbo *RepositoryTagCreationOptions) (*RepositoryTag, error) {
280+
urlStr := r.c.requestUrl("/repositories/%s/%s/refs/tags", rbo.Owner, rbo.RepoSlug)
281+
data := r.buildTagBody(rbo)
282+
283+
response, err := r.c.executeRaw("POST", urlStr, data)
284+
if err != nil {
285+
return nil, err
286+
}
287+
288+
bodyBytes, err := ioutil.ReadAll(response)
289+
if err != nil {
290+
return nil, err
291+
}
292+
293+
bodyString := string(bodyBytes)
294+
return decodeRepositoryTagCreated(bodyString)
295+
}
296+
279297
func (r *Repository) Delete(ro *RepositoryOptions) (interface{}, error) {
280298
urlStr := r.c.requestUrl("/repositories/%s/%s", ro.Owner, ro.RepoSlug)
281299
return r.c.execute("DELETE", urlStr, "")
@@ -480,6 +498,17 @@ func (r *Repository) buildPipelineBuildNumberBody(rpbno *RepositoryPipelineBuild
480498
return r.buildJsonBody(body)
481499
}
482500

501+
func (r *Repository) buildTagBody(rbo *RepositoryTagCreationOptions) string {
502+
body := map[string]interface{}{
503+
"name": rbo.Name,
504+
"target": map[string]string{
505+
"hash": rbo.Target.Hash,
506+
},
507+
}
508+
509+
return r.buildJsonBody(body)
510+
}
511+
483512
func decodeRepository(repoResponse interface{}) (*Repository, error) {
484513
repoMap := repoResponse.(map[string]interface{})
485514

@@ -579,6 +608,15 @@ func decodeRepositoryBranch(branchResponseStr string) (*RepositoryBranch, error)
579608
return &repositoryBranch, nil
580609
}
581610

611+
func decodeRepositoryTagCreated(tagResponseStr string) (*RepositoryTag, error) {
612+
var responseTagCreated RepositoryTag
613+
err := json.Unmarshal([]byte(tagResponseStr), &responseTagCreated)
614+
if err != nil {
615+
return nil, err
616+
}
617+
return &responseTagCreated, nil
618+
}
619+
582620
func decodeRepositoryTags(tagResponseStr string) (*RepositoryTags, error) {
583621

584622
var tagResponseMap map[string]interface{}

0 commit comments

Comments
 (0)