Skip to content

Commit 24cb1f6

Browse files
joeybloggsjoeybloggs
authored andcommitted
Add Examples + Complete README
1 parent 9af9035 commit 24cb1f6

File tree

3 files changed

+245
-2
lines changed

3 files changed

+245
-2
lines changed

README.md

Lines changed: 150 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,154 @@ Library webhooks
33

44
[![GoDoc](https://godoc.org/github.com/joeybloggs/webhooks?status.svg)](https://godoc.org/github.com/joeybloggs/webhooks)
55

6-
Source control webhook reciever for GitHub, others to come, with complete event payload parsing
6+
Library webhooks allows for easy recieving and parsing of GitHub Webhook Events; more services to come i.e. BitBucket...
77

8-
### In Development
8+
Features:
9+
10+
* Parses the entire payload, not just a few fields.
11+
* Fields + Schema directly lines up with webhook posted json
12+
13+
Notes:
14+
15+
* Github - Currently only accepting json payloads.
16+
17+
Installation
18+
------------
19+
20+
Use go get.
21+
22+
go get github.com/joeybloggs/webhooks
23+
24+
or to update
25+
26+
go get -u github.com/joeybloggs/webhooks
27+
28+
Then import the validator package into your own code.
29+
30+
import "github.com/joeybloggs/webhooks"
31+
32+
Usage and documentation
33+
------
34+
35+
Please see http://godoc.org/github.com/joeybloggs/webhooks for detailed usage docs.
36+
37+
##### Examples:
38+
39+
Multiple Handlers for each event you subscribe to
40+
```go
41+
package main
42+
43+
import (
44+
"fmt"
45+
"strconv"
46+
47+
"github.com/joeybloggs/webhooks"
48+
"github.com/joeybloggs/webhooks/github"
49+
)
50+
51+
const (
52+
path = "/webhooks"
53+
port = 80
54+
)
55+
56+
func main() {
57+
hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
58+
hook.RegisterEvents(HandleRelease, github.ReleaseEvent)
59+
hook.RegisterEvents(HandlePullRequest, github.PullRequestEvent)
60+
61+
err := webhooks.Run(hook, ":"+strconv.Itoa(port), path)
62+
if err != nil {
63+
fmt.Println(err)
64+
}
65+
}
66+
67+
// HandleRelease handles GitHub release events
68+
func HandleRelease(payload interface{}) {
69+
70+
fmt.Println("Handling Release")
71+
72+
pl := payload.(github.ReleasePayload)
73+
74+
// only want to compile on full releases
75+
if pl.Release.Draft || pl.Release.Prelelease || pl.Release.TargetCommitish != "master" {
76+
return
77+
}
78+
79+
// Do whatever you want from here...
80+
fmt.Printf("%+v", pl)
81+
}
82+
83+
// HandlePullRequest handles GitHub pull_request events
84+
func HandlePullRequest(payload interface{}) {
85+
86+
fmt.Println("Handling Pull Request")
87+
88+
pl := payload.(github.PullRequestPayload)
89+
90+
// Do whatever you want from here...
91+
fmt.Printf("%+v", pl)
92+
}
93+
```
94+
95+
Single receiver for events you subscribe to
96+
```go
97+
package main
98+
99+
import (
100+
"fmt"
101+
"strconv"
102+
103+
"github.com/joeybloggs/webhooks"
104+
"github.com/joeybloggs/webhooks/github"
105+
)
106+
107+
const (
108+
path = "/webhooks"
109+
port = 80
110+
)
111+
112+
func main() {
113+
hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
114+
hook.RegisterEvents(HandleMultiple, github.ReleaseEvent, github.PullRequestEvent) // Add as many as you want
115+
116+
err := webhooks.Run(hook, ":"+strconv.Itoa(port), path)
117+
if err != nil {
118+
fmt.Println(err)
119+
}
120+
}
121+
122+
// HandleMultiple handles multiple GitHub events
123+
func HandleMultiple(payload interface{}) {
124+
125+
fmt.Println("Handling Payload..")
126+
127+
switch payload.(type) {
128+
129+
case github.ReleasePayload:
130+
release := payload.(github.ReleasePayload)
131+
// Do whatever you want from here...
132+
fmt.Printf("%+v", release)
133+
134+
case github.PullRequestPayload:
135+
pullRequest := payload.(github.PullRequestPayload)
136+
// Do whatever you want from here...
137+
fmt.Printf("%+v", pullRequest)
138+
}
139+
}
140+
```
141+
142+
Contributing
143+
------
144+
145+
Pull requests for other service like BitBucket are welcome!
146+
147+
There will always be a development branch for each version i.e. `v1-development`. In order to contribute,
148+
please make your pull requests against those branches.
149+
150+
If the changes being proposed or requested are breaking changes, please create an issue, for discussion
151+
or create a pull request against the highest development branch for example this package has a
152+
v1 and v1-development branch however, there will also be a v2-development branch even though v2 doesn't exist yet.
153+
154+
License
155+
------
156+
Distributed under MIT License, please see license file in code for more details.

examples/multiple-handlers/main.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
7+
"github.com/joeybloggs/webhooks"
8+
"github.com/joeybloggs/webhooks/github"
9+
)
10+
11+
const (
12+
path = "/webhooks"
13+
port = 80
14+
)
15+
16+
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{}) {
29+
30+
fmt.Println("Handling Release")
31+
32+
pl := payload.(github.ReleasePayload)
33+
34+
// only want to compile on full releases
35+
if pl.Release.Draft || pl.Release.Prelelease || pl.Release.TargetCommitish != "master" {
36+
return
37+
}
38+
39+
// Do whatever you want from here...
40+
fmt.Printf("%+v", pl)
41+
}
42+
43+
// HandlePullRequest handles GitHub pull_request events
44+
func HandlePullRequest(payload interface{}) {
45+
46+
fmt.Println("Handling Pull Request")
47+
48+
pl := payload.(github.PullRequestPayload)
49+
50+
// Do whatever you want from here...
51+
fmt.Printf("%+v", pl)
52+
}

examples/single-handler/main.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
7+
"github.com/joeybloggs/webhooks"
8+
"github.com/joeybloggs/webhooks/github"
9+
)
10+
11+
const (
12+
path = "/webhooks"
13+
port = 80
14+
)
15+
16+
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{}) {
28+
29+
fmt.Println("Handling Payload..")
30+
31+
switch payload.(type) {
32+
33+
case github.ReleasePayload:
34+
release := payload.(github.ReleasePayload)
35+
// Do whatever you want from here...
36+
fmt.Printf("%+v", release)
37+
38+
case github.PullRequestPayload:
39+
pullRequest := payload.(github.PullRequestPayload)
40+
// Do whatever you want from here...
41+
fmt.Printf("%+v", pullRequest)
42+
}
43+
}

0 commit comments

Comments
 (0)