-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathevent.go
95 lines (83 loc) · 2.08 KB
/
event.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
package main
import (
"encoding/json"
"github.com/dimfeld/glog"
)
type Event map[string]interface{}
func (e Event) Commits() []interface{} {
generic, ok := e["commits"]
if !ok {
if glog.V(1) {
glog.Infoln("Event had no commits")
}
return nil
}
interfaceList, ok := generic.([]interface{})
if !ok {
glog.Errorf("Commit list had type %T\n", generic)
return nil
}
return interfaceList
}
// The docs I saw were outdated. There's no need for this since the formats are actually
// the same.
/*
// Normalize makes GitLab events look like GitHub events.
func (e Event) normalize() {
// Fortunately there's very little difference, at least in the push format.
for gitlabName, githubName := range eventTranslate {
value, ok := e[gitlabName]
if ok {
e[githubName] = value
}
}
commits := e.Commits()
if commits != nil {
for _, generic := range commits {
c, ok := generic.(map[string]interface{})
if !ok {
glog.Errorf("Commit had type %T", generic)
}
for gitlabName, githubName := range commitTranslate {
value, ok := c[gitlabName]
if ok {
c[githubName] = value
}
}
}
}
}
*/
// Create a new event from the given JSON. If the event type is blank,
// this function will try to figure it out. Generally, GitHub events will
// present a value for this in the HTTP Request, and GitLab events place
// the event type in the JSON.
func NewEvent(jsonData []byte, eventName string) (Event, error) {
e := Event{}
err := json.Unmarshal(jsonData, &e)
if err != nil {
return nil, err
}
if payload, ok := e["object_attributes"].(map[string]interface{}); ok {
// For GitLab events, export all object_attributes fields into the event scope.
for key, value := range payload {
e[key] = value
}
}
if glog.V(3) {
glog.Infof("Event: %v", e)
}
if eventName == "" {
var gitlabType string
gitlabType, ok := e["object_kind"].(string)
if !ok {
// Push events look completely different from the other events and
// don't have an explicit type.
gitlabType = "push"
}
e["type"] = gitlabType
} else {
e["type"] = eventName
}
return e, nil
}