From f63e690b15319909e62a6beb84589337927f3626 Mon Sep 17 00:00:00 2001 From: Ryan Graham Date: Tue, 10 Jan 2017 08:30:56 +0000 Subject: [PATCH] Add support for multiple notification endpoints to existing webhook system --- conf/full.yaml | 26 +++++++++++++++++++++++ lib/notify.js | 57 +++++++++++++++++++++++++++++++++----------------- 2 files changed, 64 insertions(+), 19 deletions(-) diff --git a/conf/full.yaml b/conf/full.yaml index 965594767..d61d59bb4 100644 --- a/conf/full.yaml +++ b/conf/full.yaml @@ -134,6 +134,11 @@ notify: # Choose a method. Technically this will accept any HTTP # request method, but probably stick to GET or POST method: POST + # Only run this notification if the package name matches the regular + # expression + packagePattern: ^example-package$ + # Any flags to be used with the regular expression + packagePatternFlags: i # If this endpoint requires specific headers, set them here # as an array of key: value objects. headers: [{'Content-type': 'application/x-www-form-urlencoded'}] @@ -146,3 +151,24 @@ notify: content: ' {{ handlebar-expression }}' # For Slack, follow the following format: # content: '{ "text": "Package *{{ name }}* published to version *{{ dist-tags.latest }}*", "username": "Verdaccio", "icon_emoji": ":package:" }' + + # Multiple notification endpoints can be created by specifying a collection + 'example-package-1' + method: POST + # Only run this notification if the package name matches the regular + # expression + packagePattern: ^example-package-regex$ + # Any flags to be used with the regular expression + packagePatternFlags: i + # If this endpoint requires specific headers, set them here + # as an array of key: value objects. + headers: [{'Content-type': 'application/x-www-form-urlencoded'}] + # set the URL endpoint for this call + endpoint: https://hooks.slack.com/... + # Finally, the content you will be sending in the body. + # This data will first be run through Handlebars to parse + # any Handlebar expressions. All data housed in the metadata object + # is available for use within the expressions. + content: ' {{ handlebar-expression }}' + # For Slack, follow the following format: + # content: '{ "text": "Package *{{ name }}* published to version *{{ dist-tags.latest }}*", "username": "Verdaccio", "icon_emoji": ":package:" }' diff --git a/lib/notify.js b/lib/notify.js index 5482b15e9..00d5c5f10 100644 --- a/lib/notify.js +++ b/lib/notify.js @@ -1,28 +1,47 @@ var Handlebars = require('handlebars') var request = require('request') +var handleNotify = function(metadata, notifyEntry) { + var regex + if(metadata.name && notifyEntry.packagePattern) { + regex = new RegExp(notifyEntry.packagePattern, notifyEntry.packagePatternFlags || '') + if(!regex.test(metadata.name)) { + return + } + } + + var template = Handlebars.compile(notifyEntry.content) + var content = template( metadata ) + + var options = { + body: content + } + + if ( notifyEntry.headers ) { + options.headers = notifyEntry.headers + } + + options.method = notifyEntry.method + + if(notifyEntry.endpoint) { + options.url = notifyEntry.endpoint + } + + request(options) +} + module.exports.notify = function(metadata, config) { - if (config.notify && config.notify.content) { - - var template = Handlebars.compile(config.notify.content) - var content = template( metadata ) - - var options = { - body: content + if (config.notify) { + if(config.notify.content) { + handleNotify(metadata, config.notify) } - - if ( config.notify.headers ) { - options.headers = config.notify.headers; + else { + for(var key in config.notify) { + if(config.notify.hasOwnProperty(key)) { + handleNotify(metadata, config.notify[key]) + } + } } - - options.method = config.notify.method; - - if(config.notify.endpoint) { - options.url = config.notify.endpoint - } - - request(options); - } }