cleanup examples and README
This commit is contained in:
@@ -26,7 +26,6 @@ before_script:
|
|||||||
- go get -t ./...
|
- go get -t ./...
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- make lint
|
|
||||||
- make test
|
- make test
|
||||||
|
|
||||||
after_success: |
|
after_success: |
|
||||||
|
|||||||
@@ -37,110 +37,47 @@ Usage and Documentation
|
|||||||
Please see http://godoc.org/gopkg.in/go-playground/webhooks.v5 for detailed usage docs.
|
Please see http://godoc.org/gopkg.in/go-playground/webhooks.v5 for detailed usage docs.
|
||||||
|
|
||||||
##### Examples:
|
##### Examples:
|
||||||
|
|
||||||
Multiple Handlers for each event you subscribe to
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"gopkg.in/go-playground/webhooks.v5"
|
"net/http"
|
||||||
|
|
||||||
"gopkg.in/go-playground/webhooks.v5/github"
|
"gopkg.in/go-playground/webhooks.v5/github"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
path = "/webhooks"
|
path = "/webhooks"
|
||||||
port = 3016
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
hook, _ := github.New(github.Options.Secret("MyGitHubSuperSecretSecrect...?"))
|
||||||
|
|
||||||
hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
|
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
||||||
hook.RegisterEvents(HandleRelease, github.ReleaseEvent)
|
payload, err := hook.Parse(r, github.ReleaseEvent, github.PullRequestEvent)
|
||||||
hook.RegisterEvents(HandlePullRequest, github.PullRequestEvent)
|
if err != nil {
|
||||||
|
if err == github.ErrEventNotFound {
|
||||||
|
// ok event wasn;t one of the ones asked to be parsed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
switch payload.(type) {
|
||||||
|
|
||||||
err := webhooks.Run(hook, ":"+strconv.Itoa(port), path)
|
case github.ReleasePayload:
|
||||||
if err != nil {
|
release := payload.(github.ReleasePayload)
|
||||||
fmt.Println(err)
|
// Do whatever you want from here...
|
||||||
}
|
fmt.Printf("%+v", release)
|
||||||
|
|
||||||
|
case github.PullRequestPayload:
|
||||||
|
pullRequest := payload.(github.PullRequestPayload)
|
||||||
|
// Do whatever you want from here...
|
||||||
|
fmt.Printf("%+v", pullRequest)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
http.ListenAndServe(":3000", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleRelease handles GitHub release events
|
|
||||||
func HandleRelease(payload interface{}, header webhooks.Header) {
|
|
||||||
|
|
||||||
fmt.Println("Handling Release")
|
|
||||||
|
|
||||||
pl := payload.(github.ReleasePayload)
|
|
||||||
|
|
||||||
// only want to compile on full releases
|
|
||||||
if pl.Release.Draft || pl.Release.Prerelease || pl.Release.TargetCommitish != "master" {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do whatever you want from here...
|
|
||||||
fmt.Printf("%+v", pl)
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandlePullRequest handles GitHub pull_request events
|
|
||||||
func HandlePullRequest(payload interface{}, header webhooks.Header) {
|
|
||||||
|
|
||||||
fmt.Println("Handling Pull Request")
|
|
||||||
|
|
||||||
pl := payload.(github.PullRequestPayload)
|
|
||||||
|
|
||||||
// Do whatever you want from here...
|
|
||||||
fmt.Printf("%+v", pl)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Single receiver for events you subscribe to
|
|
||||||
```go
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"gopkg.in/go-playground/webhooks.v5"
|
|
||||||
"gopkg.in/go-playground/webhooks.v5/github"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
path = "/webhooks"
|
|
||||||
port = 3016
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
|
|
||||||
hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
|
|
||||||
hook.RegisterEvents(HandleMultiple, github.ReleaseEvent, github.PullRequestEvent) // Add as many as you want
|
|
||||||
|
|
||||||
err := webhooks.Run(hook, ":"+strconv.Itoa(port), path)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleMultiple handles multiple GitHub events
|
|
||||||
func HandleMultiple(payload interface{}, header webhooks.Header) {
|
|
||||||
|
|
||||||
fmt.Println("Handling Payload..")
|
|
||||||
|
|
||||||
switch payload.(type) {
|
|
||||||
|
|
||||||
case github.ReleasePayload:
|
|
||||||
release := payload.(github.ReleasePayload)
|
|
||||||
// Do whatever you want from here...
|
|
||||||
fmt.Printf("%+v", release)
|
|
||||||
|
|
||||||
case github.PullRequestPayload:
|
|
||||||
pullRequest := payload.(github.PullRequestPayload)
|
|
||||||
// Do whatever you want from here...
|
|
||||||
fmt.Printf("%+v", pullRequest)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Contributing
|
Contributing
|
||||||
|
|||||||
@@ -1,67 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"gopkg.in/go-playground/webhooks.v5"
|
|
||||||
"gopkg.in/go-playground/webhooks.v5/github"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
path = "/webhooks"
|
|
||||||
port = 3016
|
|
||||||
)
|
|
||||||
|
|
||||||
type myLogger struct {
|
|
||||||
PrintDebugs bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *myLogger) Info(msg ...interface{}) {
|
|
||||||
log.Println(msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *myLogger) Error(msg ...interface{}) {
|
|
||||||
log.Println(msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *myLogger) Debug(msg ...interface{}) {
|
|
||||||
if !l.PrintDebugs {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Println(msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// webhooks.DefaultLog=webhooks.NewLogger(true)
|
|
||||||
//
|
|
||||||
// or override with your own
|
|
||||||
webhooks.DefaultLog = &myLogger{PrintDebugs: true}
|
|
||||||
|
|
||||||
hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
|
|
||||||
hook.RegisterEvents(HandleMultiple, github.ReleaseEvent, github.PullRequestEvent) // Add as many as you want
|
|
||||||
|
|
||||||
err := webhooks.Run(hook, ":"+strconv.Itoa(port), path)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleMultiple handles multiple GitHub events
|
|
||||||
func HandleMultiple(payload interface{}, header webhooks.Header) {
|
|
||||||
fmt.Println("Handling Payload..")
|
|
||||||
|
|
||||||
switch payload.(type) {
|
|
||||||
|
|
||||||
case github.ReleasePayload:
|
|
||||||
release := payload.(github.ReleasePayload)
|
|
||||||
// Do whatever you want from here...
|
|
||||||
fmt.Printf("%+v", release)
|
|
||||||
|
|
||||||
case github.PullRequestPayload:
|
|
||||||
pullRequest := payload.(github.PullRequestPayload)
|
|
||||||
// Do whatever you want from here...
|
|
||||||
fmt.Printf("%+v", pullRequest)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,50 +2,61 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"gopkg.in/go-playground/webhooks.v5"
|
"net/http"
|
||||||
|
|
||||||
"gopkg.in/go-playground/webhooks.v5/github"
|
"gopkg.in/go-playground/webhooks.v5/github"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
path = "/webhooks"
|
path1 = "/webhooks1"
|
||||||
port = 3016
|
path2 = "/webhooks2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
|
hook1, _ := github.New(github.Options.Secret("MyGitHubSuperSecretSecrect...?"))
|
||||||
hook.RegisterEvents(HandleRelease, github.ReleaseEvent)
|
hook2, _ := github.New(github.Options.Secret("MyGitHubSuperSecretSecrect2...?"))
|
||||||
hook.RegisterEvents(HandlePullRequest, github.PullRequestEvent)
|
|
||||||
|
|
||||||
err := webhooks.Run(hook, ":"+strconv.Itoa(port), path)
|
http.HandleFunc(path1, func(w http.ResponseWriter, r *http.Request) {
|
||||||
if err != nil {
|
payload, err := hook1.Parse(r, github.ReleaseEvent, github.PullRequestEvent)
|
||||||
fmt.Println(err)
|
if err != nil {
|
||||||
}
|
if err == github.ErrEventNotFound {
|
||||||
}
|
// ok event wasn;t one of the ones asked to be parsed
|
||||||
|
}
|
||||||
// HandleRelease handles GitHub release events
|
}
|
||||||
func HandleRelease(payload interface{}, header webhooks.Header) {
|
switch payload.(type) {
|
||||||
fmt.Println("Handling Release")
|
|
||||||
|
case github.ReleasePayload:
|
||||||
pl := payload.(github.ReleasePayload)
|
release := payload.(github.ReleasePayload)
|
||||||
|
// Do whatever you want from here...
|
||||||
// only want to compile on full releases
|
fmt.Printf("%+v", release)
|
||||||
if pl.Release.Draft || pl.Release.Prerelease || pl.Release.TargetCommitish != "master" {
|
|
||||||
return
|
case github.PullRequestPayload:
|
||||||
}
|
pullRequest := payload.(github.PullRequestPayload)
|
||||||
|
// Do whatever you want from here...
|
||||||
// Do whatever you want from here...
|
fmt.Printf("%+v", pullRequest)
|
||||||
fmt.Printf("%+v", pl)
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
// HandlePullRequest handles GitHub pull_request events
|
http.HandleFunc(path2, func(w http.ResponseWriter, r *http.Request) {
|
||||||
func HandlePullRequest(payload interface{}, header webhooks.Header) {
|
payload, err := hook2.Parse(r, github.ReleaseEvent, github.PullRequestEvent)
|
||||||
|
if err != nil {
|
||||||
fmt.Println("Handling Pull Request")
|
if err == github.ErrEventNotFound {
|
||||||
|
// ok event wasn;t one of the ones asked to be parsed
|
||||||
pl := payload.(github.PullRequestPayload)
|
}
|
||||||
|
}
|
||||||
// Do whatever you want from here...
|
switch payload.(type) {
|
||||||
fmt.Printf("%+v", pl)
|
|
||||||
|
case github.ReleasePayload:
|
||||||
|
release := payload.(github.ReleasePayload)
|
||||||
|
// Do whatever you want from here...
|
||||||
|
fmt.Printf("%+v", release)
|
||||||
|
|
||||||
|
case github.PullRequestPayload:
|
||||||
|
pullRequest := payload.(github.PullRequestPayload)
|
||||||
|
// Do whatever you want from here...
|
||||||
|
fmt.Printf("%+v", pullRequest)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
http.ListenAndServe(":3000", nil)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,41 +2,38 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"gopkg.in/go-playground/webhooks.v5"
|
"net/http"
|
||||||
|
|
||||||
"gopkg.in/go-playground/webhooks.v5/github"
|
"gopkg.in/go-playground/webhooks.v5/github"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
path = "/webhooks"
|
path = "/webhooks"
|
||||||
port = 3016
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
|
hook, _ := github.New(github.Options.Secret("MyGitHubSuperSecretSecrect...?"))
|
||||||
hook.RegisterEvents(HandleMultiple, github.ReleaseEvent, github.PullRequestEvent) // Add as many as you want
|
|
||||||
|
|
||||||
err := webhooks.Run(hook, ":"+strconv.Itoa(port), path)
|
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
||||||
if err != nil {
|
payload, err := hook.Parse(r, github.ReleaseEvent, github.PullRequestEvent)
|
||||||
fmt.Println(err)
|
if err != nil {
|
||||||
}
|
if err == github.ErrEventNotFound {
|
||||||
}
|
// ok event wasn;t one of the ones asked to be parsed
|
||||||
|
}
|
||||||
// HandleMultiple handles multiple GitHub events
|
}
|
||||||
func HandleMultiple(payload interface{}, header webhooks.Header) {
|
switch payload.(type) {
|
||||||
fmt.Println("Handling Payload..")
|
|
||||||
|
case github.ReleasePayload:
|
||||||
switch payload.(type) {
|
release := payload.(github.ReleasePayload)
|
||||||
|
// Do whatever you want from here...
|
||||||
case github.ReleasePayload:
|
fmt.Printf("%+v", release)
|
||||||
release := payload.(github.ReleasePayload)
|
|
||||||
// Do whatever you want from here...
|
case github.PullRequestPayload:
|
||||||
fmt.Printf("%+v", release)
|
pullRequest := payload.(github.PullRequestPayload)
|
||||||
|
// Do whatever you want from here...
|
||||||
case github.PullRequestPayload:
|
fmt.Printf("%+v", pullRequest)
|
||||||
pullRequest := payload.(github.PullRequestPayload)
|
}
|
||||||
// Do whatever you want from here...
|
})
|
||||||
fmt.Printf("%+v", pullRequest)
|
http.ListenAndServe(":3000", nil)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user