From 206a8b42fd268e38840b874be54de8f0987086cd Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Fri, 3 Aug 2018 23:04:59 -0700 Subject: [PATCH] cleanup examples and README --- .travis.yml | 1 - README.md | 111 ++++++---------------------- _examples/custom-logger/main.go | 67 ----------------- _examples/multiple-handlers/main.go | 87 ++++++++++++---------- _examples/single-handler/main.go | 53 +++++++------ 5 files changed, 98 insertions(+), 221 deletions(-) delete mode 100644 _examples/custom-logger/main.go diff --git a/.travis.yml b/.travis.yml index 85b0687..b489161 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,7 +26,6 @@ before_script: - go get -t ./... script: - - make lint - make test after_success: | diff --git a/README.md b/README.md index 870da95..37a4776 100644 --- a/README.md +++ b/README.md @@ -37,110 +37,47 @@ Usage and Documentation Please see http://godoc.org/gopkg.in/go-playground/webhooks.v5 for detailed usage docs. ##### Examples: - -Multiple Handlers for each event 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(HandleRelease, github.ReleaseEvent) - hook.RegisterEvents(HandlePullRequest, github.PullRequestEvent) - - err := webhooks.Run(hook, ":"+strconv.Itoa(port), path) - if err != nil { - fmt.Println(err) - } -} - -// 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) { + "net/http" - 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) - } + hook, _ := github.New(github.Options.Secret("MyGitHubSuperSecretSecrect...?")) + + http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { + payload, err := hook.Parse(r, github.ReleaseEvent, github.PullRequestEvent) + if err != nil { + if err == github.ErrEventNotFound { + // ok event wasn;t one of the ones asked to be parsed + } + } + 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) + } + }) + http.ListenAndServe(":3000", nil) } -// 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 diff --git a/_examples/custom-logger/main.go b/_examples/custom-logger/main.go deleted file mode 100644 index f15a819..0000000 --- a/_examples/custom-logger/main.go +++ /dev/null @@ -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) - } -} diff --git a/_examples/multiple-handlers/main.go b/_examples/multiple-handlers/main.go index d96e761..136deb6 100644 --- a/_examples/multiple-handlers/main.go +++ b/_examples/multiple-handlers/main.go @@ -2,50 +2,61 @@ package main import ( "fmt" - "strconv" - "gopkg.in/go-playground/webhooks.v5" + "net/http" + "gopkg.in/go-playground/webhooks.v5/github" ) const ( - path = "/webhooks" - port = 3016 + path1 = "/webhooks1" + path2 = "/webhooks2" ) func main() { - hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"}) - hook.RegisterEvents(HandleRelease, github.ReleaseEvent) - hook.RegisterEvents(HandlePullRequest, github.PullRequestEvent) - - err := webhooks.Run(hook, ":"+strconv.Itoa(port), path) - if err != nil { - fmt.Println(err) - } -} - -// 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) + hook1, _ := github.New(github.Options.Secret("MyGitHubSuperSecretSecrect...?")) + hook2, _ := github.New(github.Options.Secret("MyGitHubSuperSecretSecrect2...?")) + + http.HandleFunc(path1, func(w http.ResponseWriter, r *http.Request) { + payload, err := hook1.Parse(r, github.ReleaseEvent, github.PullRequestEvent) + if err != nil { + if err == github.ErrEventNotFound { + // ok event wasn;t one of the ones asked to be parsed + } + } + 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) + } + }) + + http.HandleFunc(path2, func(w http.ResponseWriter, r *http.Request) { + payload, err := hook2.Parse(r, github.ReleaseEvent, github.PullRequestEvent) + if err != nil { + if err == github.ErrEventNotFound { + // ok event wasn;t one of the ones asked to be parsed + } + } + 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) + } + }) + http.ListenAndServe(":3000", nil) } diff --git a/_examples/single-handler/main.go b/_examples/single-handler/main.go index 72b7c69..372b08c 100644 --- a/_examples/single-handler/main.go +++ b/_examples/single-handler/main.go @@ -2,41 +2,38 @@ package main import ( "fmt" - "strconv" - "gopkg.in/go-playground/webhooks.v5" + "net/http" + "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) - } + hook, _ := github.New(github.Options.Secret("MyGitHubSuperSecretSecrect...?")) + + http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { + payload, err := hook.Parse(r, github.ReleaseEvent, github.PullRequestEvent) + if err != nil { + if err == github.ErrEventNotFound { + // ok event wasn;t one of the ones asked to be parsed + } + } + 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) + } + }) + http.ListenAndServe(":3000", nil) }