forked from ktrysmt/go-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpullrequests.go
177 lines (144 loc) · 5.45 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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)
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)
urlStr := GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/pullrequests/" + po.ID
return p.c.execute("PUT", urlStr, data)
}
func (p *PullRequests) Gets(po *PullRequestsOptions) (interface{}, error) {
urlStr := GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/pullrequests/"
if po.States != nil && len(po.States) != 0 {
parsed, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
query := parsed.Query()
for _, state := range po.States {
query.Set("state", state)
}
parsed.RawQuery = query.Encode()
urlStr = parsed.String()
}
if po.Query != "" {
parsed, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
query := parsed.Query()
query.Set("q", po.Query)
parsed.RawQuery = query.Encode()
urlStr = parsed.String()
}
if po.Sort != "" {
parsed, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
query := parsed.Query()
query.Set("sort", po.Sort)
parsed.RawQuery = query.Encode()
urlStr = parsed.String()
}
return p.c.execute("GET", urlStr, "")
}
func (p *PullRequests) Get(po *PullRequestsOptions) (interface{}, error) {
urlStr := GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/pullrequests/" + po.ID
return p.c.execute("GET", urlStr, "")
}
func (p *PullRequests) Activities(po *PullRequestsOptions) (interface{}, error) {
urlStr := GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/pullrequests/activity"
return p.c.execute("GET", urlStr, "")
}
func (p *PullRequests) Activity(po *PullRequestsOptions) (interface{}, error) {
urlStr := GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/pullrequests/" + po.ID + "/activity"
return p.c.execute("GET", urlStr, "")
}
func (p *PullRequests) Commits(po *PullRequestsOptions) (interface{}, error) {
urlStr := GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/pullrequests/" + po.ID + "/commits"
return p.c.execute("GET", urlStr, "")
}
func (p *PullRequests) Patch(po *PullRequestsOptions) (interface{}, error) {
urlStr := GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/pullrequests/" + po.ID + "/patch"
return p.c.execute("GET", urlStr, "")
}
func (p *PullRequests) Diff(po *PullRequestsOptions) (interface{}, error) {
urlStr := GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/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.RepoSlug + "/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.RepoSlug + "/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.RepoSlug + "/pullrequests/" + po.ID + "/comments/"
return p.c.execute("GET", urlStr, "")
}
func (p *PullRequests) GetComment(po *PullRequestsOptions) (interface{}, error) {
urlStr := GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/pullrequests/" + po.ID + "/comments/" + po.CommentID
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 {
body["reviewers"] = make([]map[string]string, n)
for i, user := range po.Reviewers {
body["reviewers"].([]map[string]string)[i] = map[string]string{"username": user}
}
}
if po.SourceBranch != "" {
body["source"].(map[string]interface{})["branch"] = map[string]string{"name": po.SourceBranch}
}
if po.SourceRepository != "" {
body["source"].(map[string]interface{})["repository"] = map[string]interface{}{"full_name": po.SourceRepository}
}
if po.DestinationBranch != "" {
body["destination"].(map[string]interface{})["branch"] = map[string]interface{}{"name": po.DestinationBranch}
}
if po.DestinationCommit != "" {
body["destination"].(map[string]interface{})["commit"] = map[string]interface{}{"hash": po.DestinationCommit}
}
if po.Title != "" {
body["title"] = po.Title
}
if po.Description != "" {
body["description"] = po.Description
}
if po.Message != "" {
body["message"] = po.Message
}
if po.CloseSourceBranch == true || po.CloseSourceBranch == false {
body["close_source_branch"] = po.CloseSourceBranch
}
data, err := json.Marshal(body)
if err != nil {
pp.Println(err)
os.Exit(9)
}
return string(data)
}