Last active
May 5, 2016 11:42
-
-
Save andrewtamura/81cb43f86f332be46187 to your computer and use it in GitHub Desktop.
Lambda function: Modulus Webhook to Slack incoming webhook
This file contains hidden or 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
var request = require('request'); | |
var util = require('util'); | |
var slackWebhookUrl = '<YOUR_SLACK_WEBHOOK_API_URL>'; | |
exports.handler = function(event, context) { | |
var data = convertModulusWebhook(event); | |
var options = { | |
method: 'post', | |
body: data, | |
json: true, | |
url: slackWebhookUrl | |
}; | |
request(options, context.done); | |
}; | |
/* convert a Modulus webhook into a slack webhook | |
*/ | |
var convertModulusWebhook = function(webhook) { | |
var data = webhook; | |
var text = util.format('%s - %s', data.type, data.project.name); | |
var resp = {}; | |
resp.attachments = []; | |
var info = { | |
'fallback': text, | |
'pretext': text, | |
'color': getStatusColor(data.type), | |
'fields': [] | |
}; | |
info.fields.push(buildCrashAttachment(data)); | |
resp.attachments.push(info); | |
return resp; | |
}; | |
var buildCrashAttachment = function(data) { | |
if (data.type === 'crash' && data.servo) { | |
return { | |
'title': 'Modulus Alert', | |
'text': data.servo.log, | |
'short': false | |
}; | |
} else { | |
return { | |
'title': 'Modulus Alert', | |
'text': data.project.name, | |
'short': false | |
}; | |
} | |
}; | |
/* returns a Slack color depending on the modulus event type. Crash should be red, deploy should be | |
* green, and everything else will be orange. | |
*/ | |
var getStatusColor = function(eventType) { | |
if (eventType === 'crash') { | |
return 'danger'; | |
} else if (eventType === 'deploy') { | |
return 'good'; | |
} else { | |
return 'warning'; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment