Skip to content

Commit dd20750

Browse files
authored
adding branch creation (#143)
1 parent 5452733 commit dd20750

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
@@ -210,6 +210,17 @@ type RepositoryBranchOptions struct {
210210
BranchName string `json:"branch_name"`
211211
}
212212

213+
type RepositoryBranchCreationOptions struct {
214+
Owner string `json:"owner"`
215+
RepoSlug string `json:"repo_slug"`
216+
Name string `json:"name"`
217+
Target RepositoryBranchTarget `json:"target"`
218+
}
219+
220+
type RepositoryBranchTarget struct {
221+
Hash string `json:"hash"`
222+
}
223+
213224
type RepositoryTagOptions struct {
214225
Owner string `json:"owner"`
215226
RepoSlug string `json:"repo_slug"`

repository.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,24 @@ func (r *Repository) GetBranch(rbo *RepositoryBranchOptions) (*RepositoryBranch,
331331
return decodeRepositoryBranch(bodyString)
332332
}
333333

334+
func (r *Repository) CreateBranch(rbo *RepositoryBranchCreationOptions) (*RepositoryBranch, error) {
335+
urlStr := r.c.requestUrl("/repositories/%s/%s/refs/branches", rbo.Owner, rbo.RepoSlug)
336+
data := r.buildBranchBody(rbo)
337+
338+
response, err := r.c.executeRaw("POST", urlStr, data)
339+
if err != nil {
340+
return nil, err
341+
}
342+
343+
bodyBytes, err := ioutil.ReadAll(response)
344+
if err != nil {
345+
return nil, err
346+
}
347+
348+
bodyString := string(bodyBytes)
349+
return decodeRepositoryBranchCreated(bodyString)
350+
}
351+
334352
func (r *Repository) ListTags(rbo *RepositoryTagOptions) (*RepositoryTags, error) {
335353

336354
params := url.Values{}
@@ -798,6 +816,17 @@ func (r *Repository) buildPipelineBuildNumberBody(rpbno *RepositoryPipelineBuild
798816
return r.buildJsonBody(body)
799817
}
800818

819+
func (r *Repository) buildBranchBody(rbo *RepositoryBranchCreationOptions) string {
820+
body := map[string]interface{}{
821+
"name": rbo.Name,
822+
"target": map[string]string{
823+
"hash": rbo.Target.Hash,
824+
},
825+
}
826+
827+
return r.buildJsonBody(body)
828+
}
829+
801830
func (r *Repository) buildTagBody(rbo *RepositoryTagCreationOptions) string {
802831
body := map[string]interface{}{
803832
"name": rbo.Name,
@@ -949,6 +978,15 @@ func decodeRepositoryBranch(branchResponseStr string) (*RepositoryBranch, error)
949978
return &repositoryBranch, nil
950979
}
951980

981+
func decodeRepositoryBranchCreated(branchResponseStr string) (*RepositoryBranch, error) {
982+
var responseBranchCreated RepositoryBranch
983+
err := json.Unmarshal([]byte(branchResponseStr), &responseBranchCreated)
984+
if err != nil {
985+
return nil, err
986+
}
987+
return &responseBranchCreated, nil
988+
}
989+
952990
func decodeRepositoryTagCreated(tagResponseStr string) (*RepositoryTag, error) {
953991
var responseTagCreated RepositoryTag
954992
err := json.Unmarshal([]byte(tagResponseStr), &responseTagCreated)

0 commit comments

Comments
 (0)