/
event.go
139 lines (122 loc) · 3.43 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
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
130
131
132
133
134
135
136
137
138
139
package webhook
import (
"encoding/json"
"log"
"strconv"
"github.com/NeoJRotary/GCB-bridge/app"
"github.com/NeoJRotary/GCB-bridge/github"
"github.com/NeoJRotary/GCB-bridge/trigger"
D "github.com/NeoJRotary/describe-go"
)
type pullRequest struct {
Head pullRequestRef `json:"head"`
Base pullRequestRef `json:"base"`
}
type pullRequestEvent struct {
Action string `json:"action"`
Number int `json:"number"`
PullRequest pullRequest `json:"pull_request"`
Repository repository `json:"repository"`
Installation installation `json:"installation"`
}
type pushEvent struct {
Ref string `json:"ref"`
Before string `json:"before"`
After string `json:"after"`
Created bool `json:"created"`
Deleted bool `json:"deleted"`
Repository repository `json:"repository"`
Installation installation `json:"installation"`
}
type repository struct {
ID int `json:"id"`
NodeID string `json:"node_id"`
FullName string `json:"full_name"`
}
type installation struct {
ID int `json:"id"`
}
type pullRequestRef struct {
Ref string `json:"ref"`
SHA string `json:"sha"`
}
func eventHandler(evtName string, body []byte) {
switch evtName {
case "push":
pushEventHandler(body)
case "pull_request":
pullRequestEventHandler(body)
default:
}
}
func pushEventHandler(body []byte) {
var payload pushEvent
err := json.Unmarshal(body, &payload)
if D.IsErr(err) {
log.Println("PushEvent Unmarshal Error", err)
return
}
// dont handle delete
if payload.Deleted {
return
}
refD := D.String(payload.Ref)
switch true {
// push to branch
case refD.HasPrefix("refs/heads/"):
repo := app.Repo{
Event: "Branch",
InstallationID: strconv.Itoa(payload.Installation.ID),
RepositoryNodeID: payload.Repository.NodeID,
FullName: payload.Repository.FullName,
Branch: refD.TrimPrefix("refs/heads/").Get(),
BeforeSHA: payload.Before,
AfterSHA: payload.After,
}
PRList, err := github.GetAssociatedPullRequests(repo.InstallationID, repo.FullName, repo.Branch)
if !D.IsErr(err) {
ss := D.StringSlice()
for _, pr := range PRList {
ss.Push(pr.BaseRefName)
}
repo.AssociatedBases = ss.Get()
}
trigger.EventHandler(&repo)
case refD.HasPrefix("refs/tags/"):
repo := app.Repo{
Event: "Tag",
InstallationID: strconv.Itoa(payload.Installation.ID),
RepositoryNodeID: payload.Repository.NodeID,
FullName: payload.Repository.FullName,
Tag: refD.TrimPrefix("refs/tags/").Get(),
BeforeSHA: payload.Before,
AfterSHA: payload.After,
}
trigger.EventHandler(&repo)
default:
log.Println("Invalid Ref", payload.Ref)
}
}
func pullRequestEventHandler(body []byte) {
var payload pullRequestEvent
err := json.Unmarshal(body, &payload)
if D.IsErr(err) {
log.Println("PullRequestEven Unmarshal Error", err)
return
}
// only handle PR opened
if payload.Action != "opened" {
return
}
repo := app.Repo{
Event: "PullRequest",
InstallationID: strconv.Itoa(payload.Installation.ID),
RepositoryNodeID: payload.Repository.NodeID,
FullName: payload.Repository.FullName,
Branch: payload.PullRequest.Head.Ref,
BaseBranch: payload.PullRequest.Base.Ref,
BeforeSHA: payload.PullRequest.Base.SHA,
AfterSHA: payload.PullRequest.Head.SHA,
}
trigger.EventHandler(&repo)
}