forked from julianec/justanotherircbot
/
github.go
406 lines (368 loc) · 11.1 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package main
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"encoding/json"
"hash"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
)
type GitHubAdapter struct {
msgbuffer *MessageBuffer
config *GitHubConfig
channels map[string]*GitHubRepositoryConfig
}
// Every GithubEvent has a method Strings that returns a slice of string with the text for the irc PRIVMSG.
type GithubEvent interface {
Strings() []string
GetRepository() string
}
type GithubCreate struct {
Ref string
RefType string `json:"ref_type"`
MasterBranch string
Description string
PusherType string
Repository GithubRepository
Sender GithubUser
}
type GithubDelete struct {
Ref string
RefType string `json:"ref_type"`
PusherType string
Repository GithubRepository
Sender GithubUser
}
type GithubPush struct {
Ref string
After string
Before string
Created bool
Deleted bool
Forced bool
Compare string
Commits []GithubCommit
HeadCommit GithubCommit `json:"head_commit"`
Repository GithubRepository
Pusher GithubUser
}
type GithubCommit struct {
Id string
Distinct bool
Message string
Timestamp string
Url string
Author GithubUser
Committer GithubUser
Added []string
Removed []string
Modified []string
}
type GithubIssuesEvent struct {
Action string
Issue GithubIssue
Repository GithubRepository
Sender GithubUser
Label GithubIssueLabel
}
type GithubIssue struct {
Url string
LabelsUrl string `json:"labels_url"`
CommentsUrl string `json:"comments_url"`
EventsUrl string `json:"events_url"`
HtmlUrl string `json:"html_url"`
Id int
Number int
Title string
User GithubUser
Labels []GithubIssueLabel
Assignee GithubUser
}
type GithubIssueLabel struct {
Url string
Name string
Color string
}
type GithubRepository struct {
Id uint64
Name string
FullName string `json:"full_name"`
Owner GithubUser
Private bool
HtmlUrl string
Description string
Fork bool
Url string
ForksUrl string
KeysUrl string
CollaboratorsUrl string
TeamsUrl string
HooksUrl string
IssueEventsUrl string
EventsUrl string
AssigneesUrl string
BranchesUrl string
TagsUrl string
BlobsUrl string
GitTagsUrl string
GitRefsUrl string
TreesUrl string
StatusesUrl string
LanguagesUrl string
StargazersUrl string
ContributorsUrl string
SubscribersUrl string
SubscriptionUrl string
CommitsUrl string
GitCommitsUrl string
CommentsUrl string
CompareUrl string
MergesUrl string
ArchiveUrl string
DownloadsUrl string
IssuesUrl string
PullsUrl string
MilestonesUrl string
NotificationsUrl string
LabelsUrl string
ReleasesUrl string
CreatedAt string
UpdatedAt string
PushedAt string
GitUrl string
SshUrl string
CloneUrl string
SvnUrl string
Homepage string
Size uint64
StargazersCount uint64
WatchersCount uint64
Language string
HasIssues bool
HasDownloads bool
HasWiki bool
ForksCount uint64
MirrorUrl string
OpenIssuesCount uint64
Forks uint64
OpenIssues uint64
Watchers uint64
DefaultBranch string
}
type GithubUser struct {
Login string
Id uint64
AvatarURL string
GravatarId string
Url string
HtmlUrl string
FollowersUrl string
FollowingUrl string
GistsUrl string
StarredUrl string
SubscriptionsUrl string
OrganizationsUrl string
ReposUrl string
EventsUrl string
ReceivedEventsUrl string
Type string
SiteAdmin bool
Name string
Email string
}
// CheckMAC returns true if messageMAC is a valid HMAC tag for message.
func CheckMAC(message []byte, messageMAC string, key string) bool {
var err error
var mac hash.Hash
var macdata []byte
var macparts = strings.Split(messageMAC, "=")
macdata, err = hex.DecodeString(macparts[1])
if err != nil {
log.Print("Error decoding hex digest: ", err)
return false
}
switch macparts[0] {
case "md5":
mac = hmac.New(md5.New, []byte(key))
case "sha1":
mac = hmac.New(sha1.New, []byte(key))
case "sha256":
mac = hmac.New(sha256.New, []byte(key))
case "sha512":
mac = hmac.New(sha512.New, []byte(key))
default:
log.Print("Unsupported hash: ", macparts[0])
return false
}
mac.Write(message)
expectedMAC := mac.Sum(nil)
return hmac.Equal(macdata, expectedMAC)
}
func (g *GithubUser) String() string {
if len(g.Name) > 0 && len(g.Email) > 0 {
return g.Name + " <" + g.Email + ">"
} else if len(g.Name) > 0 {
return g.Name
} else if len(g.Email) > 0 {
return g.Email
}
return g.Login
}
func (g *GithubRepository) String() string {
if len(g.FullName) > 0 {
return g.FullName
}
return g.Name
}
func (g *GithubCommit) String() string {
var lines []string = strings.Split(g.Message, "\n") // Commit message
var text string
var short bool = len(g.Added)+len(g.Removed)+len(g.Modified) < 6 // true, if more than 6 files changed
text = g.Author.String() + " \x02" + g.Id[0:7] + "\x0f" // First 7 characters
if !short {
if len(g.Added) > 0 {
text += " \x0303" + strconv.Itoa(len(g.Added)) + " files added." + "\x0f"
}
if len(g.Removed) > 0 {
text += " \x0304" + strconv.Itoa(len(g.Removed)) + " files removed." + "\x0f"
}
if len(g.Modified) > 0 {
text += " \x0310" + strconv.Itoa(len(g.Modified)) + " files modified." + "\x0f"
}
} else {
if len(g.Added) > 0 {
text += " \x0303" + strings.Join(g.Added, " ") + "\x0f"
}
if len(g.Removed) > 0 {
text += " \x0304" + strings.Join(g.Removed, " ") + "\x0f"
}
if len(g.Modified) > 0 {
text += " \x0310" + strings.Join(g.Modified, " ") + "\x0f"
}
}
if len(lines) > 0 {
text += " " + lines[0]
}
return text
}
func (g *GithubIssuesEvent) Strings() []string {
switch g.Action {
case "assigned":
return []string{"\x0303" + g.Sender.String() + "\x0f has " + g.Action + " Issue " + strconv.Itoa(g.Issue.Number) + " (" + g.Issue.Title + ") to " + g.Issue.Assignee.String() + ". \x0303" + g.Repository.String() + "\x0f"}
case "unassigned":
if g.Issue.Assignee.String() != "" {
return []string{"\x0303" + g.Sender.String() + "\x0f has " + g.Action + " Issue " + strconv.Itoa(g.Issue.Number) + " (" + g.Issue.Title + ") from " + g.Issue.Assignee.String() + ". \x0303" + g.Repository.String() + "\x0f"}
} else {
return []string{"\x0303" + g.Sender.String() + "\x0f has " + g.Action + " Issue " + strconv.Itoa(g.Issue.Number) + " (" + g.Issue.Title + ") from themselves. \x0303" + g.Repository.String() + "\x0f"}
}
case "labeled":
return []string{"\x0303" + g.Sender.String() + "\x0f has " + g.Action + " Issue " + strconv.Itoa(g.Issue.Number) + " (" + g.Issue.Title + ") with label " + g.Label.Name + ". \x0303" + g.Repository.String() + "\x0f"}
case "unlabeled":
return []string{"\x0303" + g.Sender.String() + "\x0f has removed label " + g.Label.Name + " from Issue " + strconv.Itoa(g.Issue.Number) + " (" + g.Issue.Title + "). \x0303" + g.Repository.String() + "\x0f"}
case "opened":
return []string{"\x0303" + g.Sender.String() + "\x0f has " + g.Action + " Issue " + strconv.Itoa(g.Issue.Number) + " (" + g.Issue.Title + "). \x0303" + g.Repository.String() + "\x0f"}
case "closed":
return []string{"\x0303" + g.Sender.String() + "\x0f has " + g.Action + " Issue " + strconv.Itoa(g.Issue.Number) + " (" + g.Issue.Title + "). \x0303" + g.Repository.String() + "\x0f"}
case "reopened":
return []string{"\x0303" + g.Sender.String() + "\x0f has " + g.Action + " Issue " + strconv.Itoa(g.Issue.Number) + " (" + g.Issue.Title + "). \x0303" + g.Repository.String() + "\x0f"}
default:
log.Print("Unsupported Issue Event: " + g.Action)
}
return []string{"Unsupported Issue Event: " + g.Action}
}
func (g *GithubPush) Strings() []string {
var refs []string = strings.Split(g.Ref, "/")
var prefix string = "\x0303" + g.Repository.String() + "\x0f \x0305" + refs[len(refs)-1] + "\x0f"
var pushes []string = make([]string, 0)
for _, commit := range g.Commits {
pushes = append(pushes, prefix+" "+commit.String())
pushes = append(pushes, prefix+" "+commit.Url)
}
return pushes
}
func (g *GithubPush) GetRepository() string {
return g.Repository.String()
}
func (g *GithubCreate) Strings() []string {
return []string{"\x0303" + g.Sender.String() + "\x0f has pushed a new " + g.RefType + " \x0305" + g.Ref + "\x0f to \x0303" + g.Repository.String() + "\x0f"}
}
func (g *GithubCreate) GetRepository() string {
return g.Repository.String()
}
func (g *GithubDelete) Strings() []string {
return []string{"\x0303" + g.Sender.String() + "\x0f has deleted a " + g.RefType + " \x0305" + g.Ref + "\x0f from \x0303" + g.Repository.String() + "\x0f"}
}
func (g *GithubDelete) GetRepository() string {
return g.Repository.String()
}
func (g *GithubIssuesEvent) GetRepository() string {
return g.Repository.String()
}
func (g *GitHubAdapter) WriteGithubEvent(event GithubEvent, body []byte, signature string) error {
var ok bool
var githubconf *GitHubRepositoryConfig
var err error
err = json.Unmarshal(body, &event)
if err != nil {
log.Print("Error decoding github event: ", err)
return err
}
githubconf, ok = g.channels[event.GetRepository()]
if !ok {
log.Print("Repository ", event.GetRepository(), " not configured.")
return err
}
if !CheckMAC(body, signature, githubconf.GetSecret()) {
log.Print("DEBUG Signature: ", signature)
return err
}
for _, channel := range githubconf.GetIrcChannel() {
for _, commit := range event.Strings() {
g.msgbuffer.AddMessage(channel, commit)
}
}
return err
}
func NewGitHubAdapter(msgbuffer *MessageBuffer, config *GitHubConfig) *GitHubAdapter {
var channels = make(map[string]*GitHubRepositoryConfig)
for _, repo := range config.Repo {
channels[repo.GetName()] = repo
}
return &GitHubAdapter{
msgbuffer: msgbuffer,
config: config,
channels: channels,
}
}
func (g *GitHubAdapter) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
var body []byte
var err error
body, err = ioutil.ReadAll(req.Body)
if err != nil {
log.Print("Error reading body: ", err)
return
}
switch req.Header.Get("X-GitHub-Event") {
case "create":
var create GithubCreate
g.WriteGithubEvent(&create, body, req.Header.Get("X-Hub-Signature"))
case "delete":
var del GithubDelete
g.WriteGithubEvent(&del, body, req.Header.Get("X-Hub-Signature"))
case "push":
var push GithubPush
g.WriteGithubEvent(&push, body, req.Header.Get("X-Hub-Signature"))
case "issues":
var issue GithubIssuesEvent
g.WriteGithubEvent(&issue, body, req.Header.Get("X-Hub-Signature"))
default:
log.Print("Unknown GitHub event.", req.Header.Get("X-GitHub-Event"))
}
}