/
bitbucketv2.go
309 lines (267 loc) · 9.78 KB
/
bitbucketv2.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
302
303
304
305
306
307
308
309
package bitbucketv2
//
// Docs: https://confluence.atlassian.com/bitbucket/event-payloads-740262817.html
//
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/bitrise-io/bitrise-webhooks/bitriseapi"
hookCommon "github.com/bitrise-io/bitrise-webhooks/service/hook/common"
"github.com/bitrise-io/go-utils/sliceutil"
)
const (
scmGit = "git"
scmMercurial = "hg"
)
// --------------------------
// --- Webhook Data Model ---
// ChangeItemTargetModel ...
type ChangeItemTargetModel struct {
Type string `json:"type"`
CommitHash string `json:"hash"`
CommitMessage string `json:"message"`
}
// ChangeItemModel ...
type ChangeItemModel struct {
Type string `json:"type"`
Name string `json:"name"`
Target ChangeItemTargetModel `json:"target"`
}
// ChangeInfoModel ...
type ChangeInfoModel struct {
ChangeNewItem ChangeItemModel `json:"new"`
}
// PushInfoModel ...
type PushInfoModel struct {
Changes []ChangeInfoModel `json:"changes"`
}
// PushEventModel ...
type PushEventModel struct {
PushInfo PushInfoModel `json:"push"`
RepositoryInfo RepositoryInfoModel `json:"repository"`
}
// OwnerInfoModel ...
type OwnerInfoModel struct {
Username string `json:"username"`
}
// RepositoryInfoModel ...
type RepositoryInfoModel struct {
FullName string `json:"full_name"`
IsPrivate bool `json:"is_private"`
// Scm - The type repository: Git (git) or Mercurial (hg).
Scm string `json:"scm"`
Owner OwnerInfoModel `json:"owner"`
}
// CommitInfoModel ...
type CommitInfoModel struct {
CommitHash string `json:"hash"`
}
// BranchInfoModel ...
type BranchInfoModel struct {
Name string `json:"name"`
}
// PullRequestBranchInfoModel ...
type PullRequestBranchInfoModel struct {
BranchInfo BranchInfoModel `json:"branch"`
CommitInfo CommitInfoModel `json:"commit"`
RepositoryInfo RepositoryInfoModel `json:"repository"`
}
// PullRequestInfoModel ...
type PullRequestInfoModel struct {
ID int `json:"id"`
Type string `json:"type"`
Title string `json:"title"`
Description string `json:"description"`
State string `json:"state"`
SourceInfo PullRequestBranchInfoModel `json:"source"`
DestinationInfo PullRequestBranchInfoModel `json:"destination"`
}
// PullRequestEventModel ...
type PullRequestEventModel struct {
PullRequestInfo PullRequestInfoModel `json:"pullrequest"`
RepositoryInfo RepositoryInfoModel `json:"repository"`
}
// ---------------------------------------
// --- Webhook Provider Implementation ---
// HookProvider ...
type HookProvider struct{}
func detectContentTypeAttemptNumberAndEventKey(header http.Header) (string, string, string, error) {
contentType := header.Get("Content-Type")
if contentType == "" {
return "", "", "", errors.New("No Content-Type Header found")
}
eventKey := header.Get("X-Event-Key")
if eventKey == "" {
return "", "", "", errors.New("No X-Event-Key Header found")
}
attemptNum := header.Get("X-Attempt-Number")
if attemptNum == "" {
return "", "", "", errors.New("No X-Attempt-Number Header found")
}
return contentType, attemptNum, eventKey, nil
}
func transformPushEvent(pushEvent PushEventModel) hookCommon.TransformResultModel {
if len(pushEvent.PushInfo.Changes) < 1 {
return hookCommon.TransformResultModel{
Error: fmt.Errorf("No 'changes' included in the webhook, can't start a build"),
}
}
switch pushEvent.RepositoryInfo.Scm {
case scmGit, scmMercurial:
// supported
default:
// unsupported
return hookCommon.TransformResultModel{
Error: fmt.Errorf("Unsupported repository / source control type (SCM): %s", pushEvent.RepositoryInfo.Scm),
}
}
triggerAPIParams := []bitriseapi.TriggerAPIParamsModel{}
errs := []string{}
for _, aChnage := range pushEvent.PushInfo.Changes {
aNewItm := aChnage.ChangeNewItem
if (pushEvent.RepositoryInfo.Scm == scmGit && aNewItm.Type == "branch") ||
(pushEvent.RepositoryInfo.Scm == scmMercurial && aNewItm.Type == "named_branch") {
if aNewItm.Target.Type != "commit" {
errs = append(errs, fmt.Sprintf("Target was not a type=commit change. Type was: %s", aNewItm.Target.Type))
continue
}
aTriggerAPIParams := bitriseapi.TriggerAPIParamsModel{
BuildParams: bitriseapi.BuildParamsModel{
Branch: aNewItm.Name,
CommitHash: aNewItm.Target.CommitHash,
CommitMessage: aNewItm.Target.CommitMessage,
},
}
triggerAPIParams = append(triggerAPIParams, aTriggerAPIParams)
} else if aNewItm.Type == "tag" {
if aNewItm.Target.Type != "commit" {
errs = append(errs, fmt.Sprintf("Target was not a type=commit change. Type was: %s", aNewItm.Target.Type))
continue
}
aTriggerAPIParams := bitriseapi.TriggerAPIParamsModel{
BuildParams: bitriseapi.BuildParamsModel{
Tag: aNewItm.Name,
CommitHash: aNewItm.Target.CommitHash,
CommitMessage: aNewItm.Target.CommitMessage,
},
}
triggerAPIParams = append(triggerAPIParams, aTriggerAPIParams)
} else {
errs = append(errs, fmt.Sprintf("Not a type=branch nor type=tag change. Change.Type was: %s", aNewItm.Type))
}
}
if len(triggerAPIParams) < 1 {
return hookCommon.TransformResultModel{
Error: fmt.Errorf("'changes' specified in the webhook, but none can be transformed into a build. Collected errors: %s", errs),
}
}
return hookCommon.TransformResultModel{
TriggerAPIParams: triggerAPIParams,
}
}
func transformPullRequestEvent(pullRequest PullRequestEventModel) hookCommon.TransformResultModel {
if pullRequest.PullRequestInfo.Type != "pullrequest" {
return hookCommon.TransformResultModel{
Error: fmt.Errorf("Pull Request type is not supported: %s", pullRequest.PullRequestInfo.Type),
ShouldSkip: true,
}
}
if pullRequest.PullRequestInfo.State != "OPEN" {
return hookCommon.TransformResultModel{
Error: fmt.Errorf("Pull Request state doesn't require a build: %s", pullRequest.PullRequestInfo.State),
ShouldSkip: true,
}
}
commitMsg := pullRequest.PullRequestInfo.Title
if pullRequest.PullRequestInfo.Description != "" {
commitMsg = fmt.Sprintf("%s\n\n%s", commitMsg, pullRequest.PullRequestInfo.Description)
}
if pullRequest.PullRequestInfo.SourceInfo.RepositoryInfo.FullName == pullRequest.PullRequestInfo.DestinationInfo.RepositoryInfo.FullName {
pullRequest.PullRequestInfo.SourceInfo.RepositoryInfo.IsPrivate = pullRequest.RepositoryInfo.IsPrivate
} else {
res, err := http.Head(fmt.Sprintf("https://api.bitbucket.org/2.0/repositories/%s", pullRequest.PullRequestInfo.SourceInfo.RepositoryInfo.FullName))
if err != nil {
return hookCommon.TransformResultModel{
Error: fmt.Errorf("Failed to check repository publicity: %s", err),
ShouldSkip: false,
}
}
pullRequest.PullRequestInfo.SourceInfo.RepositoryInfo.IsPrivate = (res.StatusCode != 200)
}
sourceRepositoryURL := ""
if pullRequest.PullRequestInfo.SourceInfo.RepositoryInfo.IsPrivate {
sourceRepositoryURL = fmt.Sprintf("git@bitbucket.org:%s.git", pullRequest.PullRequestInfo.SourceInfo.RepositoryInfo.FullName)
} else {
sourceRepoFullName := pullRequest.PullRequestInfo.SourceInfo.RepositoryInfo.FullName
sourceRepositoryURL = fmt.Sprintf("https://bitbucket.org/%s.git", sourceRepoFullName)
}
return hookCommon.TransformResultModel{
TriggerAPIParams: []bitriseapi.TriggerAPIParamsModel{
{
BuildParams: bitriseapi.BuildParamsModel{
CommitMessage: commitMsg,
CommitHash: pullRequest.PullRequestInfo.SourceInfo.CommitInfo.CommitHash,
Branch: pullRequest.PullRequestInfo.SourceInfo.BranchInfo.Name,
BranchDest: pullRequest.PullRequestInfo.DestinationInfo.BranchInfo.Name,
PullRequestID: &pullRequest.PullRequestInfo.ID,
PullRequestRepositoryURL: sourceRepositoryURL,
},
},
},
}
}
func isAcceptEventType(eventKey string) bool {
return sliceutil.IsStringInSlice(eventKey, []string{"repo:push", "pullrequest:created", "pullrequest:updated"})
}
// TransformRequest ...
func (hp HookProvider) TransformRequest(r *http.Request) hookCommon.TransformResultModel {
contentType, attemptNum, eventKey, err := detectContentTypeAttemptNumberAndEventKey(r.Header)
if err != nil {
return hookCommon.TransformResultModel{
Error: fmt.Errorf("Issue with Headers: %s", err),
}
}
if contentType != hookCommon.ContentTypeApplicationJSON {
return hookCommon.TransformResultModel{
Error: fmt.Errorf("Content-Type is not supported: %s", contentType),
}
}
if !isAcceptEventType(eventKey) {
return hookCommon.TransformResultModel{
Error: fmt.Errorf("X-Event-Key is not supported: %s", eventKey),
}
}
// Check: is this a re-try hook?
if attemptNum != "1" {
return hookCommon.TransformResultModel{
Error: fmt.Errorf("No retry is supported (X-Attempt-Number: %s)", attemptNum),
}
}
if r.Body == nil {
return hookCommon.TransformResultModel{
Error: fmt.Errorf("Failed to read content of request body: no or empty request body"),
}
}
if eventKey == "repo:push" {
var pushEvent PushEventModel
if err := json.NewDecoder(r.Body).Decode(&pushEvent); err != nil {
return hookCommon.TransformResultModel{
Error: fmt.Errorf("Failed to parse request body as JSON: %s", err),
}
}
return transformPushEvent(pushEvent)
} else if eventKey == "pullrequest:created" || eventKey == "pullrequest:updated" {
var pullRequestEvent PullRequestEventModel
if err := json.NewDecoder(r.Body).Decode(&pullRequestEvent); err != nil {
return hookCommon.TransformResultModel{
Error: fmt.Errorf("Failed to parse request body as JSON: %s", err),
}
}
return transformPullRequestEvent(pullRequestEvent)
}
return hookCommon.TransformResultModel{
Error: fmt.Errorf("Unsupported Bitbucket event type: %s", eventKey),
}
}