This repository has been archived by the owner on Oct 31, 2018. It is now read-only.
forked from ktrysmt/go-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpullrequests.go
139 lines (110 loc) · 4.71 KB
/
pullrequests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package bitbucket
import (
"encoding/json"
"os"
"github.com/k0kubun/pp"
)
type PullRequests struct {
c *Client
}
func (p *PullRequests) Create(po *PullRequestsOptions) (interface{}, error) {
data := p.buildPullRequestBody(po)
urlStr := p.c.requestUrl("/repositories/%s/%s/pullrequests/", po.Owner, po.Repo_slug)
return p.c.execute("POST", urlStr, data)
}
func (p *PullRequests) Update(po *PullRequestsOptions) (interface{}, error) {
data := p.buildPullRequestBody(po)
urlStr := GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.Repo_slug + "/pullrequests/" + po.Id
return p.c.execute("PUT", urlStr, data)
}
func (p *PullRequests) Gets(po *PullRequestsOptions) (interface{}, error) {
urlStr := GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.Repo_slug + "/pullrequests/"
return p.c.execute("GET", urlStr, "")
}
func (p *PullRequests) Get(po *PullRequestsOptions) (interface{}, error) {
urlStr := GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.Repo_slug + "/pullrequests/" + po.Id
return p.c.execute("GET", urlStr, "")
}
func (p *PullRequests) Activities(po *PullRequestsOptions) (interface{}, error) {
urlStr := GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.Repo_slug + "/pullrequests/activity"
return p.c.execute("GET", urlStr, "")
}
func (p *PullRequests) Activity(po *PullRequestsOptions) (interface{}, error) {
urlStr := GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.Repo_slug + "/pullrequests/" + po.Id + "/activity"
return p.c.execute("GET", urlStr, "")
}
func (p *PullRequests) Commits(po *PullRequestsOptions) (interface{}, error) {
urlStr := GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.Repo_slug + "/pullrequests/" + po.Id + "/commits"
return p.c.execute("GET", urlStr, "")
}
func (p *PullRequests) Patch(po *PullRequestsOptions) (interface{}, error) {
urlStr := GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.Repo_slug + "/pullrequests/" + po.Id + "/patch"
return p.c.execute("GET", urlStr, "")
}
func (p *PullRequests) Diff(po *PullRequestsOptions) (interface{}, error) {
urlStr := GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.Repo_slug + "/pullrequests/" + po.Id + "/diff"
return p.c.execute("GET", urlStr, "")
}
func (p *PullRequests) Merge(po *PullRequestsOptions) (interface{}, error) {
data := p.buildPullRequestBody(po)
urlStr := GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.Repo_slug + "/pullrequests/" + po.Id + "/merge"
return p.c.execute("POST", urlStr, data)
}
func (p *PullRequests) Decline(po *PullRequestsOptions) (interface{}, error) {
data := p.buildPullRequestBody(po)
urlStr := GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.Repo_slug + "/pullrequests/" + po.Id + "/decline"
return p.c.execute("POST", urlStr, data)
}
func (p *PullRequests) GetComments(po *PullRequestsOptions) (interface{}, error) {
urlStr := GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.Repo_slug + "/pullrequests/" + po.Id + "/comments/"
return p.c.execute("GET", urlStr, "")
}
func (p *PullRequests) GetComment(po *PullRequestsOptions) (interface{}, error) {
urlStr := GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.Repo_slug + "/pullrequests/" + po.Id + "/comments/" + po.Comment_id
return p.c.execute("GET", urlStr, "")
}
func (p *PullRequests) buildPullRequestBody(po *PullRequestsOptions) string {
body := map[string]interface{}{}
body["source"] = map[string]interface{}{}
body["destination"] = map[string]interface{}{}
body["reviewers"] = []map[string]string{}
body["title"] = ""
body["description"] = ""
body["message"] = ""
body["close_source_branch"] = false
if n := len(po.Reviewers); n > 0 {
for i, user := range po.Reviewers {
body["reviewers"].([]map[string]string)[i] = map[string]string{"username": user}
}
}
if po.Source_branch != "" {
body["source"].(map[string]interface{})["branch"] = map[string]string{"name": po.Source_branch}
}
if po.Source_repository != "" {
body["source"].(map[string]interface{})["repository"] = map[string]interface{}{"full_name": po.Source_repository}
}
if po.Destination_branch != "" {
body["destination"].(map[string]interface{})["branch"] = map[string]interface{}{"name": po.Destination_branch}
}
if po.Destination_commit != "" {
body["destination"].(map[string]interface{})["commit"] = map[string]interface{}{"hash": po.Destination_commit}
}
if po.Title != "" {
body["title"] = po.Title
}
if po.Description != "" {
body["description"] = po.Description
}
if po.Message != "" {
body["message"] = po.Message
}
if po.Close_source_branch == true || po.Close_source_branch == false {
body["close_source_branch"] = po.Close_source_branch
}
data, err := json.Marshal(body)
if err != nil {
pp.Println(err)
os.Exit(9)
}
return string(data)
}