cleanup examples and README

This commit is contained in:
Dean Karn
2018-08-03 23:04:59 -07:00
parent 8bd2d158e5
commit 206a8b42fd
5 changed files with 95 additions and 218 deletions
-1
View File
@@ -26,7 +26,6 @@ before_script:
- go get -t ./... - go get -t ./...
script: script:
- make lint
- make test - make test
after_success: | after_success: |
+10 -73
View File
@@ -37,97 +37,31 @@ 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)
err := webhooks.Run(hook, ":"+strconv.Itoa(port), path)
if err != nil { if err != nil {
fmt.Println(err) 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) {
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) { switch payload.(type) {
case github.ReleasePayload: case github.ReleasePayload:
@@ -140,7 +74,10 @@ func HandleMultiple(payload interface{}, header webhooks.Header) {
// Do whatever you want from here... // Do whatever you want from here...
fmt.Printf("%+v", pullRequest) fmt.Printf("%+v", pullRequest)
} }
})
http.ListenAndServe(":3000", nil)
} }
``` ```
Contributing Contributing
-67
View File
@@ -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)
}
}
+41 -30
View File
@@ -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) {
payload, err := hook1.Parse(r, github.ReleaseEvent, github.PullRequestEvent)
if err != nil { if err != nil {
fmt.Println(err) 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) {
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
} }
switch payload.(type) {
case github.ReleasePayload:
release := payload.(github.ReleasePayload)
// Do whatever you want from here... // Do whatever you want from here...
fmt.Printf("%+v", pl) fmt.Printf("%+v", release)
}
// HandlePullRequest handles GitHub pull_request events
func HandlePullRequest(payload interface{}, header webhooks.Header) {
fmt.Println("Handling Pull Request")
pl := payload.(github.PullRequestPayload)
case github.PullRequestPayload:
pullRequest := payload.(github.PullRequestPayload)
// Do whatever you want from here... // Do whatever you want from here...
fmt.Printf("%+v", pl) 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)
} }
+10 -13
View File
@@ -2,31 +2,26 @@ 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) {
payload, err := hook.Parse(r, github.ReleaseEvent, github.PullRequestEvent)
if err != nil { if err != nil {
fmt.Println(err) 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) {
fmt.Println("Handling Payload..")
switch payload.(type) { switch payload.(type) {
case github.ReleasePayload: case github.ReleasePayload:
@@ -39,4 +34,6 @@ func HandleMultiple(payload interface{}, header webhooks.Header) {
// Do whatever you want from here... // Do whatever you want from here...
fmt.Printf("%+v", pullRequest) fmt.Printf("%+v", pullRequest)
} }
})
http.ListenAndServe(":3000", nil)
} }