1 | # Description:
|
2 | # Announces GitHub events received via webhook to the chat rooms.
|
3 | #
|
4 | # Dependencies:
|
5 | # None
|
6 | #
|
7 | # Configuration:
|
8 | # HUBOT_GITHUB_EVENT_DEFAULT_ROOM - Room name of the default room to announce events in.
|
9 | # HUBOT_GITHUB_EVENT_SECRET - Secret that matches the value stored in the GitHub hook definition.
|
10 | #
|
11 | # Commands:
|
12 | # None
|
13 | #
|
14 | # Notes:
|
15 | # None
|
16 | #
|
17 | # Author:
|
18 | # lee-dohm
|
19 |
|
20 | fs = require 'fs'
|
21 |
|
22 | formatters = require './formatters/all'
|
23 |
|
24 | module.exports = (robot) ->
|
25 | robot.router.post '/hubot/github-events', (req, res) ->
|
26 | receiveHook req, (event) ->
|
27 | robot.emit 'github-event', event
|
28 | res.send(204)
|
29 |
|
30 | robot.on 'github-event', (event) ->
|
31 | announceEvent event, (room, message) ->
|
32 | robot.messageRoom room, message
|
33 |
|
34 | # Public: Announces the event.
|
35 | #
|
36 | # * `event` Event to announce.
|
37 | # * `callback` {Function} that accepts:
|
38 | # * `room` Room {String} to announce the event to.
|
39 | # * `message` Message {String} to use to announce the event.
|
40 | announceEvent = (event, callback) ->
|
41 | formatter = formatters[event.type] ? formatters.unhandled
|
42 | message = formatter(event)
|
43 | callback(event.room, message)
|
44 |
|
45 | # Public: Receives the GitHub event webhook request.
|
46 | #
|
47 | # * `req` {Request} of the webhook.
|
48 | # * `callback` {Function} that accepts:
|
49 | # * `event` {Object} consisting of:
|
50 | # * `data` Event data
|
51 | # * `id` Event ID
|
52 | # * `room` Room to announce the event to.
|
53 | # * `signature` Signature validating the event.
|
54 | # * `type` Type of the event.
|
55 | receiveHook = (req, callback) ->
|
56 | event =
|
57 | data: req.body
|
58 | id: req.get('X-Github-Delivery')
|
59 | room: req.query.room ? process.env.HUBOT_GITHUB_EVENT_DEFAULT_ROOM
|
60 | signature: req.get('X-Github-Signature')
|
61 | type: req.get('X-Github-Event')
|
62 |
|
63 | callback(event)
|