Added http headers as another param to ProcessPayloadFunc
This commit is contained in:
+23
-21
@@ -100,80 +100,82 @@ func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
hd := webhooks.Header(r.Header)
|
||||
|
||||
switch bitbucketEvent {
|
||||
case RepoPushEvent:
|
||||
var pl RepoPushPayload
|
||||
json.Unmarshal([]byte(payload), &pl)
|
||||
hook.runProcessPayloadFunc(fn, pl)
|
||||
hook.runProcessPayloadFunc(fn, pl, hd)
|
||||
case RepoForkEvent:
|
||||
var pl RepoForkPayload
|
||||
json.Unmarshal([]byte(payload), &pl)
|
||||
hook.runProcessPayloadFunc(fn, pl)
|
||||
hook.runProcessPayloadFunc(fn, pl, hd)
|
||||
case RepoCommitCommentCreatedEvent:
|
||||
var pl RepoCommitCommentCreatedPayload
|
||||
json.Unmarshal([]byte(payload), &pl)
|
||||
hook.runProcessPayloadFunc(fn, pl)
|
||||
hook.runProcessPayloadFunc(fn, pl, hd)
|
||||
case RepoCommitStatusCreatedEvent:
|
||||
var pl RepoCommitStatusCreatedPayload
|
||||
json.Unmarshal([]byte(payload), &pl)
|
||||
hook.runProcessPayloadFunc(fn, pl)
|
||||
hook.runProcessPayloadFunc(fn, pl, hd)
|
||||
case RepoCommitStatusUpdatedEvent:
|
||||
var pl RepoCommitStatusUpdatedPayload
|
||||
json.Unmarshal([]byte(payload), &pl)
|
||||
hook.runProcessPayloadFunc(fn, pl)
|
||||
hook.runProcessPayloadFunc(fn, pl, hd)
|
||||
case IssueCreatedEvent:
|
||||
var pl IssueCreatedPayload
|
||||
json.Unmarshal([]byte(payload), &pl)
|
||||
hook.runProcessPayloadFunc(fn, pl)
|
||||
hook.runProcessPayloadFunc(fn, pl, hd)
|
||||
case IssueUpdatedEvent:
|
||||
var pl IssueUpdatedPayload
|
||||
json.Unmarshal([]byte(payload), &pl)
|
||||
hook.runProcessPayloadFunc(fn, pl)
|
||||
hook.runProcessPayloadFunc(fn, pl, hd)
|
||||
case IssueCommentCreatedEvent:
|
||||
var pl IssueCommentCreatedPayload
|
||||
json.Unmarshal([]byte(payload), &pl)
|
||||
hook.runProcessPayloadFunc(fn, pl)
|
||||
hook.runProcessPayloadFunc(fn, pl, hd)
|
||||
case PullRequestCreatedEvent:
|
||||
var pl PullRequestCreatedPayload
|
||||
json.Unmarshal([]byte(payload), &pl)
|
||||
hook.runProcessPayloadFunc(fn, pl)
|
||||
hook.runProcessPayloadFunc(fn, pl, hd)
|
||||
case PullRequestUpdatedEvent:
|
||||
var pl PullRequestUpdatedPayload
|
||||
json.Unmarshal([]byte(payload), &pl)
|
||||
hook.runProcessPayloadFunc(fn, pl)
|
||||
hook.runProcessPayloadFunc(fn, pl, hd)
|
||||
case PullRequestApprovedEvent:
|
||||
var pl PullRequestApprovedPayload
|
||||
json.Unmarshal([]byte(payload), &pl)
|
||||
hook.runProcessPayloadFunc(fn, pl)
|
||||
hook.runProcessPayloadFunc(fn, pl, hd)
|
||||
case PullRequestApprovalRemovedEvent:
|
||||
var pl PullRequestApprovalRemovedPayload
|
||||
json.Unmarshal([]byte(payload), &pl)
|
||||
hook.runProcessPayloadFunc(fn, pl)
|
||||
hook.runProcessPayloadFunc(fn, pl, hd)
|
||||
case PullRequestMergedEvent:
|
||||
var pl PullRequestMergedPayload
|
||||
json.Unmarshal([]byte(payload), &pl)
|
||||
hook.runProcessPayloadFunc(fn, pl)
|
||||
hook.runProcessPayloadFunc(fn, pl, hd)
|
||||
case PullRequestDeclinedEvent:
|
||||
var pl PullRequestDeclinedPayload
|
||||
json.Unmarshal([]byte(payload), &pl)
|
||||
hook.runProcessPayloadFunc(fn, pl)
|
||||
hook.runProcessPayloadFunc(fn, pl, hd)
|
||||
case PullRequestCommentCreatedEvent:
|
||||
var pl PullRequestCommentCreatedPayload
|
||||
json.Unmarshal([]byte(payload), &pl)
|
||||
hook.runProcessPayloadFunc(fn, pl)
|
||||
hook.runProcessPayloadFunc(fn, pl, hd)
|
||||
case PullRequestCommentUpdatedEvent:
|
||||
var pl PullRequestCommentUpdatedPayload
|
||||
json.Unmarshal([]byte(payload), &pl)
|
||||
hook.runProcessPayloadFunc(fn, pl)
|
||||
hook.runProcessPayloadFunc(fn, pl, hd)
|
||||
case PullRequestCommentDeletedEvent:
|
||||
var pl PullRequestCommentDeletedPayload
|
||||
json.Unmarshal([]byte(payload), &pl)
|
||||
hook.runProcessPayloadFunc(fn, pl)
|
||||
hook.runProcessPayloadFunc(fn, pl, hd)
|
||||
}
|
||||
}
|
||||
|
||||
func (hook Webhook) runProcessPayloadFunc(fn webhooks.ProcessPayloadFunc, results interface{}) {
|
||||
go func(fn webhooks.ProcessPayloadFunc, results interface{}) {
|
||||
fn(results)
|
||||
}(fn, results)
|
||||
func (hook Webhook) runProcessPayloadFunc(fn webhooks.ProcessPayloadFunc, results interface{}, header webhooks.Header) {
|
||||
go func(fn webhooks.ProcessPayloadFunc, results interface{}, header webhooks.Header) {
|
||||
fn(results, header)
|
||||
}(fn, results, header)
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ const (
|
||||
)
|
||||
|
||||
// HandlePayload handles GitHub event(s)
|
||||
func HandlePayload(payload interface{}) {
|
||||
func HandlePayload(payload interface{}, header webhooks.Header) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ func main() {
|
||||
}
|
||||
|
||||
// HandleRelease handles GitHub release events
|
||||
func HandleRelease(payload interface{}) {
|
||||
func HandleRelease(payload interface{}, header webhooks.Header) {
|
||||
|
||||
fmt.Println("Handling Release")
|
||||
|
||||
@@ -41,7 +41,7 @@ func HandleRelease(payload interface{}) {
|
||||
}
|
||||
|
||||
// HandlePullRequest handles GitHub pull_request events
|
||||
func HandlePullRequest(payload interface{}) {
|
||||
func HandlePullRequest(payload interface{}, header webhooks.Header) {
|
||||
|
||||
fmt.Println("Handling Pull Request")
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ func main() {
|
||||
}
|
||||
|
||||
// HandleMultiple handles multiple GitHub events
|
||||
func HandleMultiple(payload interface{}) {
|
||||
func HandleMultiple(payload interface{}, header webhooks.Header) {
|
||||
|
||||
fmt.Println("Handling Payload..")
|
||||
|
||||
|
||||
+28
-25
@@ -129,96 +129,99 @@ func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// Make headers available to ProcessPayloadFunc as a webhooks type
|
||||
hd := webhooks.Header(r.Header)
|
||||
|
||||
switch gitHubEvent {
|
||||
case CommitCommentEvent:
|
||||
var cc CommitCommentPayload
|
||||
json.Unmarshal([]byte(payload), &cc)
|
||||
hook.runProcessPayloadFunc(fn, cc)
|
||||
hook.runProcessPayloadFunc(fn, cc, hd)
|
||||
case CreateEvent:
|
||||
var c CreatePayload
|
||||
json.Unmarshal([]byte(payload), &c)
|
||||
hook.runProcessPayloadFunc(fn, c)
|
||||
hook.runProcessPayloadFunc(fn, c, hd)
|
||||
case DeleteEvent:
|
||||
var d DeletePayload
|
||||
json.Unmarshal([]byte(payload), &d)
|
||||
hook.runProcessPayloadFunc(fn, d)
|
||||
hook.runProcessPayloadFunc(fn, d, hd)
|
||||
case DeploymentEvent:
|
||||
var d DeploymentPayload
|
||||
json.Unmarshal([]byte(payload), &d)
|
||||
hook.runProcessPayloadFunc(fn, d)
|
||||
hook.runProcessPayloadFunc(fn, d, hd)
|
||||
case DeploymentStatusEvent:
|
||||
var d DeploymentStatusPayload
|
||||
json.Unmarshal([]byte(payload), &d)
|
||||
hook.runProcessPayloadFunc(fn, d)
|
||||
hook.runProcessPayloadFunc(fn, d, hd)
|
||||
case ForkEvent:
|
||||
var f ForkPayload
|
||||
json.Unmarshal([]byte(payload), &f)
|
||||
hook.runProcessPayloadFunc(fn, f)
|
||||
hook.runProcessPayloadFunc(fn, f, hd)
|
||||
case GollumEvent:
|
||||
var g GollumPayload
|
||||
json.Unmarshal([]byte(payload), &g)
|
||||
hook.runProcessPayloadFunc(fn, g)
|
||||
hook.runProcessPayloadFunc(fn, g, hd)
|
||||
case IssueCommentEvent:
|
||||
var i IssueCommentPayload
|
||||
json.Unmarshal([]byte(payload), &i)
|
||||
hook.runProcessPayloadFunc(fn, i)
|
||||
hook.runProcessPayloadFunc(fn, i, hd)
|
||||
case IssuesEvent:
|
||||
var i IssuesPayload
|
||||
json.Unmarshal([]byte(payload), &i)
|
||||
hook.runProcessPayloadFunc(fn, i)
|
||||
hook.runProcessPayloadFunc(fn, i, hd)
|
||||
case MemberEvent:
|
||||
var m MemberPayload
|
||||
json.Unmarshal([]byte(payload), &m)
|
||||
hook.runProcessPayloadFunc(fn, m)
|
||||
hook.runProcessPayloadFunc(fn, m, hd)
|
||||
case MembershipEvent:
|
||||
var m MembershipPayload
|
||||
json.Unmarshal([]byte(payload), &m)
|
||||
hook.runProcessPayloadFunc(fn, m)
|
||||
hook.runProcessPayloadFunc(fn, m, hd)
|
||||
case PageBuildEvent:
|
||||
var p PageBuildPayload
|
||||
json.Unmarshal([]byte(payload), &p)
|
||||
hook.runProcessPayloadFunc(fn, p)
|
||||
hook.runProcessPayloadFunc(fn, p, hd)
|
||||
case PublicEvent:
|
||||
var p PublicPayload
|
||||
json.Unmarshal([]byte(payload), &p)
|
||||
hook.runProcessPayloadFunc(fn, p)
|
||||
hook.runProcessPayloadFunc(fn, p, hd)
|
||||
case PullRequestReviewCommentEvent:
|
||||
var p PullRequestReviewCommentPayload
|
||||
json.Unmarshal([]byte(payload), &p)
|
||||
hook.runProcessPayloadFunc(fn, p)
|
||||
hook.runProcessPayloadFunc(fn, p, hd)
|
||||
case PullRequestEvent:
|
||||
var p PullRequestPayload
|
||||
json.Unmarshal([]byte(payload), &p)
|
||||
hook.runProcessPayloadFunc(fn, p)
|
||||
hook.runProcessPayloadFunc(fn, p, hd)
|
||||
case PushEvent:
|
||||
var p PushPayload
|
||||
json.Unmarshal([]byte(payload), &p)
|
||||
hook.runProcessPayloadFunc(fn, p)
|
||||
hook.runProcessPayloadFunc(fn, p, hd)
|
||||
case RepositoryEvent:
|
||||
var r RepositoryPayload
|
||||
json.Unmarshal([]byte(payload), &r)
|
||||
hook.runProcessPayloadFunc(fn, r)
|
||||
hook.runProcessPayloadFunc(fn, r, hd)
|
||||
case ReleaseEvent:
|
||||
var r ReleasePayload
|
||||
json.Unmarshal([]byte(payload), &r)
|
||||
hook.runProcessPayloadFunc(fn, r)
|
||||
hook.runProcessPayloadFunc(fn, r, hd)
|
||||
case StatusEvent:
|
||||
var s StatusPayload
|
||||
json.Unmarshal([]byte(payload), &s)
|
||||
hook.runProcessPayloadFunc(fn, s)
|
||||
hook.runProcessPayloadFunc(fn, s, hd)
|
||||
case TeamAddEvent:
|
||||
var t TeamAddPayload
|
||||
json.Unmarshal([]byte(payload), &t)
|
||||
hook.runProcessPayloadFunc(fn, t)
|
||||
hook.runProcessPayloadFunc(fn, t, hd)
|
||||
case WatchEvent:
|
||||
var w WatchPayload
|
||||
json.Unmarshal([]byte(payload), &w)
|
||||
hook.runProcessPayloadFunc(fn, w)
|
||||
hook.runProcessPayloadFunc(fn, w, hd)
|
||||
}
|
||||
}
|
||||
|
||||
func (hook Webhook) runProcessPayloadFunc(fn webhooks.ProcessPayloadFunc, results interface{}) {
|
||||
go func(fn webhooks.ProcessPayloadFunc, results interface{}) {
|
||||
fn(results)
|
||||
}(fn, results)
|
||||
func (hook Webhook) runProcessPayloadFunc(fn webhooks.ProcessPayloadFunc, results interface{}, header webhooks.Header) {
|
||||
go func(fn webhooks.ProcessPayloadFunc, results interface{}, header webhooks.Header) {
|
||||
fn(results, header)
|
||||
}(fn, results, header)
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ const (
|
||||
)
|
||||
|
||||
// HandlePayload handles GitHub event(s)
|
||||
func HandlePayload(payload interface{}) {
|
||||
func HandlePayload(payload interface{}, header webhooks.Header) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
+4
-1
@@ -2,6 +2,9 @@ package webhooks
|
||||
|
||||
import "net/http"
|
||||
|
||||
// Webhook provides http.Header to minimize imports
|
||||
type Header http.Header
|
||||
|
||||
// Provider defines the type of webhook
|
||||
type Provider int
|
||||
|
||||
@@ -34,7 +37,7 @@ type server struct {
|
||||
}
|
||||
|
||||
// ProcessPayloadFunc is a common function for payload return values
|
||||
type ProcessPayloadFunc func(payload interface{})
|
||||
type ProcessPayloadFunc func(payload interface{}, header Header)
|
||||
|
||||
// Run runs a server
|
||||
func Run(hook Webhook, addr string, path string) error {
|
||||
|
||||
Reference in New Issue
Block a user