forked from ckingdev/githubbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
202 lines (195 loc) · 5.44 KB
/
github.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package githubbot
import (
"fmt"
"strconv"
"time"
"github.com/cpalone/gohook"
)
func (s *Session) hookServer(port int, secret string, sendReplyChan chan PacketEvent) {
// spin off github server
gServer := gohook.NewServer(port, secret, "/postreceive")
s.logger.Info("Starting github server...")
gServer.GoListenAndServe()
// Wait for github hook event
for {
et := <-gServer.EventAndTypes
s.logger.Infof("Received hook event of type '%s'.", et.Type)
switch et.Type {
case gohook.CommitCommentEventType:
payload, ok := et.Event.(*gohook.CommitCommentEvent)
if !ok {
panic("Malformed *CommitCommentEvent.")
}
// TODO: can we get the branch here?
msg := fmt.Sprintf("[ %s ] Comment on commit: %s (%s)",
payload.Repository.Name,
payload.Comment.Body,
payload.Comment.HTMLURL,
)
s.sendMessage(msg, "", strconv.Itoa(s.msgID))
s.msgID++
case gohook.CreateEventType:
payload, ok := et.Event.(*gohook.CreateEvent)
if !ok {
panic("Malformed *CreateEvent.")
}
msg := fmt.Sprintf("[ %s | Branch/Tag: %s] Created.",
payload.Repository.Name,
payload.RefType,
)
s.sendMessage(msg, "", strconv.Itoa(s.msgID))
s.msgID++
case gohook.DeleteEventType:
payload, ok := et.Event.(*gohook.DeleteEvent)
if !ok {
panic("Malformed *DeleteEvent.")
}
msg := fmt.Sprintf("[ %s | Branch/Tag: %s] Deleted.",
payload.Repository,
payload.RefType,
)
s.sendMessage(msg, "", strconv.Itoa(s.msgID))
s.msgID++
case gohook.IssueCommentEventType:
payload, ok := et.Event.(*gohook.IssueCommentEvent)
if !ok {
panic("Malformed *CommitCommentEvent.")
}
msg := fmt.Sprintf("[ %s | Issue: %s ] Comment: %s (%s)",
payload.Repository.Name,
payload.Issue.Title,
payload.Comment.Body,
payload.Comment.HTMLURL,
)
s.sendMessage(msg, "", strconv.Itoa(s.msgID))
s.msgID++
case gohook.IssuesEventType:
payload, ok := et.Event.(*gohook.IssuesEvent)
if !ok {
panic("Malformed *IssuesEvent.")
}
msg := fmt.Sprintf("[ %s | Issue: %s ] Action: %s. (%s)",
payload.Repository.Name,
payload.Issue.Title,
payload.Action,
payload.Issue.HTMLURL,
)
s.sendMessage(msg, "", strconv.Itoa(s.msgID))
s.msgID++
case gohook.PullRequestEventType:
payload, ok := et.Event.(*gohook.PullRequestEvent)
if !ok {
panic("Malformed *PullRequestEvent.")
}
action := payload.Action
if action == "synchronize" {
action = "New commits made to synced branch."
}
msg := fmt.Sprintf(":pencil: [ %s | PR: %s ] %s (%s)",
payload.Repository.Name,
payload.PullRequest.Title,
action,
payload.PullRequest.HTMLURL,
)
s.msgID++
t := strconv.Itoa(int(time.Now().Unix()))
s.waiting = true
s.sendMessage(msg, "", t)
var reply PacketEvent
for s.waiting {
reply = <-sendReplyChan
if reply.ID == t {
s.waiting = false
}
}
srPayload, err := reply.Payload()
if err != nil {
s.logger.Fatalln(err)
}
// need send-reply for msgID to reply to
data, ok := srPayload.(*SendReply)
if !ok {
s.logger.Fatalln("Could not assert *SendReply as such.")
}
s.commitParent[payload.PullRequest.Head.SHA] = data.ID
case gohook.PullRequestReviewCommentEventType:
payload, ok := et.Event.(*gohook.PullRequestReviewCommentEvent)
if !ok {
panic("Malformed *PullRequestReviewCommentEvent.")
}
msg := fmt.Sprintf(":speech_balloon: [ %s | PR: %s ] Comment: %s: %s (%s)",
payload.Repository.Name,
payload.PullRequest.Title,
payload.Sender.Login,
payload.Comment.Body,
payload.PullRequest.HTMLURL,
)
s.sendMessage(msg, "", strconv.Itoa(s.msgID))
s.msgID++
case gohook.RepositoryEventType:
payload, ok := et.Event.(*gohook.RepositoryEvent)
if !ok {
panic("Malformed *RepositoryEvent.")
}
msg := fmt.Sprintf("[ Repository: %s ] Action: created. (%s) ",
payload.Repository.Name,
payload.Repository.HTMLURL,
)
s.sendMessage(msg, "", strconv.Itoa(s.msgID))
s.msgID++
case gohook.PushEventType:
s.logger.Info("Entering PushEventType case.")
payload, ok := et.Event.(*gohook.PushEvent)
if !ok {
panic("Malformed *PushEvent.")
}
if payload.HeadCommit.Message == "" {
continue
}
var msg string
if len(payload.Commits) > 1 {
msg = fmt.Sprintf(":repeat: [ %s | Branch: %s ] %v Commits: %s (%s)",
payload.Repository.Name,
payload.Ref[11:], // this discards "refs/heads/"
len(payload.Commits),
payload.HeadCommit.Message,
payload.Compare,
)
} else {
msg = fmt.Sprintf(":repeat: [ %s | Branch: %s ] Commit: %s (%s)",
payload.Repository.Name,
payload.Ref[11:], // this discards "refs/heads/"
payload.HeadCommit.Message,
payload.HeadCommit.URL,
)
}
t := strconv.Itoa(int(time.Now().Unix()))
s.waiting = true
s.sendMessage(msg, "", t)
var reply PacketEvent
for s.waiting {
reply = <-sendReplyChan
if reply.ID == t {
s.waiting = false
}
}
srPayload, err := reply.Payload()
if err != nil {
s.logger.Fatalln(err)
}
// need send-reply for msgID to reply to
data, ok := srPayload.(*SendReply)
if !ok {
s.logger.Fatalln("Could not assert *SendReply as such.")
}
s.commitParent[payload.HeadCommit.ID] = data.ID
case gohook.PingEventType:
//payload, ok := et.Event.(*gohook.PingEvent)
//if !ok {
// panic("Malformed *PingEvent")
//}
s.sendMessage("[ Github Webhook | ping ]", "", strconv.Itoa(s.msgID))
s.msgID++
}
}
}