forked from blindside-io/octohooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
40 lines (32 loc) · 850 Bytes
/
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
package octohooks
import (
"bytes"
"encoding/json"
"net/http"
"github.com/google/go-github/github"
)
// Event contains the basic information about an event that happened
type Event struct {
// Name of the event (issue, pull request, commit, etc...)
Name string
EventDetail interface{}
Err error
}
// NewEventFromRequestAndBody constructs an event from a github webhook request
func NewEventFromRequestAndBody(r *http.Request, body []byte) Event {
e := Event{}
e.Name = r.Header.Get("X-Github-Event")
var eventDetail interface{}
switch e.Name {
case "pull_request":
eventDetail = &github.PullRequestEvent{}
case "push":
eventDetail = &github.PushEvent{}
}
if err := json.NewDecoder(bytes.NewBuffer(body)).Decode(eventDetail); err != nil {
e.Err = err
return e
}
e.EventDetail = eventDetail
return e
}