-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.coffee
executable file
·91 lines (74 loc) · 2.67 KB
/
app.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
#!/usr/bin/env coffee
#
# Copyright 2013 Artillery Games, Inc.
# Licensed under the MIT license.
#
# Useful links:
# http://developer.github.com/v3/repos/hooks/
# https://www.hipchat.com/docs/api/method/rooms/message
express = require 'express'
https = require 'https'
http = require 'http'
querystring = require 'querystring'
app = express()
app.use express.logger('dev')
app.use express.bodyParser()
app.use app.router
if not process.env.HIPCHAT_API_KEY
console.error "Missing HIPCHAT_API_KEY env var"
process.exit 1
sendHipChatMessage = (roomID, text) ->
qs = querystring.stringify
auth_token: process.env.HIPCHAT_API_KEY
room_id: roomID
from: 'GitHub'
message: text
message_format: 'text'
notify: 1
color: 'yellow'
path = "/v1/rooms/message?#{ qs }"
req = https.get { host: 'api.hipchat.com', path: path }, (res) ->
console.log "Sent '#{ text }' to room #{ roomID }, HTTP code #{ res.statusCode }"
console.log "Path was: #{ path }"
req.on 'error', (err) ->
console.log "Unable to send to room #{ roomID }: #{ err }"
req.end()
app.get '/', (req, res) ->
res.redirect 'https://github.com/artillery/pull-request-notifier'
app.post '/', (req, res) ->
secret = process.env.SECRET_PARAM
if secret and req.param('secret') != secret
return res.send 400, 'Bad secret'
if req.headers['x-github-event'] != 'pull_request'
return res.send 400, 'Not a pull request'
payload = JSON.parse req.body.payload
number = payload.number
person = payload.pull_request.user.login
action = payload.action
title = payload.pull_request.title
url = payload.pull_request.html_url
isMerged = payload.pull_request.merged
if action is 'synchronize'
return res.send 200, 'ignored'
# Trying this: Only announce opened PRs.
if action not in ['opened', 'reopened']
return res.send 200, 'ignored'
roomID = process.env.HIPCHAT_DETAIL_ROOM
if roomID
# We prefix our PR titles with names of who should review them, like
# "IAN/MARK: This fixes the thing". Might as well use @-style addressing in
# the eng room chat so people are notified.
newTitle = title
.replace(/([A-Z]+)([:/])/g, '@$1 ')
.replace('@ANYONE', '@ALL')
message = "PR #{ number } #{ action }: #{ newTitle } (#{ person }) - #{ url }"
sendHipChatMessage roomID, message
roomID = process.env.HIPCHAT_ANNOUNCE_ROOM
if roomID and isMerged
message = "Pull request by #{ person } merged: #{ title }"
sendHipChatMessage roomID, message
res.send 200, 'ok'
server = http.createServer app
server.listen process.env.PORT or 9090
address = server.address()
console.log "Pull Request Notifier listening on #{ address.address }:#{ address.port }"