Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1e0ece40df | |||
| 60d6ca11e3 | |||
| 622d26f48f | |||
| 53781ac0e7 | |||
| 16a6ac7a61 |
@@ -1,11 +1,11 @@
|
||||
Library webhooks
|
||||
================
|
||||
<img align="right" src="https://raw.githubusercontent.com/go-playground/webhooks/v1/logo.png">
|
||||

|
||||
<img align="right" src="https://raw.githubusercontent.com/go-playground/webhooks/v2/logo.png">
|
||||

|
||||
[](https://semaphoreci.com/joeybloggs/webhooks)
|
||||
[](https://coveralls.io/github/go-playground/webhooks?branch=v1)
|
||||
[](https://coveralls.io/github/go-playground/webhooks?branch=v2)
|
||||
[](https://goreportcard.com/report/go-playground/webhooks)
|
||||
[](https://godoc.org/gopkg.in/go-playground/webhooks.v1)
|
||||
[](https://godoc.org/gopkg.in/go-playground/webhooks.v2)
|
||||

|
||||
|
||||
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
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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,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,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
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
@@ -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