-
Notifications
You must be signed in to change notification settings - Fork 2
/
github-payload.go
129 lines (120 loc) · 2.83 KB
/
github-payload.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"regexp"
"strings"
)
//go:generate gojson -input=github-events/push.json -name=PushEvent -o=push_event.go
//go:generate gojson -input=github-events/release.json -name=ReleaseEvent -o=release_event.go
//go:generate gojson -input=github-events/issues.json -name=IssuesEvent -o=issues_event.go
type Payload interface {
IsMatch(Criteria) bool
Type() string
}
func parsePayload(req *http.Request) (Payload, error) {
defer req.Body.Close()
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return PushEvent{}, err
}
event := req.Header.Get("X-Github-Event")
switch event {
case "push":
pushEvent := PushEvent{}
err = json.Unmarshal(body, &pushEvent)
return pushEvent, err
case "release":
releaseEvent := ReleaseEvent{}
err = json.Unmarshal(body, &releaseEvent)
return releaseEvent, err
case "issues":
issuesEvent := IssuesEvent{}
err = json.Unmarshal(body, &issuesEvent)
return issuesEvent, err
default:
return PushEvent{}, errors.New("invalid event type: " + event)
}
}
func (e PushEvent) Type() string {
return "push"
}
func (e PushEvent) Branch() string {
if strings.HasPrefix(e.Ref, "refs/heads/") {
return e.Ref[11:]
}
return ""
}
func (e PushEvent) Tag() string {
if strings.HasPrefix(e.Ref, "refs/tags/") {
return e.Ref[10:]
}
return ""
}
func (e PushEvent) IsMatch(c Criteria) bool {
//check that types match
if e.Type() != c.Event {
return false
}
//check that owners match
if c.Owner != "" && e.Repository.Owner.Name != c.Owner {
return false
}
//check that repo names match
if c.Repository != "" && e.Repository.Name != c.Repository {
return false
}
//check that branch matches
if c.PushParams.Branch != "" {
matched, err := regexp.MatchString("refs/heads/"+c.PushParams.Branch, e.Ref)
if err != nil {
return false
}
if !matched {
return false
}
}
return true
}
func (e ReleaseEvent) Type() string {
return "release"
}
func (e ReleaseEvent) IsMatch(c Criteria) bool {
//check that types match
if e.Type() != c.Event {
return false
}
//check that owners match
if c.Owner != "" && e.Repository.Owner.Login != c.Owner {
return false
}
//check that repo names match
if c.Repository != "" && e.Repository.Name != c.Repository {
return false
}
//check that prerelease matches
if c.ReleaseParams.Prerelease != nil && *c.ReleaseParams.Prerelease != e.Release.Prerelease {
return false
}
return true
}
func (e IssuesEvent) Type() string {
return "issues"
}
func (e IssuesEvent) IsMatch(c Criteria) bool {
//check that types match
if e.Type() != c.Event {
return false
}
//check that owners match
if c.Owner != "" && e.Repository.Owner.Login != c.Owner {
return false
}
//check that repo names match
if c.Repository != "" && e.Repository.Name != c.Repository {
return false
}
return true
}