2016-05-20 08:48:29 -05:00
|
|
|
var Handlebars = require('handlebars')
|
|
|
|
var request = require('request')
|
2017-04-11 14:53:21 -05:00
|
|
|
var Logger = require('./logger')
|
2016-05-20 08:48:29 -05:00
|
|
|
|
2017-01-10 03:30:56 -05:00
|
|
|
var handleNotify = function(metadata, notifyEntry) {
|
|
|
|
var regex
|
2017-04-26 07:20:24 -05:00
|
|
|
if (metadata.name && notifyEntry.packagePattern) {
|
2017-01-10 03:30:56 -05:00
|
|
|
regex = new RegExp(notifyEntry.packagePattern, notifyEntry.packagePatternFlags || '')
|
2017-04-26 07:20:24 -05:00
|
|
|
if (!regex.test(metadata.name)) {
|
2017-01-10 03:30:56 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-05-20 08:48:29 -05:00
|
|
|
|
2017-01-10 03:30:56 -05:00
|
|
|
var template = Handlebars.compile(notifyEntry.content)
|
|
|
|
var content = template( metadata )
|
2016-05-20 08:48:29 -05:00
|
|
|
|
2017-01-10 03:30:56 -05:00
|
|
|
var options = {
|
|
|
|
body: content
|
|
|
|
}
|
2016-05-20 08:48:29 -05:00
|
|
|
|
2017-04-13 14:32:49 -05:00
|
|
|
// provides fallback support, it's accept an Object {} and Array of {}
|
2017-04-26 07:20:24 -05:00
|
|
|
if (notifyEntry.headers && Array.isArray(notifyEntry.headers)) {
|
|
|
|
var header = {}
|
2017-04-18 07:56:36 -05:00
|
|
|
notifyEntry.headers.map(function(item) {
|
2017-04-13 14:32:49 -05:00
|
|
|
if (Object.is(item, item)) {
|
|
|
|
for (var key in item) {
|
2017-04-26 07:20:24 -05:00
|
|
|
header[key] = item[key]
|
2017-04-13 14:32:49 -05:00
|
|
|
}
|
|
|
|
}
|
2017-04-26 07:20:24 -05:00
|
|
|
})
|
2017-04-13 14:32:49 -05:00
|
|
|
options.headers = header;
|
2017-04-18 07:56:36 -05:00
|
|
|
} else if (Object.is(notifyEntry.headers, notifyEntry.headers)) {
|
2017-04-26 07:20:24 -05:00
|
|
|
options.headers = notifyEntry.headers
|
2016-05-20 08:48:29 -05:00
|
|
|
}
|
|
|
|
|
2017-01-10 03:30:56 -05:00
|
|
|
options.method = notifyEntry.method
|
2016-05-20 08:48:29 -05:00
|
|
|
|
2017-04-26 07:20:24 -05:00
|
|
|
if ( notifyEntry.endpoint ) {
|
2017-01-10 03:30:56 -05:00
|
|
|
options.url = notifyEntry.endpoint
|
|
|
|
}
|
2016-05-20 08:48:29 -05:00
|
|
|
|
2017-04-18 07:49:24 -05:00
|
|
|
request(options, function(err, response, body) {
|
2017-04-11 14:53:21 -05:00
|
|
|
if (err) {
|
2017-04-26 07:20:24 -05:00
|
|
|
Logger.logger.error( { err: err }, ' notify error: @{err.message}' )
|
2017-04-11 14:53:21 -05:00
|
|
|
} else {
|
|
|
|
Logger.logger.info({ content: content}, 'A notification has been shipped: @{content}')
|
|
|
|
if (body) {
|
2017-04-26 07:20:24 -05:00
|
|
|
Logger.logger.debug( { body: body }, ' body: @{body}' )
|
2017-04-11 14:53:21 -05:00
|
|
|
}
|
|
|
|
}
|
2017-04-26 07:20:24 -05:00
|
|
|
})
|
2017-01-10 03:30:56 -05:00
|
|
|
}
|
2016-05-20 08:48:29 -05:00
|
|
|
|
2017-01-10 03:30:56 -05:00
|
|
|
module.exports.notify = function(metadata, config) {
|
2016-05-20 08:48:29 -05:00
|
|
|
|
2017-01-10 03:30:56 -05:00
|
|
|
if (config.notify) {
|
2017-04-26 07:20:24 -05:00
|
|
|
if (config.notify.content) {
|
2017-01-10 03:30:56 -05:00
|
|
|
handleNotify(metadata, config.notify)
|
|
|
|
}
|
|
|
|
else {
|
2017-04-26 07:20:24 -05:00
|
|
|
for (var key in config.notify) {
|
|
|
|
if (config.notify.hasOwnProperty(key)) {
|
2017-01-10 03:30:56 -05:00
|
|
|
handleNotify(metadata, config.notify[key])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-20 08:48:29 -05:00
|
|
|
}
|
|
|
|
}
|