-
Notifications
You must be signed in to change notification settings - Fork 42
/
github-repo-event-notifier.coffee
112 lines (94 loc) · 3.55 KB
/
github-repo-event-notifier.coffee
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
# 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
# - page_build
# - pull_request
# - push
#
# Authors:
# spajus
# patcon
# parkr
# strugee
inspect = (require('util')).inspect
url = require('url')
querystring = require('querystring')
eventActions = require('./event-actions/all')
eventTypesRaw = process.env['HUBOT_GITHUB_EVENT_NOTIFIER_TYPES']
eventTypes = []
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
###
eventTypes = eventTypesRaw.split(',').map (e) ->
append = ""
# append :* to any elements missing it
if e.indexOf(":") == -1
append = ":*"
return "#{e}#{append}"
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.router.post "/hubot/gh-repo-events", (req, res) ->
query = querystring.parse(url.parse(req.url).query)
data = req.body
robot.logger.debug "github-repo-event-notifier: Received POST to /hubot/gh-repo-events with data = #{inspect data}"
room = query.room || process.env["HUBOT_GITHUB_EVENT_NOTIFIER_ROOM"]
eventType = req.headers["x-github-event"]
robot.logger.debug "github-repo-event-notifier: Processing event type: \"#{eventType}\"..."
adapter = robot.adapterName
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]
if event_part != eventType
return false # remove anything that isn't this event
if action_part == "*"
return true # wildcard on this event
if !data.hasOwnProperty('action')
return true # no action property, let it pass
if action_part == data.action
return true # action match
return false # no match, fail
if filter_parts.length > 0
announceRepoEvent adapter, data, eventType, (what) ->
robot.messageRoom room, what
else
console.log "Ignoring #{eventType}:#{data.action} as it's not allowed."
catch error
robot.messageRoom room, "Whoa, I got an error: #{error}"
console.log "Github repo event notifier error: #{error}. Request: #{req.body}"
res.end ""
announceRepoEvent = (adapter, data, eventType, cb) ->
if eventActions[eventType]?
eventActions[eventType](adapter, data, cb)
else
cb("Received a new #{eventType} event, just so you know.")