forked from harness/gitness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brokers.go
301 lines (239 loc) · 7.96 KB
/
brokers.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package bitbucket
import (
"encoding/json"
"errors"
"fmt"
"net/url"
)
// Bitbucket POSTs to the service URL you specify. The service
// receives an POST whenever user pushes to the repository.
const BrokerTypePost = "POST"
const BrokerTypePullRequestPost = "Pull Request POST"
type Broker struct {
// A Bitbucket assigned integer representing a unique
// identifier for the service.
Id int `json:"id"`
// A profile describing the service.
Profile *BrokerProfile `json:"service"`
}
type BrokerProfile struct {
// One of the supported services. The type is a
// case-insensitive value.
Type string `json:"type"`
// A parameter array containing a name and value pair
// for each parameter associated with the service.
Fields []*BrokerField `json:"fields"`
}
type BrokerField struct {
Name string `json:"name"`
Value string `json:"value"`
}
// Bitbucket offers integration with external services by allowing a set of
// services, or brokers, to run at certain events.
//
// The Bitbucket services resource provides functionality for adding,
// removing, and configuring brokers on your repositories. All the methods
// on this resource require the caller to authenticate.
//
// https://confluence.atlassian.com/display/BITBUCKET/services+Resource
type BrokerResource struct {
client *Client
}
func (r *BrokerResource) List(owner, slug string) ([]*Broker, error) {
brokers := []*Broker{}
path := fmt.Sprintf("/repositories/%s/%s/services", owner, slug)
if err := r.client.do("GET", path, nil, nil, &brokers); err != nil {
return nil, err
}
return brokers, nil
}
func (r *BrokerResource) Find(owner, slug string, id int) (*Broker, error) {
brokers := []*Broker{}
path := fmt.Sprintf("/repositories/%s/%s/services/%v", owner, slug, id)
if err := r.client.do("GET", path, nil, nil, &brokers); err != nil {
return nil, err
}
if len(brokers) == 0 {
return nil, ErrNotFound
}
return brokers[0], nil
}
func (r *BrokerResource) FindUrl(owner, slug, link, brokerType string) (*Broker, error) {
brokers, err := r.List(owner, slug)
if err != nil {
return nil, err
}
//fmt.Println("Total borkers:", len(brokers))
// iterate though list of brokers
for _, broker := range brokers {
if broker.Profile != nil && broker.Profile.Fields != nil {
//fmt.Printf("Compare: %v == %v is %v\n", broker.Profile.Type, brokerType, broker.Profile.Type == brokerType)
if broker.Profile.Type != brokerType {
continue
}
// iterate through list of fields
for _, field := range broker.Profile.Fields {
//fmt.Printf("-->%v==%v\n", field.Name, field.Value)
if field.Name == "URL" && field.Value == link {
// fmt.Println("Found match")
return broker, nil
}
}
//fmt.Println("Skipping...")
}
}
//fmt.Println("Not match found")
return nil, ErrNotFound
}
func (r *BrokerResource) Create(owner, slug, link, brokerType string) (*Broker, error) {
values := url.Values{}
values.Add("type", brokerType)
values.Add("URL", link)
if brokerType == BrokerTypePullRequestPost {
values.Add("comments", "off")
// TODO: figure out how to only set the hook for pull request create and update
// This is the sample string from the docs...it does not work becuase of
// the stupid slashes
// Check out the docs: https://confluence.atlassian.com/display/BITBUCKET/Pull+Request+POST+hook+management
// Sample string:
// -data "type=POST&URL=https://www.test.comcreate%2Fedit%2Fmerge%2Fdecline=on&comments=on&approve%2Funapprove=on"
}
b := Broker{}
path := fmt.Sprintf("/repositories/%s/%s/services", owner, slug)
if err := r.client.do("POST", path, nil, values, &b); err != nil {
return nil, err
}
return &b, nil
}
func (r *BrokerResource) Update(owner, slug, link, brokerType string, id int) (*Broker, error) {
values := url.Values{}
values.Add("type", brokerType)
values.Add("URL", link)
if brokerType == BrokerTypePullRequestPost {
values.Add("comments", "off")
// TODO: figure out how to also shutoff other events!
}
b := Broker{}
path := fmt.Sprintf("/repositories/%s/%s/services/%v", owner, slug, id)
if err := r.client.do("PUT", path, nil, values, &b); err != nil {
return nil, err
}
return &b, nil
}
// CreateUpdate will attempt to Create a Broker (Server Hook) if
// it doesn't already exist in the Bitbucket.
func (r *BrokerResource) CreateUpdate(owner, slug, link, brokerType string) (*Broker, error) {
if found, err := r.FindUrl(owner, slug, link, brokerType); err == nil {
// if the Broker already exists, just return it
// ... not need to re-create
//fmt.Println("Broker already found, skipping!", brokerType)
return found, nil
}
return r.Create(owner, slug, link, brokerType)
}
func (r *BrokerResource) Delete(owner, slug string, id int) error {
path := fmt.Sprintf("/repositories/%s/%s/services/%v", owner, slug, id)
return r.client.do("DELETE", path, nil, nil, nil)
}
func (r *BrokerResource) DeleteUrl(owner, slug, url, brokerType string) error {
broker, err := r.FindUrl(owner, slug, url, brokerType)
if err != nil {
return err
}
return r.Delete(owner, slug, broker.Id)
}
// patch := bitbucket.GetPatch(repo, p.Id, u.BitbucketToken, u.BitbucketSecret)
func (r *BrokerResource) GetPatch(owner, slug string, id int) (string, error) {
data := []byte{}
// uri, err := url.Parse("https://api.bitbucket.org/1.0" + path)
// https://bitbucket.org/!api/2.0/repositories/tdburke/test_mymysql/pullrequests/1/patch
path := fmt.Sprintf("/repositories/tdburke/test_mymysql/pullrequests/1/patch")
fmt.Println(path)
if err := r.client.do("GET", path, nil, nil, &data); err != nil {
fmt.Println("Get error:", err)
return "", err
}
fmt.Println(data)
if len(data) == 0 {
return "", ErrNotFound
}
return "", nil
}
// -----------------------------------------------------------------------------
// Post Receive Hook Functions
// -----------------------------------------------------------------------------
var ErrInvalidPostReceiveHook = errors.New("Invalid Post Receive Hook")
type PullRequestHook struct {
Id string `json:"id"`
}
type PostReceiveHook struct {
Repo *Repo `json:"repository"`
User string `json:"user"`
Url string `json:"canon_url"`
Commits []*Commit `json:"commits"`
}
type Commit struct {
Message string `json:"message"`
Author string `json:"author"`
Branch string `json:"branch"`
Hash string `json:"raw_node"`
Files []*File `json:"files"`
}
type File struct {
Name string `json:"file"`
Type string `json:"type"`
}
func ParseHook(raw []byte) (*PostReceiveHook, error) {
hook := PostReceiveHook{}
if err := json.Unmarshal(raw, &hook); err != nil {
return nil, err
}
// it is possible the JSON was parsed, however,
// was not from Bitbucket (maybe was from Google Code)
// So we'll check to be sure certain key fields
// were populated
switch {
case hook.Repo == nil:
return nil, ErrInvalidPostReceiveHook
case hook.Commits == nil:
return nil, ErrInvalidPostReceiveHook
case len(hook.User) == 0:
return nil, ErrInvalidPostReceiveHook
case len(hook.Commits) == 0:
return nil, ErrInvalidPostReceiveHook
}
return &hook, nil
}
func ParsePullRequestHook(raw []byte) (*PullRequestHook, error) {
data := make(map[string]map[string]interface{})
if err := json.Unmarshal(raw, &data); err != nil {
return nil, err
}
hook := PullRequestHook{}
if p, ok := data["pullrequest_created"]; ok {
fmt.Println(p["id"])
hook.Id = fmt.Sprintf("%v", p["id"])
if hook.Id == "" {
return nil, errors.New("Could not parse bitbucket pullrequest_created message")
}
/*
brokers := []*Broker{}
path := fmt.Sprintf("/repositories/%s/%s/services/%v", owner, slug, id)
if err := r.client.do("GET", path, nil, nil, &brokers); err != nil {
return nil, err
}
*/
return &hook, nil
}
//
// How do we get the diff file?
return nil, errors.New("Could not parse bitbucket pull request hook")
}
var ips = map[string]bool{
"63.246.22.222": true,
}
// Check's to see if the Post-Receive Build Hook is coming
// from a valid sender (IP Address)
func IsValidSender(ip string) bool {
return ips[ip]
}