Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/ktrysmt/go-bitbucket
go 1.12

require (
github.com/golang/protobuf v1.0.0
github.com/golang/protobuf v1.0.0 // indirect
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you remove the line if it is not a problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it

github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/k0kubun/pp v2.3.0+incompatible
github.com/mattn/go-colorable v0.0.9 // indirect
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"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the below was the right one, and do you think?

Suggested change
body["is_private"] = strings.ToLower(strings.TrimSpace(ro.IsPrivate)) != "false"
body["is_private"] = strings.ToLower(strings.TrimSpace(ro.IsPrivate))

Copy link
Contributor Author

@danielrs danielrs Jul 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ktrysmt Bitbucket expects a boolean value in the payload for the is_private field. If you try and submit a request with the previous code you get the error:

{"type": "error", "error": {"fields": {"is_private": "expected bool"}, "message": "Bad request"}}

The new condition of != makes the value a real boolean, which is what Bitbucket expects. Check the schema here.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.
I understood that is_private: boolean is detected at the site.

}
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