Skip to content

Commit 206a8b4

Browse files
author
Dean Karn
committed
cleanup examples and README
1 parent 8bd2d15 commit 206a8b4

File tree

5 files changed

+98
-221
lines changed

5 files changed

+98
-221
lines changed

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ before_script:
2626
- go get -t ./...
2727

2828
script:
29-
- make lint
3029
- make test
3130

3231
after_success: |

README.md

+24-87
Original file line numberDiff line numberDiff line change
@@ -37,110 +37,47 @@ Usage and Documentation
3737
Please see http://godoc.org/gopkg.in/go-playground/webhooks.v5 for detailed usage docs.
3838

3939
##### Examples:
40-
41-
Multiple Handlers for each event you subscribe to
4240
```go
4341
package main
4442

4543
import (
4644
"fmt"
47-
"strconv"
48-
49-
"gopkg.in/go-playground/webhooks.v5"
50-
"gopkg.in/go-playground/webhooks.v5/github"
51-
)
52-
53-
const (
54-
path = "/webhooks"
55-
port = 3016
56-
)
57-
58-
func main() {
59-
60-
hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
61-
hook.RegisterEvents(HandleRelease, github.ReleaseEvent)
62-
hook.RegisterEvents(HandlePullRequest, github.PullRequestEvent)
63-
64-
err := webhooks.Run(hook, ":"+strconv.Itoa(port), path)
65-
if err != nil {
66-
fmt.Println(err)
67-
}
68-
}
69-
70-
// HandleRelease handles GitHub release events
71-
func HandleRelease(payload interface{}, header webhooks.Header) {
72-
73-
fmt.Println("Handling Release")
74-
75-
pl := payload.(github.ReleasePayload)
76-
77-
// only want to compile on full releases
78-
if pl.Release.Draft || pl.Release.Prerelease || pl.Release.TargetCommitish != "master" {
79-
return
80-
}
81-
82-
// Do whatever you want from here...
83-
fmt.Printf("%+v", pl)
84-
}
8545

86-
// HandlePullRequest handles GitHub pull_request events
87-
func HandlePullRequest(payload interface{}, header webhooks.Header) {
46+
"net/http"
8847

89-
fmt.Println("Handling Pull Request")
90-
91-
pl := payload.(github.PullRequestPayload)
92-
93-
// Do whatever you want from here...
94-
fmt.Printf("%+v", pl)
95-
}
96-
```
97-
98-
Single receiver for events you subscribe to
99-
```go
100-
package main
101-
102-
import (
103-
"fmt"
104-
"strconv"
105-
106-
"gopkg.in/go-playground/webhooks.v5"
10748
"gopkg.in/go-playground/webhooks.v5/github"
10849
)
10950

11051
const (
11152
path = "/webhooks"
112-
port = 3016
11353
)
11454

11555
func main() {
116-
117-
hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
118-
hook.RegisterEvents(HandleMultiple, github.ReleaseEvent, github.PullRequestEvent) // Add as many as you want
119-
120-
err := webhooks.Run(hook, ":"+strconv.Itoa(port), path)
121-
if err != nil {
122-
fmt.Println(err)
123-
}
56+
hook, _ := github.New(github.Options.Secret("MyGitHubSuperSecretSecrect...?"))
57+
58+
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
59+
payload, err := hook.Parse(r, github.ReleaseEvent, github.PullRequestEvent)
60+
if err != nil {
61+
if err == github.ErrEventNotFound {
62+
// ok event wasn;t one of the ones asked to be parsed
63+
}
64+
}
65+
switch payload.(type) {
66+
67+
case github.ReleasePayload:
68+
release := payload.(github.ReleasePayload)
69+
// Do whatever you want from here...
70+
fmt.Printf("%+v", release)
71+
72+
case github.PullRequestPayload:
73+
pullRequest := payload.(github.PullRequestPayload)
74+
// Do whatever you want from here...
75+
fmt.Printf("%+v", pullRequest)
76+
}
77+
})
78+
http.ListenAndServe(":3000", nil)
12479
}
12580

126-
// HandleMultiple handles multiple GitHub events
127-
func HandleMultiple(payload interface{}, header webhooks.Header) {
128-
129-
fmt.Println("Handling Payload..")
130-
131-
switch payload.(type) {
132-
133-
case github.ReleasePayload:
134-
release := payload.(github.ReleasePayload)
135-
// Do whatever you want from here...
136-
fmt.Printf("%+v", release)
137-
138-
case github.PullRequestPayload:
139-
pullRequest := payload.(github.PullRequestPayload)
140-
// Do whatever you want from here...
141-
fmt.Printf("%+v", pullRequest)
142-
}
143-
}
14481
```
14582

14683
Contributing

_examples/custom-logger/main.go

-67
This file was deleted.

_examples/multiple-handlers/main.go

+49-38
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,61 @@ package main
22

33
import (
44
"fmt"
5-
"strconv"
65

7-
"gopkg.in/go-playground/webhooks.v5"
6+
"net/http"
7+
88
"gopkg.in/go-playground/webhooks.v5/github"
99
)
1010

1111
const (
12-
path = "/webhooks"
13-
port = 3016
12+
path1 = "/webhooks1"
13+
path2 = "/webhooks2"
1414
)
1515

1616
func main() {
17-
hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
18-
hook.RegisterEvents(HandleRelease, github.ReleaseEvent)
19-
hook.RegisterEvents(HandlePullRequest, github.PullRequestEvent)
20-
21-
err := webhooks.Run(hook, ":"+strconv.Itoa(port), path)
22-
if err != nil {
23-
fmt.Println(err)
24-
}
25-
}
26-
27-
// HandleRelease handles GitHub release events
28-
func HandleRelease(payload interface{}, header webhooks.Header) {
29-
fmt.Println("Handling Release")
30-
31-
pl := payload.(github.ReleasePayload)
32-
33-
// only want to compile on full releases
34-
if pl.Release.Draft || pl.Release.Prerelease || pl.Release.TargetCommitish != "master" {
35-
return
36-
}
37-
38-
// Do whatever you want from here...
39-
fmt.Printf("%+v", pl)
40-
}
41-
42-
// HandlePullRequest handles GitHub pull_request events
43-
func HandlePullRequest(payload interface{}, header webhooks.Header) {
44-
45-
fmt.Println("Handling Pull Request")
46-
47-
pl := payload.(github.PullRequestPayload)
48-
49-
// Do whatever you want from here...
50-
fmt.Printf("%+v", pl)
17+
hook1, _ := github.New(github.Options.Secret("MyGitHubSuperSecretSecrect...?"))
18+
hook2, _ := github.New(github.Options.Secret("MyGitHubSuperSecretSecrect2...?"))
19+
20+
http.HandleFunc(path1, func(w http.ResponseWriter, r *http.Request) {
21+
payload, err := hook1.Parse(r, github.ReleaseEvent, github.PullRequestEvent)
22+
if err != nil {
23+
if err == github.ErrEventNotFound {
24+
// ok event wasn;t one of the ones asked to be parsed
25+
}
26+
}
27+
switch payload.(type) {
28+
29+
case github.ReleasePayload:
30+
release := payload.(github.ReleasePayload)
31+
// Do whatever you want from here...
32+
fmt.Printf("%+v", release)
33+
34+
case github.PullRequestPayload:
35+
pullRequest := payload.(github.PullRequestPayload)
36+
// Do whatever you want from here...
37+
fmt.Printf("%+v", pullRequest)
38+
}
39+
})
40+
41+
http.HandleFunc(path2, func(w http.ResponseWriter, r *http.Request) {
42+
payload, err := hook2.Parse(r, github.ReleaseEvent, github.PullRequestEvent)
43+
if err != nil {
44+
if err == github.ErrEventNotFound {
45+
// ok event wasn;t one of the ones asked to be parsed
46+
}
47+
}
48+
switch payload.(type) {
49+
50+
case github.ReleasePayload:
51+
release := payload.(github.ReleasePayload)
52+
// Do whatever you want from here...
53+
fmt.Printf("%+v", release)
54+
55+
case github.PullRequestPayload:
56+
pullRequest := payload.(github.PullRequestPayload)
57+
// Do whatever you want from here...
58+
fmt.Printf("%+v", pullRequest)
59+
}
60+
})
61+
http.ListenAndServe(":3000", nil)
5162
}

_examples/single-handler/main.go

+25-28
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,38 @@ package main
22

33
import (
44
"fmt"
5-
"strconv"
65

7-
"gopkg.in/go-playground/webhooks.v5"
6+
"net/http"
7+
88
"gopkg.in/go-playground/webhooks.v5/github"
99
)
1010

1111
const (
1212
path = "/webhooks"
13-
port = 3016
1413
)
1514

1615
func main() {
17-
hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
18-
hook.RegisterEvents(HandleMultiple, github.ReleaseEvent, github.PullRequestEvent) // Add as many as you want
19-
20-
err := webhooks.Run(hook, ":"+strconv.Itoa(port), path)
21-
if err != nil {
22-
fmt.Println(err)
23-
}
24-
}
25-
26-
// HandleMultiple handles multiple GitHub events
27-
func HandleMultiple(payload interface{}, header webhooks.Header) {
28-
fmt.Println("Handling Payload..")
29-
30-
switch payload.(type) {
31-
32-
case github.ReleasePayload:
33-
release := payload.(github.ReleasePayload)
34-
// Do whatever you want from here...
35-
fmt.Printf("%+v", release)
36-
37-
case github.PullRequestPayload:
38-
pullRequest := payload.(github.PullRequestPayload)
39-
// Do whatever you want from here...
40-
fmt.Printf("%+v", pullRequest)
41-
}
16+
hook, _ := github.New(github.Options.Secret("MyGitHubSuperSecretSecrect...?"))
17+
18+
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
19+
payload, err := hook.Parse(r, github.ReleaseEvent, github.PullRequestEvent)
20+
if err != nil {
21+
if err == github.ErrEventNotFound {
22+
// ok event wasn;t one of the ones asked to be parsed
23+
}
24+
}
25+
switch payload.(type) {
26+
27+
case github.ReleasePayload:
28+
release := payload.(github.ReleasePayload)
29+
// Do whatever you want from here...
30+
fmt.Printf("%+v", release)
31+
32+
case github.PullRequestPayload:
33+
pullRequest := payload.(github.PullRequestPayload)
34+
// Do whatever you want from here...
35+
fmt.Printf("%+v", pullRequest)
36+
}
37+
})
38+
http.ListenAndServe(":3000", nil)
4239
}

0 commit comments

Comments
 (0)