-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgithub-repo-event-notifier.coffee
More file actions
128 lines (107 loc) · 4.55 KB
/
Copy pathgithub-repo-event-notifier.coffee
File metadata and controls
128 lines (107 loc) · 4.55 KB
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
# Description:
# Notifies about any available GitHub repo event via webhook #
# Configuration:
# HUBOT_GITHUB_EVENT_NOTIFIER_ROOM - The default room to which message should go (optional)
# HUBOT_GITHUB_EVENT_NOTIFIER_TYPES - Comma-separated list of event types to notify on
# (See: http://developer.github.com/webhooks/#events)
#
# You will have to do the following:
# 1. Create a new webhook for your `myuser/myrepo` repository at:
# https://github.com/myuser/myrepo/settings/hooks/new
#
# 2. Select the individual events to minimize the load on your Hubot.
#
# 3. Add the url: <HUBOT_URL>:<PORT>/hubot/gh-repo-events[?room=<room>]
# (Don't forget to urlencode the room name, especially for IRC. Hint: # = %23)
#
# Commands:
# None
#
# URLS:
# POST /hubot/gh-repo-events?room=<room>
#
# Notes:
# Currently tested with the following event types in HUBOT_GITHUB_EVENT_NOTIFIER_TYPES:
# - issue
# - pull_request
#
# Authors:
# spajus
# patcon
# parkr
# pmgarman
url = require('url')
querystring = require('querystring')
eventActions = require('./event-actions/all')
eventTypesRaw = process.env['HUBOT_GITHUB_EVENT_NOTIFIER_TYPES']
eventTypes = []
regexGithubUser = /(?:I'm|I am) @?([a-z0-9]+) on GitHub/i
regexNotGithubUser = /(?:I'm|I am) not on GitHub/i
if eventTypesRaw?
# create a list like: "issues:* pull_request:comment pull_request:close fooevent:baraction"
# -- if any action is omitted, it will be appended with an asterisk (foo becomes foo:*) to
# indicate that any action on event foo is acceptable
splitRawEvents = eventTypesRaw.split(',')
eventTypes = ((if e.indexOf(":") > -1 then e.trim() else e.trim()+":*") for e in splitRawEvents)
console.log "Registered event:action entry #{entry}" for entry in eventTypes
else
console.warn("github-repo-event-notifier is not setup to receive any events (HUBOT_GITHUB_EVENT_NOTIFIER_TYPES is empty).")
module.exports = (robot) ->
robot.respond regexGithubUser, (msg) ->
match = regexGithubUser.exec msg.text
user = msg.user
if match
console.log "User @#{msg.user.mention_name} asked to link their GitHub account #{match[1]}"
(user.accounts ?= {})['github'] = match[1]
msg.reply "Ok, I'll remember you as #{match[1]} on GitHub."
robot.respond regexNotGithubUser, (msg) ->
delete (msg.user.accounts ?= {})['github']
msg.reply "Ok, you're not on GitHub."
robot.router.post "/hubot/gh-repo-events", (req, res) ->
query = querystring.parse(url.parse(req.url).query)
content_type = req.headers["content-type"]
if content_type == "application/x-www-form-urlencoded"
console.log "Received a non-JSON formatted webhook, exiting..."
return
data = req.body
room = query.room || process.env["HUBOT_GITHUB_EVENT_NOTIFIER_ROOM"]
eventType = req.headers["x-github-event"]
console.log "Processing event type #{eventType}, format #{content_type}..."
filtered_results = []
try
filter_parts = eventTypes.filter (e) ->
# should always be at least two parts, from eventTypes creation above
parts = e.split(":")
event_part = parts[0]
action_part = parts[1]
# remove anything that isn't this event
return false if event_part != eventType
# wildcard on this event
return true if action_part == "*"
# no action property, let it pass
return true if !data.hasOwnProperty('action')
# action match
return true if action_part == data.action
# no match, fail
return false
if filter_parts.length > 0 # something matched
console.log "After filtering, #{entry} remained" for entry in filter_parts
filtered_results.push filter_parts
else
console.log "Ignoring #{eventType} event as it's not allowed"
catch error
robot.messageRoom room, "Whoa, I got an error filtering event types: #{error}"
console.log "github repo event notifier error filtering event types: #{error}. Request: #{req.body}"
for e in filtered_results
try
announceRepoEvent robot, data, eventType, (what) ->
robot.messageRoom room, what
catch error
robot.messageRoom room, "Whoa, I failed to announce event type #{eventType}: #{error}"
console.log "github repo event notifier error announcing event types #{eventType}: #{error}. Request: #{req.body}"
res.end ""
announceRepoEvent = (robot, data, eventType, cb) ->
if eventActions[eventType]?
eventActions[eventType](robot, data, cb)
else
cb("Received a new #{eventType} event, just so you know.")