This repository has been archived by the owner on May 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
github-webhook-echo.go
317 lines (290 loc) · 9.76 KB
/
github-webhook-echo.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
// Steve Phillips / elimisteve
// Created 2011.03.25
// Updated 2014.12.12
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"strings"
"github.com/elimisteve/fun"
)
const (
IRC_SERVER = "irc.freenode.net:6667"
BOT_NICK = "sbhx_github"
IRC_CHANNEL = "#sbhackerspace"
PRIVMSG_PRE = "PRIVMSG "
PRIVMSG_POST = " :"
IRC_CHAN_MSG_PRE = PRIVMSG_PRE + IRC_CHANNEL + PRIVMSG_POST
GITHUB_PORT = "9000"
)
// Only one connection allowed (subject to change)
var conn net.Conn
// Anything passed to this channel is echoed into IRC_CHANNEL
var irc = make(chan string)
func main() {
// Listens for webhook POSTs from GitHub
go webhookListener()
// Anything passed to the `irc` channel (get it?) is echoed into
// IRC_CHANNEL
go func() {
for {
rawIrcMsg(IRC_CHAN_MSG_PRE + <-irc)
}
}()
// Connect to IRC
conn = ircSetup()
defer conn.Close()
//
// Main loop
//
buf := make([]byte, 512)
for {
// n bytes read
n, err := conn.Read(buf)
if err != nil {
log.Printf("Error reading conn; reconnecting. Err: %v\n", err)
conn.Close()
conn = ircSetup()
continue
}
rawData := string(buf[:n])
rawData = strings.TrimRight(rawData, " \t\r\n") // Remove trailing whitespace
log.Printf("%v\n", rawData)
//
// Respond to PING from server to stay connected
//
if strings.HasPrefix(rawData, "PING") {
rawIrcMsg("PONG " + rawData)
continue
}
//
// Re-join if kicked
//
if fun.ContainsAllStrings(rawData, "KICK", BOT_NICK) {
rawIrcMsg("JOIN " + IRC_CHANNEL)
}
}
}
func ircSetup() net.Conn {
var err error
// Global `conn`
conn, err = net.Dial("tcp", IRC_SERVER)
if err != nil {
log.Fatalf("Error connecting to %s: %v\n", IRC_SERVER, err)
}
rawIrcMsg("NICK " + BOT_NICK)
rawIrcMsg("USER " + strings.Repeat(BOT_NICK+" ", 4))
rawIrcMsg("JOIN " + IRC_CHANNEL)
return conn
}
// rawIrcMsg takes a string and writes it to the global TCP connection
// to the IRC server _verbatim_
func rawIrcMsg(str string) {
conn.Write([]byte(str + "\n"))
}
//
//
// Listen for GitHub pushes
//
//
// webhookListener listens on GITHUB_PORT (default: 9000) for JSON
// HTTP POSTs, parses the relevant data, then sends it over a channel
// to a function waiting for strings to echo into IRC_CHANNEL
func webhookListener() {
http.HandleFunc("/", webhookHandler)
log.Printf("Listening on port %v...\n", GITHUB_PORT)
log.Fatal(http.ListenAndServe(":"+GITHUB_PORT, nil))
}
func webhookHandler(w http.ResponseWriter, r *http.Request) {
// Read POST data
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("Error in webhookHandler: %v\n", err)
return
}
// Unmarshal JSON
push := &GithubPush{}
err = json.Unmarshal(body, push)
if err != nil {
log.Printf("Error unmarshaling \n\n%s\n\n -- %v\n", body, err)
return
}
// Generate summary and echo it into IRC_CHANNEL
summary := push.SummarizeHeadCommit()
log.Printf("Echoing this summary to IRC:\n%s\n\n", summary)
irc <- summary
}
func (push *GithubPush) SummarizeHeadCommit() string {
head := push.HeadCommit
name := head.Committer.Name
username := head.Committer.Username
repo := push.Repository.FullName
msg := head.Message
url := head.Url
return fmt.Sprintf(
`%s (%s) just pushed %d commit(s) to %s. Latest: %q. %v`,
name, username, len(push.Commits), repo, msg, url)
}
type GithubPush struct {
Ref string `json:"ref"`
Before string `json:"before"`
After string `json:"after"`
Created bool `json:"created"`
Deleted bool `json:"deleted"`
Forced bool `json:"forced"`
BaseRef interface{} `json:"base_ref"`
Compare string `json:"compare"`
Commits []struct {
Id string `json:"id"`
Distinct bool `json:"distinct"`
Message string `json:"message"`
Timestamp string `json:"timestamp"`
Url string `json:"url"`
Author struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"author"`
Committer struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"committer"`
Added []string `json:"added"`
Removed []interface{} `json:"removed"`
Modified []interface{} `json:"modified"`
} `json:"commits"`
HeadCommit struct {
Id string `json:"id"`
Distinct bool `json:"distinct"`
Message string `json:"message"`
Timestamp string `json:"timestamp"`
Url string `json:"url"`
Author struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"author"`
Committer struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"committer"`
Added []string `json:"added"`
Removed []interface{} `json:"removed"`
Modified []interface{} `json:"modified"`
} `json:"head_commit"`
Repository struct {
Id int `json:"id"`
Name string `json:"name"`
FullName string `json:"full_name"`
Owner struct {
Name string `json:"name"`
Email string `json:"email"`
} `json:"owner"`
Private bool `json:"private"`
HtmlUrl string `json:"html_url"`
Description string `json:"description"`
Fork bool `json:"fork"`
Url string `json:"url"`
ForksUrl string `json:"forks_url"`
KeysUrl string `json:"keys_url"`
CollaboratorsUrl string `json:"collaborators_url"`
TeamsUrl string `json:"teams_url"`
HooksUrl string `json:"hooks_url"`
IssueEventsUrl string `json:"issue_events_url"`
EventsUrl string `json:"events_url"`
AssigneesUrl string `json:"assignees_url"`
BranchesUrl string `json:"branches_url"`
TagsUrl string `json:"tags_url"`
BlobsUrl string `json:"blobs_url"`
GitTagsUrl string `json:"git_tags_url"`
GitRefsUrl string `json:"git_refs_url"`
TreesUrl string `json:"trees_url"`
StatusesUrl string `json:"statuses_url"`
LanguagesUrl string `json:"languages_url"`
StargazersUrl string `json:"stargazers_url"`
ContributorsUrl string `json:"contributors_url"`
SubscribersUrl string `json:"subscribers_url"`
SubscriptionUrl string `json:"subscription_url"`
CommitsUrl string `json:"commits_url"`
GitCommitsUrl string `json:"git_commits_url"`
CommentsUrl string `json:"comments_url"`
IssueCommentUrl string `json:"issue_comment_url"`
ContentsUrl string `json:"contents_url"`
CompareUrl string `json:"compare_url"`
MergesUrl string `json:"merges_url"`
ArchiveUrl string `json:"archive_url"`
DownloadsUrl string `json:"downloads_url"`
IssuesUrl string `json:"issues_url"`
PullsUrl string `json:"pulls_url"`
MilestonesUrl string `json:"milestones_url"`
NotificationsUrl string `json:"notifications_url"`
LabelsUrl string `json:"labels_url"`
ReleasesUrl string `json:"releases_url"`
CreatedAt int `json:"created_at"`
UpdatedAt string `json:"updated_at"`
PushedAt int `json:"pushed_at"`
GitUrl string `json:"git_url"`
SshUrl string `json:"ssh_url"`
CloneUrl string `json:"clone_url"`
SvnUrl string `json:"svn_url"`
Homepage string `json:"homepage"`
Size int `json:"size"`
StargazersCount int `json:"stargazers_count"`
WatchersCount int `json:"watchers_count"`
Language string `json:"language"`
HasIssues bool `json:"has_issues"`
HasDownloads bool `json:"has_downloads"`
HasWiki bool `json:"has_wiki"`
HasPages bool `json:"has_pages"`
ForksCount int `json:"forks_count"`
MirrorUrl interface{} `json:"mirror_url"`
OpenIssuesCount int `json:"open_issues_count"`
Forks int `json:"forks"`
OpenIssues int `json:"open_issues"`
Watchers int `json:"watchers"`
DefaultBranch string `json:"default_branch"`
Stargazers int `json:"stargazers"`
MasterBranch string `json:"master_branch"`
Organization string `json:"organization"`
} `json:"repository"`
Pusher struct {
Name string `json:"name"`
Email string `json:"email"`
} `json:"pusher"`
Organization struct {
Login string `json:"login"`
Id int `json:"id"`
Url string `json:"url"`
ReposUrl string `json:"repos_url"`
EventsUrl string `json:"events_url"`
MembersUrl string `json:"members_url"`
PublicMembersUrl string `json:"public_members_url"`
AvatarUrl string `json:"avatar_url"`
} `json:"organization"`
Sender struct {
Login string `json:"login"`
Id int `json:"id"`
AvatarUrl string `json:"avatar_url"`
GravatarId string `json:"gravatar_id"`
Url string `json:"url"`
HtmlUrl string `json:"html_url"`
FollowersUrl string `json:"followers_url"`
FollowingUrl string `json:"following_url"`
GistsUrl string `json:"gists_url"`
StarredUrl string `json:"starred_url"`
SubscriptionsUrl string `json:"subscriptions_url"`
OrganizationsUrl string `json:"organizations_url"`
ReposUrl string `json:"repos_url"`
EventsUrl string `json:"events_url"`
ReceivedEventsUrl string `json:"received_events_url"`
Type string `json:"type"`
SiteAdmin bool `json:"site_admin"`
} `json:"sender"`
}