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