Skip to content

Commit

Permalink
cleanup examples and README
Browse files Browse the repository at this point in the history
  • Loading branch information
deankarn committed Aug 4, 2018
1 parent 8bd2d15 commit 206a8b4
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 221 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ before_script:
- go get -t ./...

script:
- make lint
- make test

after_success: |
Expand Down
111 changes: 24 additions & 87 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 0 additions & 67 deletions _examples/custom-logger/main.go

This file was deleted.

87 changes: 49 additions & 38 deletions _examples/multiple-handlers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
53 changes: 25 additions & 28 deletions _examples/single-handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 206a8b4

Please sign in to comment.