Last active
August 29, 2015 14:15
-
-
Save fadrizul/dbcee888167caf87860b to your computer and use it in GitHub Desktop.
Phabricator feed hooks to Hipchat notifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Phabricator to HipChat feed message hooks | |
* Author: Fadrizul Hasani <fadrizul@gmail.com> | |
*/ | |
var http = require('http'); | |
var https = require('https'); | |
var qs = require('querystring'); | |
http.createServer(function (req, res) { | |
// Listen for incoming request from Phabricator | |
if (req.method == 'POST') { | |
var body = ''; | |
req.on('data', function (data) { | |
body += data; | |
var jsonBody = qs.parse(body); | |
var link = jsonBody.storyText.match(/D\d+/g); | |
// Determine the background colour for the message. | |
var colour = 'yellow'; | |
if (jsonBody.storyText.indexOf('requested changes') > 0) { | |
colour = 'red'; | |
} else if (jsonBody.storyText.indexOf('accepted') > 0) { | |
colour = 'green'; | |
} else if (jsonBody.storyText.indexOf('updated') > 0) { | |
colour = 'gray'; | |
} | |
// Building the post data for HipChat request | |
var postData = qs.stringify({ | |
format : 'json', | |
auth_token : '', | |
room_id : '', | |
from : 'Phabricator', | |
message : jsonBody.storyText + ' <a href="' + link + '">View diff</a>', | |
message_format : 'html', | |
color : colour, | |
notify : 1, | |
}); | |
// Post options | |
var options = { | |
hostname: 'api.hipchat.com', | |
port: 443, | |
path: '/v1/rooms/message', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Content-Length': postData.length | |
} | |
}; | |
// Execute the POST request | |
var initReq = https.request(options, function(initRes) { | |
initRes.setEncoding('utf8'); | |
initRes.on('data', function (chunk) { | |
console.log('BODY: ' + chunk); | |
}); | |
}); | |
initReq.on('error', function(e) { | |
console.log('problem with request: ' + e.message); | |
}); | |
// write data to request body | |
initReq.write(postData); | |
initReq.end(); | |
if (body.length > 1e6) initReq.connection.destroy(); | |
}); | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
res.end(); | |
} | |
}).listen(80, '0.0.0.0'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment