-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgittt.go
91 lines (77 loc) · 1.98 KB
/
gittt.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
package gittt
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
const ghEventHeader = "X-GitHub-Event"
type Gittt struct {
triggers map[string]trigger
conditions []condition
}
func Init() *Gittt {
return &Gittt{
triggers: make(map[string]trigger),
conditions: make([]condition, 0),
}
}
func (g *Gittt) Handler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
event := r.Header.Get(ghEventHeader)
t, enabled := g.triggers[event]
if !enabled {
log.Printf("Invalid Event: %s\n", event)
http.Error(w, "Invalid Event", http.StatusBadRequest)
return
}
data, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println("Invalid payload")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer r.Body.Close()
go t(g, data)
}
func (g *Gittt) ListenForEvents(eventTypes ...string) error {
currentTriggers := g.triggers
for _, event := range eventTypes {
if t, exist := availableTriggers[event]; exist {
g.triggers[event] = t
continue
}
g.triggers = currentTriggers
return fmt.Errorf(`event "%s" is not available`, event)
}
return nil
}
func (g *Gittt) ListenAllEvents() {
for event, t := range availableTriggers {
g.triggers[event] = t
}
}
func (g *Gittt) ConditionBuilder(onEvent EventType, conditionFunc func(data interface{}, args ...interface{}) bool, args ...interface{}) condition {
return condition{
event: onEvent,
arg: args,
evalFunc: conditionFunc,
}
}
func (g *Gittt) ActionBuilder(actionFunc func(data interface{}, args ...interface{}), args ...interface{}) action {
return action{
args: args,
actionFunc: actionFunc,
}
}
func (g *Gittt) AddConditions(conditions ...condition) {
g.conditions = append(g.conditions, conditions...)
}
func (g *Gittt) matchConditionals(event EventType, data interface{}) (actions []action) {
for _, c := range g.conditions {
if (c.event == event || c.event == AnyEvent) && c.evalFunc(data, c.arg...) {
actions = append(actions, c.actions...)
}
}
return actions
}