Compare commits

...

5 Commits

Author SHA1 Message Date
Dean Karn 1e0ece40df cleanup imports + README for v2 2016-10-20 19:51:20 -04:00
Dean Karn 60d6ca11e3 Merge pull request #3 from alrs/headers
HTTP Header Passed to ProcessPayloadFunc
2016-10-20 19:42:36 -04:00
Dean Karn 622d26f48f Merge pull request #4 from go-playground/v1
merge v1 into v2-development
2016-10-20 19:39:08 -04:00
Lars Lehtonen 53781ac0e7 Added header param to README 2016-10-20 16:02:45 -07:00
Lars Lehtonen 16a6ac7a61 Added http headers as another param to ProcessPayloadFunc 2016-10-20 15:53:15 -07:00
8 changed files with 89 additions and 78 deletions
+19 -18
View File
@@ -1,11 +1,11 @@
Library webhooks
================
<img align="right" src="https://raw.githubusercontent.com/go-playground/webhooks/v1/logo.png">
![Project status](https://img.shields.io/badge/version-1.0-green.svg)
<img align="right" src="https://raw.githubusercontent.com/go-playground/webhooks/v2/logo.png">
![Project status](https://img.shields.io/badge/version-2.0.0-green.svg)
[![Build Status](https://semaphoreci.com/api/v1/projects/5b9e2eda-8f8d-40aa-8cb4-e3f6120171fe/587820/badge.svg)](https://semaphoreci.com/joeybloggs/webhooks)
[![Coverage Status](https://coveralls.io/repos/go-playground/webhooks/badge.svg?branch=v1&service=github)](https://coveralls.io/github/go-playground/webhooks?branch=v1)
[![Coverage Status](https://coveralls.io/repos/go-playground/webhooks/badge.svg?branch=v2&service=github)](https://coveralls.io/github/go-playground/webhooks?branch=v2)
[![Go Report Card](https://goreportcard.com/badge/go-playground/webhooks)](https://goreportcard.com/report/go-playground/webhooks)
[![GoDoc](https://godoc.org/gopkg.in/go-playground/webhooks.v1?status.svg)](https://godoc.org/gopkg.in/go-playground/webhooks.v1)
[![GoDoc](https://godoc.org/gopkg.in/go-playground/webhooks.v2?status.svg)](https://godoc.org/gopkg.in/go-playground/webhooks.v2)
![License](https://img.shields.io/dub/l/vibe-d.svg)
Library webhooks allows for easy recieving and parsing of GitHub & Bitbucket Webhook Events
@@ -24,20 +24,18 @@ Installation
Use go get.
go get gopkg.in/go-playground/webhooks.v1
or to update
go get -u gopkg.in/go-playground/webhooks.v1
```shell
go get -u gopkg.in/go-playground/webhooks.v2
```
Then import the validator package into your own code.
import "gopkg.in/go-playground/webhooks.v1"
import "gopkg.in/go-playground/webhooks.v2"
Usage and documentation
------
Please see http://godoc.org/gopkg.in/go-playground/webhooks.v1 for detailed usage docs.
Please see http://godoc.org/gopkg.in/go-playground/webhooks.v2 for detailed usage docs.
##### Examples:
@@ -49,8 +47,8 @@ import (
"fmt"
"strconv"
"gopkg.in/go-playground/webhooks.v1"
"gopkg.in/go-playground/webhooks.v1/github"
"gopkg.in/go-playground/webhooks.v2"
"gopkg.in/go-playground/webhooks.v2/github"
)
const (
@@ -59,6 +57,7 @@ const (
)
func main() {
hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
hook.RegisterEvents(HandleRelease, github.ReleaseEvent)
hook.RegisterEvents(HandlePullRequest, github.PullRequestEvent)
@@ -70,7 +69,7 @@ func main() {
}
// HandleRelease handles GitHub release events
func HandleRelease(payload interface{}) {
func HandleRelease(payload interface{}, header webhooks.Header) {
fmt.Println("Handling Release")
@@ -86,7 +85,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")
@@ -95,6 +94,7 @@ func HandlePullRequest(payload interface{}) {
// Do whatever you want from here...
fmt.Printf("%+v", pl)
}
```
Single receiver for events you subscribe to
@@ -105,8 +105,8 @@ import (
"fmt"
"strconv"
"gopkg.in/go-playground/webhooks.v1"
"gopkg.in/go-playground/webhooks.v1/github"
"gopkg.in/go-playground/webhooks.v2"
"gopkg.in/go-playground/webhooks.v2/github"
)
const (
@@ -115,6 +115,7 @@ const (
)
func main() {
hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
hook.RegisterEvents(HandleMultiple, github.ReleaseEvent, github.PullRequestEvent) // Add as many as you want
@@ -125,7 +126,7 @@ func main() {
}
// HandleMultiple handles multiple GitHub events
func HandleMultiple(payload interface{}) {
func HandleMultiple(payload interface{}, header webhooks.Header) {
fmt.Println("Handling Payload..")
+24 -22
View File
@@ -5,7 +5,7 @@ import (
"io/ioutil"
"net/http"
"gopkg.in/go-playground/webhooks.v1"
"gopkg.in/go-playground/webhooks.v2"
)
// Webhook instance contains all methods needed to process events
@@ -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)
}
+2 -2
View File
@@ -9,7 +9,7 @@ import (
"time"
. "gopkg.in/go-playground/assert.v1"
"gopkg.in/go-playground/webhooks.v1"
"gopkg.in/go-playground/webhooks.v2"
)
// NOTES:
@@ -29,7 +29,7 @@ const (
)
// HandlePayload handles GitHub event(s)
func HandlePayload(payload interface{}) {
func HandlePayload(payload interface{}, header webhooks.Header) {
}
+5 -4
View File
@@ -4,8 +4,8 @@ import (
"fmt"
"strconv"
"gopkg.in/go-playground/webhooks.v1"
"gopkg.in/go-playground/webhooks.v1/github"
"gopkg.in/go-playground/webhooks.v2"
"gopkg.in/go-playground/webhooks.v2/github"
)
const (
@@ -14,6 +14,7 @@ const (
)
func main() {
hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
hook.RegisterEvents(HandleRelease, github.ReleaseEvent)
hook.RegisterEvents(HandlePullRequest, github.PullRequestEvent)
@@ -25,7 +26,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 +42,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")
+4 -3
View File
@@ -4,8 +4,8 @@ import (
"fmt"
"strconv"
"gopkg.in/go-playground/webhooks.v1"
"gopkg.in/go-playground/webhooks.v1/github"
"gopkg.in/go-playground/webhooks.v2"
"gopkg.in/go-playground/webhooks.v2/github"
)
const (
@@ -14,6 +14,7 @@ const (
)
func main() {
hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
hook.RegisterEvents(HandleMultiple, github.ReleaseEvent, github.PullRequestEvent) // Add as many as you want
@@ -24,7 +25,7 @@ func main() {
}
// HandleMultiple handles multiple GitHub events
func HandleMultiple(payload interface{}) {
func HandleMultiple(payload interface{}, header webhooks.Header) {
fmt.Println("Handling Payload..")
+29 -26
View File
@@ -8,7 +8,7 @@ import (
"io/ioutil"
"net/http"
"gopkg.in/go-playground/webhooks.v1"
"gopkg.in/go-playground/webhooks.v2"
)
// Webhook instance contains all methods needed to process events
@@ -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)
}
+2 -2
View File
@@ -9,7 +9,7 @@ import (
"time"
. "gopkg.in/go-playground/assert.v1"
"gopkg.in/go-playground/webhooks.v1"
"gopkg.in/go-playground/webhooks.v2"
)
// NOTES:
@@ -29,7 +29,7 @@ const (
)
// HandlePayload handles GitHub event(s)
func HandlePayload(payload interface{}) {
func HandlePayload(payload interface{}, header webhooks.Header) {
}
+4 -1
View File
@@ -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 {