This repository was archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathgithub-pull-request-notifier.coffee
72 lines (59 loc) · 2.21 KB
/
github-pull-request-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
# Description:
# An HTTP Listener that notifies about new Github pull requests
#
# Dependencies:
# "url": ""
# "querystring": ""
#
# Configuration:
# You will have to do the following:
# 1. Get an API token: curl -u 'username' -d '{"scopes":["repo"],"note":"Hooks management"}' \
# https://api.github.com/authorizations
# 2. Add <HUBOT_URL>:<PORT>/hubot/gh-pull-requests?room=<room>[&type=<type>] url hook via API:
# curl -H "Authorization: token <your api token>" \
# -d '{"name":"web","active":true,"events":["pull_request"],"config":{"url":"<this script url>","content_type":"json"}}' \
# https://api.github.com/repos/<your user>/<your repo>/hooks
#
# Commands:
# None
#
# URLS:
# POST /hubot/gh-pull-requests?room=<room>[&type=<type]
#
# Authors:
# spajus
#
# Notes:
# Room information can be obtained by hubot-script: room-info.coffee
# Room must be in url encoded format (i.e. encodeURIComponent("yourRoomInfo"))
url = require('url')
querystring = require('querystring')
module.exports = (robot) ->
robot.router.post "/hubot/gh-pull-requests", (req, res) ->
query = querystring.parse(url.parse(req.url).query)
data = req.body
room = query.room
try
announcePullRequest data, (what) ->
robot.messageRoom room, what
catch error
robot.messageRoom room, "Whoa, I got an error: #{error}"
console.log "github pull request notifier error: #{error}. Request: #{req.body}"
res.end ""
announcePullRequest = (data, cb) ->
if data.action == 'opened'
mentioned = data.pull_request.body?.match(/(^|\s)(@[\w\-\/]+)/g)
if mentioned
unique = (array) ->
output = {}
output[array[key]] = array[key] for key in [0...array.length]
value for key, value of output
mentioned = mentioned.filter (nick) ->
slashes = nick.match(/\//g)
slashes is null or slashes.length < 2
mentioned = mentioned.map (nick) -> nick.trim()
mentioned = unique mentioned
mentioned_line = "\nMentioned: #{mentioned.join(", ")}"
else
mentioned_line = ''
cb "New pull request \"#{data.pull_request.title}\" by #{data.pull_request.user.login}: #{data.pull_request.html_url}#{mentioned_line}"