Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,17 @@ type RepositoryBranchOptions struct {
BranchName string `json:"branch_name"`
}

type RepositoryBranchCreationOptions struct {
Owner string `json:"owner"`
RepoSlug string `json:"repo_slug"`
Name string `json:"name"`
Target RepositoryBranchTarget `json:"target"`
}

type RepositoryBranchTarget struct {
Hash string `json:"hash"`
}

type RepositoryTagOptions struct {
Owner string `json:"owner"`
RepoSlug string `json:"repo_slug"`
Expand Down
38 changes: 38 additions & 0 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,24 @@ func (r *Repository) GetBranch(rbo *RepositoryBranchOptions) (*RepositoryBranch,
return decodeRepositoryBranch(bodyString)
}

func (r *Repository) CreateBranch(rbo *RepositoryBranchCreationOptions) (*RepositoryBranch, error) {
urlStr := r.c.requestUrl("/repositories/%s/%s/refs/branches", rbo.Owner, rbo.RepoSlug)
data := r.buildBranchBody(rbo)

response, err := r.c.executeRaw("POST", urlStr, data)
if err != nil {
return nil, err
}

bodyBytes, err := ioutil.ReadAll(response)
if err != nil {
return nil, err
}

bodyString := string(bodyBytes)
return decodeRepositoryBranchCreated(bodyString)
}

func (r *Repository) ListTags(rbo *RepositoryTagOptions) (*RepositoryTags, error) {

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

func (r *Repository) buildBranchBody(rbo *RepositoryBranchCreationOptions) string {
body := map[string]interface{}{
"name": rbo.Name,
"target": map[string]string{
"hash": rbo.Target.Hash,
},
}

return r.buildJsonBody(body)
}

func (r *Repository) buildTagBody(rbo *RepositoryTagCreationOptions) string {
body := map[string]interface{}{
"name": rbo.Name,
Expand Down Expand Up @@ -949,6 +978,15 @@ func decodeRepositoryBranch(branchResponseStr string) (*RepositoryBranch, error)
return &repositoryBranch, nil
}

func decodeRepositoryBranchCreated(branchResponseStr string) (*RepositoryBranch, error) {
var responseBranchCreated RepositoryBranch
err := json.Unmarshal([]byte(branchResponseStr), &responseBranchCreated)
if err != nil {
return nil, err
}
return &responseBranchCreated, nil
}

func decodeRepositoryTagCreated(tagResponseStr string) (*RepositoryTag, error) {
var responseTagCreated RepositoryTag
err := json.Unmarshal([]byte(tagResponseStr), &responseTagCreated)
Expand Down