Skip to content

Commit 60d6ca1

Browse files
author
Dean Karn
authored
Merge pull request #3 from alrs/headers
HTTP Header Passed to ProcessPayloadFunc
2 parents 622d26f + 53781ac commit 60d6ca1

File tree

8 files changed

+63
-55
lines changed

8 files changed

+63
-55
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func main() {
7070
}
7171

7272
// HandleRelease handles GitHub release events
73-
func HandleRelease(payload interface{}) {
73+
func HandleRelease(payload interface{}, header webhooks.Header) {
7474

7575
fmt.Println("Handling Release")
7676

@@ -86,7 +86,7 @@ func HandleRelease(payload interface{}) {
8686
}
8787

8888
// HandlePullRequest handles GitHub pull_request events
89-
func HandlePullRequest(payload interface{}) {
89+
func HandlePullRequest(payload interface{}, header webhooks.Header) {
9090

9191
fmt.Println("Handling Pull Request")
9292

@@ -125,7 +125,7 @@ func main() {
125125
}
126126

127127
// HandleMultiple handles multiple GitHub events
128-
func HandleMultiple(payload interface{}) {
128+
func HandleMultiple(payload interface{}, header webhooks.Header) {
129129

130130
fmt.Println("Handling Payload..")
131131

bitbucket/bitbucket.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,80 +100,82 @@ func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
100100
return
101101
}
102102

103+
hd := webhooks.Header(r.Header)
104+
103105
switch bitbucketEvent {
104106
case RepoPushEvent:
105107
var pl RepoPushPayload
106108
json.Unmarshal([]byte(payload), &pl)
107-
hook.runProcessPayloadFunc(fn, pl)
109+
hook.runProcessPayloadFunc(fn, pl, hd)
108110
case RepoForkEvent:
109111
var pl RepoForkPayload
110112
json.Unmarshal([]byte(payload), &pl)
111-
hook.runProcessPayloadFunc(fn, pl)
113+
hook.runProcessPayloadFunc(fn, pl, hd)
112114
case RepoCommitCommentCreatedEvent:
113115
var pl RepoCommitCommentCreatedPayload
114116
json.Unmarshal([]byte(payload), &pl)
115-
hook.runProcessPayloadFunc(fn, pl)
117+
hook.runProcessPayloadFunc(fn, pl, hd)
116118
case RepoCommitStatusCreatedEvent:
117119
var pl RepoCommitStatusCreatedPayload
118120
json.Unmarshal([]byte(payload), &pl)
119-
hook.runProcessPayloadFunc(fn, pl)
121+
hook.runProcessPayloadFunc(fn, pl, hd)
120122
case RepoCommitStatusUpdatedEvent:
121123
var pl RepoCommitStatusUpdatedPayload
122124
json.Unmarshal([]byte(payload), &pl)
123-
hook.runProcessPayloadFunc(fn, pl)
125+
hook.runProcessPayloadFunc(fn, pl, hd)
124126
case IssueCreatedEvent:
125127
var pl IssueCreatedPayload
126128
json.Unmarshal([]byte(payload), &pl)
127-
hook.runProcessPayloadFunc(fn, pl)
129+
hook.runProcessPayloadFunc(fn, pl, hd)
128130
case IssueUpdatedEvent:
129131
var pl IssueUpdatedPayload
130132
json.Unmarshal([]byte(payload), &pl)
131-
hook.runProcessPayloadFunc(fn, pl)
133+
hook.runProcessPayloadFunc(fn, pl, hd)
132134
case IssueCommentCreatedEvent:
133135
var pl IssueCommentCreatedPayload
134136
json.Unmarshal([]byte(payload), &pl)
135-
hook.runProcessPayloadFunc(fn, pl)
137+
hook.runProcessPayloadFunc(fn, pl, hd)
136138
case PullRequestCreatedEvent:
137139
var pl PullRequestCreatedPayload
138140
json.Unmarshal([]byte(payload), &pl)
139-
hook.runProcessPayloadFunc(fn, pl)
141+
hook.runProcessPayloadFunc(fn, pl, hd)
140142
case PullRequestUpdatedEvent:
141143
var pl PullRequestUpdatedPayload
142144
json.Unmarshal([]byte(payload), &pl)
143-
hook.runProcessPayloadFunc(fn, pl)
145+
hook.runProcessPayloadFunc(fn, pl, hd)
144146
case PullRequestApprovedEvent:
145147
var pl PullRequestApprovedPayload
146148
json.Unmarshal([]byte(payload), &pl)
147-
hook.runProcessPayloadFunc(fn, pl)
149+
hook.runProcessPayloadFunc(fn, pl, hd)
148150
case PullRequestApprovalRemovedEvent:
149151
var pl PullRequestApprovalRemovedPayload
150152
json.Unmarshal([]byte(payload), &pl)
151-
hook.runProcessPayloadFunc(fn, pl)
153+
hook.runProcessPayloadFunc(fn, pl, hd)
152154
case PullRequestMergedEvent:
153155
var pl PullRequestMergedPayload
154156
json.Unmarshal([]byte(payload), &pl)
155-
hook.runProcessPayloadFunc(fn, pl)
157+
hook.runProcessPayloadFunc(fn, pl, hd)
156158
case PullRequestDeclinedEvent:
157159
var pl PullRequestDeclinedPayload
158160
json.Unmarshal([]byte(payload), &pl)
159-
hook.runProcessPayloadFunc(fn, pl)
161+
hook.runProcessPayloadFunc(fn, pl, hd)
160162
case PullRequestCommentCreatedEvent:
161163
var pl PullRequestCommentCreatedPayload
162164
json.Unmarshal([]byte(payload), &pl)
163-
hook.runProcessPayloadFunc(fn, pl)
165+
hook.runProcessPayloadFunc(fn, pl, hd)
164166
case PullRequestCommentUpdatedEvent:
165167
var pl PullRequestCommentUpdatedPayload
166168
json.Unmarshal([]byte(payload), &pl)
167-
hook.runProcessPayloadFunc(fn, pl)
169+
hook.runProcessPayloadFunc(fn, pl, hd)
168170
case PullRequestCommentDeletedEvent:
169171
var pl PullRequestCommentDeletedPayload
170172
json.Unmarshal([]byte(payload), &pl)
171-
hook.runProcessPayloadFunc(fn, pl)
173+
hook.runProcessPayloadFunc(fn, pl, hd)
172174
}
173175
}
174176

175-
func (hook Webhook) runProcessPayloadFunc(fn webhooks.ProcessPayloadFunc, results interface{}) {
176-
go func(fn webhooks.ProcessPayloadFunc, results interface{}) {
177-
fn(results)
178-
}(fn, results)
177+
func (hook Webhook) runProcessPayloadFunc(fn webhooks.ProcessPayloadFunc, results interface{}, header webhooks.Header) {
178+
go func(fn webhooks.ProcessPayloadFunc, results interface{}, header webhooks.Header) {
179+
fn(results, header)
180+
}(fn, results, header)
179181
}

bitbucket/bitbucket_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929
)
3030

3131
// HandlePayload handles GitHub event(s)
32-
func HandlePayload(payload interface{}) {
32+
func HandlePayload(payload interface{}, header webhooks.Header) {
3333

3434
}
3535

examples/multiple-handlers/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func main() {
2525
}
2626

2727
// HandleRelease handles GitHub release events
28-
func HandleRelease(payload interface{}) {
28+
func HandleRelease(payload interface{}, header webhooks.Header) {
2929

3030
fmt.Println("Handling Release")
3131

@@ -41,7 +41,7 @@ func HandleRelease(payload interface{}) {
4141
}
4242

4343
// HandlePullRequest handles GitHub pull_request events
44-
func HandlePullRequest(payload interface{}) {
44+
func HandlePullRequest(payload interface{}, header webhooks.Header) {
4545

4646
fmt.Println("Handling Pull Request")
4747

examples/single-handler/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func main() {
2424
}
2525

2626
// HandleMultiple handles multiple GitHub events
27-
func HandleMultiple(payload interface{}) {
27+
func HandleMultiple(payload interface{}, header webhooks.Header) {
2828

2929
fmt.Println("Handling Payload..")
3030

github/github.go

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -129,96 +129,99 @@ func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
129129
}
130130
}
131131

132+
// Make headers available to ProcessPayloadFunc as a webhooks type
133+
hd := webhooks.Header(r.Header)
134+
132135
switch gitHubEvent {
133136
case CommitCommentEvent:
134137
var cc CommitCommentPayload
135138
json.Unmarshal([]byte(payload), &cc)
136-
hook.runProcessPayloadFunc(fn, cc)
139+
hook.runProcessPayloadFunc(fn, cc, hd)
137140
case CreateEvent:
138141
var c CreatePayload
139142
json.Unmarshal([]byte(payload), &c)
140-
hook.runProcessPayloadFunc(fn, c)
143+
hook.runProcessPayloadFunc(fn, c, hd)
141144
case DeleteEvent:
142145
var d DeletePayload
143146
json.Unmarshal([]byte(payload), &d)
144-
hook.runProcessPayloadFunc(fn, d)
147+
hook.runProcessPayloadFunc(fn, d, hd)
145148
case DeploymentEvent:
146149
var d DeploymentPayload
147150
json.Unmarshal([]byte(payload), &d)
148-
hook.runProcessPayloadFunc(fn, d)
151+
hook.runProcessPayloadFunc(fn, d, hd)
149152
case DeploymentStatusEvent:
150153
var d DeploymentStatusPayload
151154
json.Unmarshal([]byte(payload), &d)
152-
hook.runProcessPayloadFunc(fn, d)
155+
hook.runProcessPayloadFunc(fn, d, hd)
153156
case ForkEvent:
154157
var f ForkPayload
155158
json.Unmarshal([]byte(payload), &f)
156-
hook.runProcessPayloadFunc(fn, f)
159+
hook.runProcessPayloadFunc(fn, f, hd)
157160
case GollumEvent:
158161
var g GollumPayload
159162
json.Unmarshal([]byte(payload), &g)
160-
hook.runProcessPayloadFunc(fn, g)
163+
hook.runProcessPayloadFunc(fn, g, hd)
161164
case IssueCommentEvent:
162165
var i IssueCommentPayload
163166
json.Unmarshal([]byte(payload), &i)
164-
hook.runProcessPayloadFunc(fn, i)
167+
hook.runProcessPayloadFunc(fn, i, hd)
165168
case IssuesEvent:
166169
var i IssuesPayload
167170
json.Unmarshal([]byte(payload), &i)
168-
hook.runProcessPayloadFunc(fn, i)
171+
hook.runProcessPayloadFunc(fn, i, hd)
169172
case MemberEvent:
170173
var m MemberPayload
171174
json.Unmarshal([]byte(payload), &m)
172-
hook.runProcessPayloadFunc(fn, m)
175+
hook.runProcessPayloadFunc(fn, m, hd)
173176
case MembershipEvent:
174177
var m MembershipPayload
175178
json.Unmarshal([]byte(payload), &m)
176-
hook.runProcessPayloadFunc(fn, m)
179+
hook.runProcessPayloadFunc(fn, m, hd)
177180
case PageBuildEvent:
178181
var p PageBuildPayload
179182
json.Unmarshal([]byte(payload), &p)
180-
hook.runProcessPayloadFunc(fn, p)
183+
hook.runProcessPayloadFunc(fn, p, hd)
181184
case PublicEvent:
182185
var p PublicPayload
183186
json.Unmarshal([]byte(payload), &p)
184-
hook.runProcessPayloadFunc(fn, p)
187+
hook.runProcessPayloadFunc(fn, p, hd)
185188
case PullRequestReviewCommentEvent:
186189
var p PullRequestReviewCommentPayload
187190
json.Unmarshal([]byte(payload), &p)
188-
hook.runProcessPayloadFunc(fn, p)
191+
hook.runProcessPayloadFunc(fn, p, hd)
189192
case PullRequestEvent:
190193
var p PullRequestPayload
191194
json.Unmarshal([]byte(payload), &p)
192-
hook.runProcessPayloadFunc(fn, p)
195+
hook.runProcessPayloadFunc(fn, p, hd)
193196
case PushEvent:
194197
var p PushPayload
195198
json.Unmarshal([]byte(payload), &p)
196-
hook.runProcessPayloadFunc(fn, p)
199+
hook.runProcessPayloadFunc(fn, p, hd)
197200
case RepositoryEvent:
198201
var r RepositoryPayload
199202
json.Unmarshal([]byte(payload), &r)
200-
hook.runProcessPayloadFunc(fn, r)
203+
hook.runProcessPayloadFunc(fn, r, hd)
201204
case ReleaseEvent:
202205
var r ReleasePayload
203206
json.Unmarshal([]byte(payload), &r)
204-
hook.runProcessPayloadFunc(fn, r)
207+
hook.runProcessPayloadFunc(fn, r, hd)
205208
case StatusEvent:
206209
var s StatusPayload
207210
json.Unmarshal([]byte(payload), &s)
208-
hook.runProcessPayloadFunc(fn, s)
211+
hook.runProcessPayloadFunc(fn, s, hd)
209212
case TeamAddEvent:
210213
var t TeamAddPayload
211214
json.Unmarshal([]byte(payload), &t)
212-
hook.runProcessPayloadFunc(fn, t)
215+
hook.runProcessPayloadFunc(fn, t, hd)
213216
case WatchEvent:
214217
var w WatchPayload
215218
json.Unmarshal([]byte(payload), &w)
216-
hook.runProcessPayloadFunc(fn, w)
219+
hook.runProcessPayloadFunc(fn, w, hd)
217220
}
218221
}
219222

220-
func (hook Webhook) runProcessPayloadFunc(fn webhooks.ProcessPayloadFunc, results interface{}) {
221-
go func(fn webhooks.ProcessPayloadFunc, results interface{}) {
222-
fn(results)
223-
}(fn, results)
223+
func (hook Webhook) runProcessPayloadFunc(fn webhooks.ProcessPayloadFunc, results interface{}, header webhooks.Header) {
224+
go func(fn webhooks.ProcessPayloadFunc, results interface{}, header webhooks.Header) {
225+
fn(results, header)
226+
}(fn, results, header)
224227
}

github/github_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929
)
3030

3131
// HandlePayload handles GitHub event(s)
32-
func HandlePayload(payload interface{}) {
32+
func HandlePayload(payload interface{}, header webhooks.Header) {
3333

3434
}
3535

webhooks.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package webhooks
22

33
import "net/http"
44

5+
// Webhook provides http.Header to minimize imports
6+
type Header http.Header
7+
58
// Provider defines the type of webhook
69
type Provider int
710

@@ -34,7 +37,7 @@ type server struct {
3437
}
3538

3639
// ProcessPayloadFunc is a common function for payload return values
37-
type ProcessPayloadFunc func(payload interface{})
40+
type ProcessPayloadFunc func(payload interface{}, header Header)
3841

3942
// Run runs a server
4043
func Run(hook Webhook, addr string, path string) error {

0 commit comments

Comments
 (0)