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
20 changes: 11 additions & 9 deletions branchrestrictions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package bitbucket

import (
"encoding/json"
"os"

"github.com/k0kubun/pp"
"github.com/mitchellh/mapstructure"
)

Expand All @@ -23,7 +21,10 @@ func (b *BranchRestrictions) Gets(bo *BranchRestrictionsOptions) (interface{}, e
}

func (b *BranchRestrictions) Create(bo *BranchRestrictionsOptions) (*BranchRestrictions, error) {
data := b.buildBranchRestrictionsBody(bo)
data, err := b.buildBranchRestrictionsBody(bo)
if err != nil {
return nil, err
}
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions", bo.Owner, bo.RepoSlug)
response, err := b.c.execute("POST", urlStr, data)
if err != nil {
Expand All @@ -44,7 +45,10 @@ func (b *BranchRestrictions) Get(bo *BranchRestrictionsOptions) (*BranchRestrict
}

func (b *BranchRestrictions) Update(bo *BranchRestrictionsOptions) (interface{}, error) {
data := b.buildBranchRestrictionsBody(bo)
data, err := b.buildBranchRestrictionsBody(bo)
if err != nil {
return nil, err
}
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions/%s", bo.Owner, bo.RepoSlug, bo.ID)
response, err := b.c.execute("PUT", urlStr, data)
if err != nil {
Expand Down Expand Up @@ -115,8 +119,7 @@ type branchRestrictionsBodyUser struct {
} `json:"links"`
}

func (b *BranchRestrictions) buildBranchRestrictionsBody(bo *BranchRestrictionsOptions) string {

func (b *BranchRestrictions) buildBranchRestrictionsBody(bo *BranchRestrictionsOptions) (string, error) {
var users []branchRestrictionsBodyUser
var groups []branchRestrictionsBodyGroup
for _, u := range bo.Users {
Expand All @@ -142,11 +145,10 @@ func (b *BranchRestrictions) buildBranchRestrictionsBody(bo *BranchRestrictionsO

data, err := json.Marshal(body)
if err != nil {
pp.Println(err)
os.Exit(9)
return "", err
}

return string(data)
return string(data), nil
}

func decodeBranchRestriction(branchResponse interface{}) (*BranchRestrictions, error) {
Expand Down
14 changes: 7 additions & 7 deletions deploykeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package bitbucket

import (
"encoding/json"
"os"

"github.com/k0kubun/pp"
"github.com/mitchellh/mapstructure"
)

Expand Down Expand Up @@ -35,22 +33,24 @@ func decodeDeployKey(response interface{}) (*DeployKey, error) {
return deployKey, nil
}

func buildDeployKeysBody(opt *DeployKeyOptions) string {
func buildDeployKeysBody(opt *DeployKeyOptions) (string, error) {
body := map[string]interface{}{}
body["label"] = opt.Label
body["key"] = opt.Key

data, err := json.Marshal(body)
if err != nil {
_, _ = pp.Println(err)
os.Exit(9)
return "", err
}

return string(data)
return string(data), nil
}

func (dk *DeployKeys) Create(opt *DeployKeyOptions) (*DeployKey, error) {
data := buildDeployKeysBody(opt)
data, err := buildDeployKeysBody(opt)
if err != nil {
return nil, err
}
urlStr := dk.c.requestUrl("/repositories/%s/%s/deploy-keys", opt.Owner, opt.RepoSlug)
response, err := dk.c.execute("POST", urlStr, data)
if err != nil {
Expand Down
20 changes: 11 additions & 9 deletions issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import (
"encoding/json"
"fmt"
"net/url"
"os"
"strings"

"github.com/k0kubun/pp"
)

type Issues struct {
Expand Down Expand Up @@ -54,13 +51,19 @@ func (p *Issues) Delete(io *IssuesOptions) (interface{}, error) {
}

func (p *Issues) Update(io *IssuesOptions) (interface{}, error) {
data := p.buildIssueBody(io)
data, err := p.buildIssueBody(io)
if err != nil {
return nil, err
}
urlStr := p.c.requestUrl("/repositories/%s/%s/issues/%s", io.Owner, io.RepoSlug, io.ID)
return p.c.execute("PUT", urlStr, data)
}

func (p *Issues) Create(io *IssuesOptions) (interface{}, error) {
data := p.buildIssueBody(io)
data, err := p.buildIssueBody(io)
if err != nil {
return nil, err
}
urlStr := p.c.requestUrl("/repositories/%s/%s/issues", io.Owner, io.RepoSlug)
return p.c.execute("POST", urlStr, data)
}
Expand Down Expand Up @@ -109,7 +112,7 @@ func (p *Issues) DeleteWatch(io *IssuesOptions) error {
return err
}

func (p *Issues) buildIssueBody(io *IssuesOptions) string {
func (p *Issues) buildIssueBody(io *IssuesOptions) (string, error) {
body := map[string]interface{}{}

// This feld is required
Expand Down Expand Up @@ -160,11 +163,10 @@ func (p *Issues) buildIssueBody(io *IssuesOptions) string {

data, err := json.Marshal(body)
if err != nil {
pp.Println(err)
os.Exit(9)
return "", err
}

return string(data)
return string(data), nil
}

func (p *Issues) GetComments(ico *IssueCommentsOptions) (interface{}, error) {
Expand Down
21 changes: 12 additions & 9 deletions project.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package bitbucket

import (
"encoding/json"
"os"

"github.com/k0kubun/pp"
"github.com/mitchellh/mapstructure"
)

Expand Down Expand Up @@ -38,7 +36,10 @@ func (t *Workspace) GetProject(opt *ProjectOptions) (*Project, error) {
}

func (t *Workspace) CreateProject(opt *ProjectOptions) (*Project, error) {
data := t.buildProjectBody(opt)
data, err := t.buildProjectBody(opt)
if err != nil {
return nil, err
}
urlStr := t.c.requestUrl("/workspaces/%s/projects", opt.Owner)
response, err := t.c.execute("POST", urlStr, data)
if err != nil {
Expand All @@ -54,7 +55,10 @@ func (t *Workspace) DeleteProject(opt *ProjectOptions) (interface{}, error) {
}

func (t *Workspace) UpdateProject(opt *ProjectOptions) (*Project, error) {
data := t.buildProjectBody(opt)
data, err := t.buildProjectBody(opt)
if err != nil {
return nil, err
}
urlStr := t.c.requestUrl("/workspaces/%s/projects/%s", opt.Owner, opt.Key)
response, err := t.c.execute("PUT", urlStr, data)
if err != nil {
Expand All @@ -64,17 +68,16 @@ func (t *Workspace) UpdateProject(opt *ProjectOptions) (*Project, error) {
return decodeProject(response)
}

func (t *Workspace) buildJsonBody(body map[string]interface{}) string {
func (t *Workspace) buildJsonBody(body map[string]interface{}) (string, error) {
data, err := json.Marshal(body)
if err != nil {
pp.Println(err)
os.Exit(9)
return "", err
}

return string(data)
return string(data), nil
}

func (t *Workspace) buildProjectBody(opts *ProjectOptions) string {
func (t *Workspace) buildProjectBody(opts *ProjectOptions) (string, error) {
body := map[string]interface{}{}

if opts.Description != "" {
Expand Down
31 changes: 19 additions & 12 deletions pullrequests.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@ package bitbucket
import (
"encoding/json"
"net/url"
"os"

"github.com/k0kubun/pp"
)

type PullRequests struct {
c *Client
}

func (p *PullRequests) Create(po *PullRequestsOptions) (interface{}, error) {
data := p.buildPullRequestBody(po)
data, err := p.buildPullRequestBody(po)
if err != nil {
return nil, err
}
urlStr := p.c.requestUrl("/repositories/%s/%s/pullrequests/", po.Owner, po.RepoSlug)
return p.c.execute("POST", urlStr, data)
}

func (p *PullRequests) Update(po *PullRequestsOptions) (interface{}, error) {
data := p.buildPullRequestBody(po)
data, err := p.buildPullRequestBody(po)
if err != nil {
return nil, err
}
urlStr := p.c.GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/pullrequests/" + po.ID
return p.c.execute("PUT", urlStr, data)
}
Expand Down Expand Up @@ -96,13 +99,19 @@ func (p *PullRequests) Diff(po *PullRequestsOptions) (interface{}, error) {
}

func (p *PullRequests) Merge(po *PullRequestsOptions) (interface{}, error) {
data := p.buildPullRequestBody(po)
data, err := p.buildPullRequestBody(po)
if err != nil {
return nil, err
}
urlStr := p.c.GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/pullrequests/" + po.ID + "/merge"
return p.c.execute("POST", urlStr, data)
}

func (p *PullRequests) Decline(po *PullRequestsOptions) (interface{}, error) {
data := p.buildPullRequestBody(po)
data, err := p.buildPullRequestBody(po)
if err != nil {
return nil, err
}
urlStr := p.c.GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/pullrequests/" + po.ID + "/decline"
return p.c.execute("POST", urlStr, data)
}
Expand Down Expand Up @@ -173,8 +182,7 @@ func (p *PullRequests) Statuses(po *PullRequestsOptions) (interface{}, error) {
return p.c.execute("GET", urlStr, "")
}

func (p *PullRequests) buildPullRequestBody(po *PullRequestsOptions) string {

func (p *PullRequests) buildPullRequestBody(po *PullRequestsOptions) (string, error) {
body := map[string]interface{}{}
body["source"] = map[string]interface{}{}
body["destination"] = map[string]interface{}{}
Expand Down Expand Up @@ -225,11 +233,10 @@ func (p *PullRequests) buildPullRequestBody(po *PullRequestsOptions) string {

data, err := json.Marshal(body)
if err != nil {
pp.Println(err)
os.Exit(9)
return "", err
}

return string(data)
return string(data), nil
}

func (p *PullRequests) buildPullRequestCommentBody(co *PullRequestCommentOptions) (string, error) {
Expand Down
Loading