mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-01-06 22:40:26 -05:00
Add support for multiple notification endpoints to existing webhook system
This commit is contained in:
parent
014a0b53ae
commit
f63e690b15
2 changed files with 64 additions and 19 deletions
|
@ -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:" }'
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue