-
Notifications
You must be signed in to change notification settings - Fork 17
/
webhooks.go
96 lines (76 loc) · 1.91 KB
/
webhooks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package webhooks
import (
"fmt"
"log"
"net/http"
"reflect"
"github.com/gorilla/mux"
"github.com/checkr/codeflow/server/agent"
"github.com/checkr/codeflow/server/plugins/webhooks/github"
)
type Webhook interface {
Register(router *mux.Router, events chan agent.Event)
}
func init() {
agent.RegisterPlugin("webhooks", func() agent.Plugin { return NewWebhooks() })
}
type Webhooks struct {
ServiceAddress string `mapstructure:"service_address"`
Github *github.GithubWebhook
}
func NewWebhooks() *Webhooks {
return &Webhooks{}
}
func (wb *Webhooks) SampleConfig() string {
return `
## Address and port to host Webhook listener on
service_address: ":1619"
github:
path: "/github"
`
}
func (wb *Webhooks) Description() string {
return "A Webhooks Event collector"
}
func (wb *Webhooks) Listen(events chan agent.Event) {
r := mux.NewRouter()
for _, webhook := range wb.AvailableWebhooks() {
webhook.Register(r, events)
}
err := http.ListenAndServe(fmt.Sprintf("%s", wb.ServiceAddress), r)
if err != nil {
log.Printf("Error starting server: %v", err)
}
}
// Looks for fields which implement Webhook interface
func (wb *Webhooks) AvailableWebhooks() []Webhook {
webhooks := make([]Webhook, 0)
s := reflect.ValueOf(wb).Elem()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
if !f.CanInterface() {
continue
}
if wbPlugin, ok := f.Interface().(Webhook); ok {
if !reflect.ValueOf(wbPlugin).IsNil() {
webhooks = append(webhooks, wbPlugin)
}
}
}
return webhooks
}
func (wb *Webhooks) Start(events chan agent.Event) error {
go wb.Listen(events)
log.Printf("Started the webhooks service on %s\n", wb.ServiceAddress)
return nil
}
func (rb *Webhooks) Stop() {
log.Println("Stopping the Webhooks service")
}
func (rb *Webhooks) Subscribe() []string {
return []string{}
}
func (rb *Webhooks) Process(e agent.Event) error {
log.Printf("Process Webhook event: %s", e.Name)
return nil
}