Skip to content

Commit 3f0716f

Browse files
joeybloggsjoeybloggs
authored andcommitted
Merge branch 'add-bitbucket-hooks' into v1
2 parents 098864e + b6efbd9 commit 3f0716f

File tree

6 files changed

+3454
-1
lines changed

6 files changed

+3454
-1
lines changed

bitbucket/bitbucket.go

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package bitbucket
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"net/http"
7+
8+
"gopkg.in/go-playground/webhooks.v1"
9+
)
10+
11+
// Webhook instance contains all methods needed to process events
12+
type Webhook struct {
13+
provider webhooks.Provider
14+
uuid string
15+
eventFuncs map[Event]webhooks.ProcessPayloadFunc
16+
}
17+
18+
// Config defines the configuration to create a new Bitbucket Webhook instance
19+
type Config struct {
20+
UUID string
21+
}
22+
23+
// Event defines a Bitbucket hook event type
24+
type Event string
25+
26+
// Bitbucket hook types
27+
const (
28+
RepoPushEvent Event = "repo:push"
29+
RepoForkEvent Event = "repo:fork"
30+
RepoCommitCommentCreatedEvent Event = "repo:commit_comment_created"
31+
RepoCommitStatusCreatedEvent Event = "repo:commit_status_created"
32+
RepoCommitStatusUpdatedEvent Event = "repo:commit_status_updated"
33+
IssueCreatedEvent Event = "issue:created"
34+
IssueUpdatedEvent Event = "issue:updated"
35+
IssueCommentCreatedEvent Event = "issue:comment_created"
36+
PullRequestCreatedEvent Event = "pullrequest:created"
37+
PullRequestUpdatedEvent Event = "pullrequest:updated"
38+
PullRequestApprovedEvent Event = "pullrequest:approved"
39+
PullRequestApprovalRemovedEvent Event = "pullrequest:unapproved"
40+
PullRequestMergedEvent Event = "pullrequest:fulfilled"
41+
PullRequestDeclinedEvent Event = "pullrequest:rejected"
42+
PullRequestCommentCreatedEvent Event = "pullrequest:comment_created"
43+
PullRequestCommentUpdatedEvent Event = "pullrequest:comment_updated"
44+
PullRequestCommentDeletedEvent Event = "pull_request:comment_deleted"
45+
)
46+
47+
// New creates and returns a WebHook instance denoted by the Provider type
48+
func New(config *Config) *Webhook {
49+
return &Webhook{
50+
provider: webhooks.Bitbucket,
51+
uuid: config.UUID,
52+
eventFuncs: map[Event]webhooks.ProcessPayloadFunc{},
53+
}
54+
}
55+
56+
// Provider returns the current hooks provider ID
57+
func (hook Webhook) Provider() webhooks.Provider {
58+
return hook.provider
59+
}
60+
61+
// RegisterEvents registers the function to call when the specified event(s) are encountered
62+
func (hook Webhook) RegisterEvents(fn webhooks.ProcessPayloadFunc, events ...Event) {
63+
64+
for _, event := range events {
65+
hook.eventFuncs[event] = fn
66+
}
67+
}
68+
69+
// ParsePayload parses and verifies the payload and fires off the mapped function, if it exists.
70+
func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
71+
72+
uuid := r.Header.Get("X-Hook-UUID")
73+
if uuid == "" {
74+
http.Error(w, "400 Bad Request - Missing X-Hook-UUID Header", http.StatusBadRequest)
75+
return
76+
}
77+
78+
if uuid != hook.uuid {
79+
http.Error(w, "403 Forbidden - Missing X-Hook-UUID does not match", http.StatusForbidden)
80+
return
81+
}
82+
83+
event := r.Header.Get("X-Event-Key")
84+
if event == "" {
85+
http.Error(w, "400 Bad Request - Missing X-Event-Key Header", http.StatusBadRequest)
86+
return
87+
}
88+
89+
bitbucketEvent := Event(event)
90+
91+
fn, ok := hook.eventFuncs[bitbucketEvent]
92+
// if no event registered
93+
if !ok {
94+
return
95+
}
96+
97+
payload, err := ioutil.ReadAll(r.Body)
98+
if err != nil || len(payload) == 0 {
99+
http.Error(w, "Error reading Body", http.StatusInternalServerError)
100+
return
101+
}
102+
103+
switch bitbucketEvent {
104+
case RepoPushEvent:
105+
var pl RepoPushPayload
106+
json.Unmarshal([]byte(payload), &pl)
107+
hook.runProcessPayloadFunc(fn, pl)
108+
case RepoForkEvent:
109+
var pl RepoForkPayload
110+
json.Unmarshal([]byte(payload), &pl)
111+
hook.runProcessPayloadFunc(fn, pl)
112+
case RepoCommitCommentCreatedEvent:
113+
var pl RepoCommitCommentCreatedPayload
114+
json.Unmarshal([]byte(payload), &pl)
115+
hook.runProcessPayloadFunc(fn, pl)
116+
case RepoCommitStatusCreatedEvent:
117+
var pl RepoCommitStatusCreatedPayload
118+
json.Unmarshal([]byte(payload), &pl)
119+
hook.runProcessPayloadFunc(fn, pl)
120+
case RepoCommitStatusUpdatedEvent:
121+
var pl RepoCommitStatusUpdatedPayload
122+
json.Unmarshal([]byte(payload), &pl)
123+
hook.runProcessPayloadFunc(fn, pl)
124+
case IssueCreatedEvent:
125+
var pl IssueCreatedPayload
126+
json.Unmarshal([]byte(payload), &pl)
127+
hook.runProcessPayloadFunc(fn, pl)
128+
case IssueUpdatedEvent:
129+
var pl IssueUpdatedPayload
130+
json.Unmarshal([]byte(payload), &pl)
131+
hook.runProcessPayloadFunc(fn, pl)
132+
case IssueCommentCreatedEvent:
133+
var pl IssueCommentCreatedPayload
134+
json.Unmarshal([]byte(payload), &pl)
135+
hook.runProcessPayloadFunc(fn, pl)
136+
case PullRequestCreatedEvent:
137+
var pl PullRequestCreatedPayload
138+
json.Unmarshal([]byte(payload), &pl)
139+
hook.runProcessPayloadFunc(fn, pl)
140+
case PullRequestUpdatedEvent:
141+
var pl PullRequestUpdatedPayload
142+
json.Unmarshal([]byte(payload), &pl)
143+
hook.runProcessPayloadFunc(fn, pl)
144+
case PullRequestApprovedEvent:
145+
var pl PullRequestApprovedPayload
146+
json.Unmarshal([]byte(payload), &pl)
147+
hook.runProcessPayloadFunc(fn, pl)
148+
case PullRequestApprovalRemovedEvent:
149+
var pl PullRequestApprovalRemovedPayload
150+
json.Unmarshal([]byte(payload), &pl)
151+
hook.runProcessPayloadFunc(fn, pl)
152+
case PullRequestMergedEvent:
153+
var pl PullRequestMergedPayload
154+
json.Unmarshal([]byte(payload), &pl)
155+
hook.runProcessPayloadFunc(fn, pl)
156+
case PullRequestDeclinedEvent:
157+
var pl PullRequestDeclinedPayload
158+
json.Unmarshal([]byte(payload), &pl)
159+
hook.runProcessPayloadFunc(fn, pl)
160+
case PullRequestCommentCreatedEvent:
161+
var pl PullRequestCommentCreatedPayload
162+
json.Unmarshal([]byte(payload), &pl)
163+
hook.runProcessPayloadFunc(fn, pl)
164+
case PullRequestCommentUpdatedEvent:
165+
var pl PullRequestCommentUpdatedPayload
166+
json.Unmarshal([]byte(payload), &pl)
167+
hook.runProcessPayloadFunc(fn, pl)
168+
case PullRequestCommentDeletedEvent:
169+
var pl PullRequestCommentDeletedPayload
170+
json.Unmarshal([]byte(payload), &pl)
171+
hook.runProcessPayloadFunc(fn, pl)
172+
}
173+
}
174+
175+
func (hook Webhook) runProcessPayloadFunc(fn webhooks.ProcessPayloadFunc, results interface{}) {
176+
go func(fn webhooks.ProcessPayloadFunc, results interface{}) {
177+
fn(results)
178+
}(fn, results)
179+
}

0 commit comments

Comments
 (0)