Refactored sending of HipChat room messages.

This commit is contained in:
Nick Winter 2015-02-04 16:17:53 -08:00
parent 8f996a44b0
commit a189e32948
8 changed files with 61 additions and 58 deletions

View file

@ -461,8 +461,9 @@ module.exports = class Handler
sendwithus.api.send context, (err, result) -> sendwithus.api.send context, (err, result) ->
sendChangedHipChatMessage: (options) -> sendChangedHipChatMessage: (options) ->
message = "#{options.creator.get('name')} saved a change to <a href=\"#{options.docLink}\">#{options.target.get('name')}</a>: #{options.target.get('commitMessage')}" message = "#{options.creator.get('name')} saved a change to <a href=\"#{options.docLink}\">#{options.target.get('name')}</a>: #{options.target.get('commitMessage') or '(no commit message)'}"
hipchat.sendHipChatMessage message rooms = if /Diplomat submission/.test(message) then ['main'] else ['main', 'artisans']
hipchat.sendHipChatMessage message, rooms
makeNewInstance: (req) -> makeNewInstance: (req) ->
model = new @modelClass({}) model = new @modelClass({})

View file

@ -2,32 +2,34 @@ config = require '../server_config'
request = require 'request' request = require 'request'
log = require 'winston' log = require 'winston'
module.exports.sendHipChatMessage = sendHipChatMessage = (message) -> roomIDMap =
return unless key = config.hipchatAPIKey main: 254598
artisans: 1146994
tower: 318356
module.exports.sendHipChatMessage = sendHipChatMessage = (message, rooms, options) ->
return unless config.isProduction return unless config.isProduction
roomID = 254598 rooms ?= ['main']
options ?= {}
for room in rooms
unless roomID = roomIDMap[room]
log.error "Unknown HipChat room #{room}."
continue
unless key = config.hipchat[room]
log.info "No HipChat API key for room #{room}."
continue
form = form =
color: 'yellow' color: options.color or 'yellow'
notify: false notify: false
message: message message: message
messageFormat: 'html' messageFormat: 'html'
url = "https://api.hipchat.com/v2/room/#{roomID}/notification?auth_token=#{key}" if options.papertrail
request.post {uri: url, json: form}, (err, res, body) ->
return log.error 'Error sending HipChat patch request:', err or body if err or /error/i.test body
#log.info "Got HipChat patch response:", body
module.exports.sendTowerHipChatMessage = sendTowerHipChatMessage = (message) ->
secondsFromEpoch = Math.floor(new Date().getTime() / 1000) secondsFromEpoch = Math.floor(new Date().getTime() / 1000)
link = "<a href=\"https://papertrailapp.com/groups/488214/events?time=#{secondsFromEpoch}\">PaperTrail</a>" link = "<a href=\"https://papertrailapp.com/groups/488214/events?time=#{secondsFromEpoch}\">PaperTrail</a>"
message = "#{message} #{link}" form.message = "#{message} #{link}"
return unless key = config.hipchatTowerAPIKey form.color = options.color or 'red'
return unless config.isProduction form.notify = true
roomID = 318356
form =
color: 'red'
notify: true
message: message
messageFormat: 'html'
url = "https://api.hipchat.com/v2/room/#{roomID}/notification?auth_token=#{key}" url = "https://api.hipchat.com/v2/room/#{roomID}/notification?auth_token=#{key}"
request.post {uri: url, json: form}, (err, res, body) -> request.post {uri: url, json: form}, (err, res, body) ->
return log.error 'Error sending HipChat Tower message:', err or body if err or /error/i.test body return log.error 'Error sending HipChat message:', err or body if err or /error/i.test body
#log.info "Got HipChat message response:", body

View file

@ -101,6 +101,6 @@ PatchHandler = class PatchHandler extends Handler
sendPatchCreatedHipChatMessage: (options) -> sendPatchCreatedHipChatMessage: (options) ->
message = "#{options.creator.get('name')} submitted a patch to <a href=\"#{options.docLink}\">#{options.target.get('name')}</a>: #{options.patch.get('commitMessage')}" message = "#{options.creator.get('name')} submitted a patch to <a href=\"#{options.docLink}\">#{options.target.get('name')}</a>: #{options.patch.get('commitMessage')}"
hipchat.sendHipChatMessage message hipchat.sendHipChatMessage message, ['main']
module.exports = new PatchHandler() module.exports = new PatchHandler()

View file

@ -366,7 +366,7 @@ PaymentHandler = class PaymentHandler extends Handler
sendPaymentHipChatMessage: (options) -> sendPaymentHipChatMessage: (options) ->
try try
message = "#{options.user?.get('name')} bought #{options.payment?.get('amount')} via #{options.payment?.get('service')}." message = "#{options.user?.get('name')} bought #{options.payment?.get('amount')} via #{options.payment?.get('service')}."
hipchat.sendHipChatMessage message hipchat.sendHipChatMessage message, ['tower']
catch e catch e
log.error "Couldn't send HipChat message on payment because of error: #{e}" log.error "Couldn't send HipChat message on payment because of error: #{e}"

View file

@ -4,8 +4,6 @@ Handler = require '../commons/Handler'
{handlers} = require '../commons/mapping' {handlers} = require '../commons/mapping'
mongoose = require 'mongoose' mongoose = require 'mongoose'
log = require 'winston' log = require 'winston'
sendwithus = require '../sendwithus'
hipchat = require '../hipchat'
PurchaseHandler = class PurchaseHandler extends Handler PurchaseHandler = class PurchaseHandler extends Handler
modelClass: Purchase modelClass: Purchase

View file

@ -48,7 +48,7 @@ module.exports.setup = (app) ->
log.error(error.stack) log.error(error.stack)
# TODO: Generally ignore this error: error: Error trying db method get route analytics.log.event from undefined: Error: Cannot find module '../undefined' # TODO: Generally ignore this error: error: Error trying db method get route analytics.log.event from undefined: Error: Cannot find module '../undefined'
unless "#{parts}" in ['analytics.users.active'] unless "#{parts}" in ['analytics.users.active']
hipchat.sendTowerHipChatMessage errorMessage hipchat.sendHipChatMessage errorMessage, ['tower'], papertrail: true
errors.notFound(res, "Route #{req?.path} not found.") errors.notFound(res, "Route #{req?.path} not found.")
getSchema = (req, res, moduleName) -> getSchema = (req, res, moduleName) ->

View file

@ -249,7 +249,6 @@ UserHandler = class UserHandler extends Handler
sendOneTimeEmail: (req, res) -> sendOneTimeEmail: (req, res) ->
# TODO: should this API be somewhere else? # TODO: should this API be somewhere else?
# TODO: hipchat tower success message shows up as a misleading PaperTrail error message
return @sendForbiddenError(res) unless req.user return @sendForbiddenError(res) unless req.user
email = req.query.email or req.body.email email = req.query.email or req.body.email
type = req.query.type or req.body.type type = req.query.type or req.body.type
@ -276,7 +275,7 @@ UserHandler = class UserHandler extends Handler
req.user.save (err) => req.user.save (err) =>
return @sendDatabaseError(res, err) if err return @sendDatabaseError(res, err) if err
@sendSuccess(res, {result: 'success'}) @sendSuccess(res, {result: 'success'})
hipchat.sendTowerHipChatMessage "#{req.user.get('name') or req.user.get('email')} just submitted subscribe modal parent email #{email}." hipchat.sendHipChatMessage "#{req.user.get('name') or req.user.get('email')} just submitted subscribe modal parent email #{email}.", ['tower']
AnalyticsLogEvent.logEvent req.user, 'Sent one time email', email: email, type: type AnalyticsLogEvent.logEvent req.user, 'Sent one time email', email: email, type: type
agreeToCLA: (req, res) -> agreeToCLA: (req, res) ->
@ -295,7 +294,7 @@ UserHandler = class UserHandler extends Handler
req.user.save (err) => req.user.save (err) =>
return @sendDatabaseError(res, err) if err return @sendDatabaseError(res, err) if err
@sendSuccess(res, {result: 'success'}) @sendSuccess(res, {result: 'success'})
hipchat.sendHipChatMessage "#{req.body.githubUsername or req.user.get('name')} just signed the CLA." hipchat.sendHipChatMessage "#{req.body.githubUsername or req.user.get('name')} just signed the CLA.", ['main']
avatar: (req, res, id) -> avatar: (req, res, id) ->
@modelClass.findById(id).exec (err, document) => @modelClass.findById(id).exec (err, document) =>

View file

@ -48,8 +48,11 @@ config.mail =
cronHandlerPublicIP: process.env.COCO_CRON_PUBLIC_IP or '' cronHandlerPublicIP: process.env.COCO_CRON_PUBLIC_IP or ''
cronHandlerPrivateIP: process.env.COCO_CRON_PRIVATE_IP or '' cronHandlerPrivateIP: process.env.COCO_CRON_PRIVATE_IP or ''
config.hipchatAPIKey = process.env.COCO_HIPCHAT_API_KEY or '' config.hipchat =
config.hipchatTowerAPIKey = process.env.COCO_HIPCHAT_TOWER_API_KEY or '' main: process.env.COCO_HIPCHAT_API_KEY or ''
tower: process.env.COCO_HIPCHAT_TOWER_API_KEY or ''
artisans: process.env.COCO_HIPCHAT_ARTISANS_API_KEY or ''
config.queue = config.queue =
accessKeyId: process.env.COCO_AWS_ACCESS_KEY_ID or '' accessKeyId: process.env.COCO_AWS_ACCESS_KEY_ID or ''
secretAccessKey: process.env.COCO_AWS_SECRET_ACCESS_KEY or '' secretAccessKey: process.env.COCO_AWS_SECRET_ACCESS_KEY or ''