Add support for multiple notification endpoints to existing webhook system

This commit is contained in:
Ryan Graham 2017-01-10 08:30:56 +00:00
parent 014a0b53ae
commit f63e690b15
2 changed files with 64 additions and 19 deletions

View File

@ -134,6 +134,11 @@ notify:
# Choose a method. Technically this will accept any HTTP # Choose a method. Technically this will accept any HTTP
# request method, but probably stick to GET or POST # request method, but probably stick to GET or POST
method: 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 # If this endpoint requires specific headers, set them here
# as an array of key: value objects. # as an array of key: value objects.
headers: [{'Content-type': 'application/x-www-form-urlencoded'}] headers: [{'Content-type': 'application/x-www-form-urlencoded'}]
@ -146,3 +151,24 @@ notify:
content: ' {{ handlebar-expression }}' content: ' {{ handlebar-expression }}'
# For Slack, follow the following format: # For Slack, follow the following format:
# content: '{ "text": "Package *{{ name }}* published to version *{{ dist-tags.latest }}*", "username": "Verdaccio", "icon_emoji": ":package:" }' # 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:" }'

View File

@ -1,28 +1,47 @@
var Handlebars = require('handlebars') var Handlebars = require('handlebars')
var request = require('request') 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) { module.exports.notify = function(metadata, config) {
if (config.notify && config.notify.content) { if (config.notify) {
if(config.notify.content) {
var template = Handlebars.compile(config.notify.content) handleNotify(metadata, config.notify)
var content = template( metadata )
var options = {
body: content
} }
else {
if ( config.notify.headers ) { for(var key in config.notify) {
options.headers = config.notify.headers; 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);
} }
} }