Skip to content

Commit

Permalink
adding branch creation (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
rendername authored Jun 30, 2021
1 parent 5452733 commit dd20750
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
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

0 comments on commit dd20750

Please sign in to comment.