@@ -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.
0 commit comments