Skip to content

Commit

Permalink
Adds the ability to create forks (#91)
Browse files Browse the repository at this point in the history
* Added fork function

* Fixed is_private type to bool

* Go mod tidy

* Removed indirect from protobuf mod requirement

Co-authored-by: Daniel Rivas <[email protected]>
  • Loading branch information
danielrs and danielrs authored Jul 18, 2020
1 parent a8d562c commit c020169
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
16 changes: 16 additions & 0 deletions bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,22 @@ type RepositoryOptions struct {
Project string `json:"project"`
}

type RepositoryForkOptions struct {
FromOwner string `json:"from_owner"`
FromSlug string `json:"from_slug"`
Owner string `json:"owner"`
// TODO: does the API supports specifying slug on forks?
// see: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/forks#post
Name string `json:"name"`
IsPrivate string `json:"is_private"`
Description string `json:"description"`
ForkPolicy string `json:"fork_policy"`
Language string `json:"language"`
HasIssues string `json:"has_issues"`
HasWiki string `json:"has_wiki"`
Project string `json:"project"`
}

type RepositoryFilesOptions struct {
Owner string `json:"owner"`
RepoSlug string `json:"repo_slug"`
Expand Down
53 changes: 52 additions & 1 deletion repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path"
"strconv"
"strings"

"github.com/k0kubun/pp"
"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -135,6 +136,17 @@ func (r *Repository) Create(ro *RepositoryOptions) (*Repository, error) {
return decodeRepository(response)
}

func (r *Repository) Fork(fo *RepositoryForkOptions) (*Repository, error) {
data := r.buildForkBody(fo)
urlStr := r.c.requestUrl("/repositories/%s/%s/forks", fo.FromOwner, fo.FromSlug)
response, err := r.c.execute("POST", urlStr, data)
if err != nil {
return nil, err
}

return decodeRepository(response)
}

func (r *Repository) Get(ro *RepositoryOptions) (*Repository, error) {
urlStr := r.c.requestUrl("/repositories/%s/%s", ro.Owner, ro.RepoSlug)
response, err := r.c.execute("GET", urlStr, "")
Expand Down Expand Up @@ -331,7 +343,7 @@ func (r *Repository) buildRepositoryBody(ro *RepositoryOptions) string {
// body["name"] = ro.Name
//}
if ro.IsPrivate != "" {
body["is_private"] = ro.IsPrivate
body["is_private"] = strings.ToLower(strings.TrimSpace(ro.IsPrivate)) != "false"
}
if ro.Description != "" {
body["description"] = ro.Description
Expand All @@ -357,6 +369,45 @@ func (r *Repository) buildRepositoryBody(ro *RepositoryOptions) string {
return r.buildJsonBody(body)
}

func (r *Repository) buildForkBody(fo *RepositoryForkOptions) string {

body := map[string]interface{}{}

if fo.Owner != "" {
body["workspace"] = map[string]string{
"slug": fo.Owner,
}
}
if fo.Name != "" {
body["name"] = fo.Name
}
if fo.IsPrivate != "" {
body["is_private"] = strings.ToLower(strings.TrimSpace(fo.IsPrivate)) != "false"
}
if fo.Description != "" {
body["description"] = fo.Description
}
if fo.ForkPolicy != "" {
body["fork_policy"] = fo.ForkPolicy
}
if fo.Language != "" {
body["language"] = fo.Language
}
if fo.HasIssues != "" {
body["has_issues"] = fo.HasIssues
}
if fo.HasWiki != "" {
body["has_wiki"] = fo.HasWiki
}
if fo.Project != "" {
body["project"] = map[string]string{
"key": fo.Project,
}
}

return r.buildJsonBody(body)
}

func (r *Repository) buildPipelineBody(rpo *RepositoryPipelineOptions) string {

body := map[string]interface{}{}
Expand Down

0 comments on commit c020169

Please sign in to comment.