Skip to content

Commit c020169

Browse files
authored
Adds the ability to create forks (#91)
* 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]>
1 parent a8d562c commit c020169

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

bitbucket.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ type RepositoryOptions struct {
109109
Project string `json:"project"`
110110
}
111111

112+
type RepositoryForkOptions struct {
113+
FromOwner string `json:"from_owner"`
114+
FromSlug string `json:"from_slug"`
115+
Owner string `json:"owner"`
116+
// TODO: does the API supports specifying slug on forks?
117+
// see: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/forks#post
118+
Name string `json:"name"`
119+
IsPrivate string `json:"is_private"`
120+
Description string `json:"description"`
121+
ForkPolicy string `json:"fork_policy"`
122+
Language string `json:"language"`
123+
HasIssues string `json:"has_issues"`
124+
HasWiki string `json:"has_wiki"`
125+
Project string `json:"project"`
126+
}
127+
112128
type RepositoryFilesOptions struct {
113129
Owner string `json:"owner"`
114130
RepoSlug string `json:"repo_slug"`

repository.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path"
99
"strconv"
10+
"strings"
1011

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

139+
func (r *Repository) Fork(fo *RepositoryForkOptions) (*Repository, error) {
140+
data := r.buildForkBody(fo)
141+
urlStr := r.c.requestUrl("/repositories/%s/%s/forks", fo.FromOwner, fo.FromSlug)
142+
response, err := r.c.execute("POST", urlStr, data)
143+
if err != nil {
144+
return nil, err
145+
}
146+
147+
return decodeRepository(response)
148+
}
149+
138150
func (r *Repository) Get(ro *RepositoryOptions) (*Repository, error) {
139151
urlStr := r.c.requestUrl("/repositories/%s/%s", ro.Owner, ro.RepoSlug)
140152
response, err := r.c.execute("GET", urlStr, "")
@@ -331,7 +343,7 @@ func (r *Repository) buildRepositoryBody(ro *RepositoryOptions) string {
331343
// body["name"] = ro.Name
332344
//}
333345
if ro.IsPrivate != "" {
334-
body["is_private"] = ro.IsPrivate
346+
body["is_private"] = strings.ToLower(strings.TrimSpace(ro.IsPrivate)) != "false"
335347
}
336348
if ro.Description != "" {
337349
body["description"] = ro.Description
@@ -357,6 +369,45 @@ func (r *Repository) buildRepositoryBody(ro *RepositoryOptions) string {
357369
return r.buildJsonBody(body)
358370
}
359371

372+
func (r *Repository) buildForkBody(fo *RepositoryForkOptions) string {
373+
374+
body := map[string]interface{}{}
375+
376+
if fo.Owner != "" {
377+
body["workspace"] = map[string]string{
378+
"slug": fo.Owner,
379+
}
380+
}
381+
if fo.Name != "" {
382+
body["name"] = fo.Name
383+
}
384+
if fo.IsPrivate != "" {
385+
body["is_private"] = strings.ToLower(strings.TrimSpace(fo.IsPrivate)) != "false"
386+
}
387+
if fo.Description != "" {
388+
body["description"] = fo.Description
389+
}
390+
if fo.ForkPolicy != "" {
391+
body["fork_policy"] = fo.ForkPolicy
392+
}
393+
if fo.Language != "" {
394+
body["language"] = fo.Language
395+
}
396+
if fo.HasIssues != "" {
397+
body["has_issues"] = fo.HasIssues
398+
}
399+
if fo.HasWiki != "" {
400+
body["has_wiki"] = fo.HasWiki
401+
}
402+
if fo.Project != "" {
403+
body["project"] = map[string]string{
404+
"key": fo.Project,
405+
}
406+
}
407+
408+
return r.buildJsonBody(body)
409+
}
410+
360411
func (r *Repository) buildPipelineBody(rpo *RepositoryPipelineOptions) string {
361412

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

0 commit comments

Comments
 (0)