mirror of
https://github.com/verdaccio/verdaccio.git
synced 2024-12-16 21:56:25 -05:00
Merge pull request #748 from ilyaztsv/feature-user-info-for-notications
feat: added information about package publisher for notifications
This commit is contained in:
commit
30f1ca1c1d
4 changed files with 96 additions and 23 deletions
|
@ -38,6 +38,32 @@ notify:
|
|||
content: '{"color":"green","message":"New package published: * {{ name }}*","notify":true,"message_format":"text"}'
|
||||
```
|
||||
|
||||
### Publisher information
|
||||
|
||||
You can access to the package publisher information in the `content` of a webhook using the `publisher` object.
|
||||
|
||||
See below the `publisher` object type:
|
||||
|
||||
```
|
||||
{
|
||||
name: string,
|
||||
groups: string[],
|
||||
real_groups: string[]
|
||||
}
|
||||
```
|
||||
|
||||
An example:
|
||||
|
||||
```
|
||||
notify:
|
||||
method: POST
|
||||
headers: [{'Content-Type': 'application/json'}]
|
||||
endpoint: https://usagge.hipchat.com/v2/room/3729485/notification?auth_token=mySecretToken
|
||||
content: '{"color":"green","message":"New package published: * {{ name }}*. Publisher name: * {{ publisher.name }} *.","notify":true,"message_format":"text"}'
|
||||
```
|
||||
|
||||
**Note:** it's not possible to get the publisher information if the `package.json` file already has the `publisher` property.
|
||||
|
||||
## Configuration
|
||||
|
||||
Property | Type | Required | Support | Default | Description
|
||||
|
|
|
@ -91,7 +91,7 @@ export default function(router: Router, auth: IAuth, storage: IStorageHandler, c
|
|||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
notify(metadata, config);
|
||||
notify(metadata, config, req.remote_user);
|
||||
res.status(201);
|
||||
return next({ok: ok_message, success: true});
|
||||
});
|
||||
|
|
|
@ -3,7 +3,7 @@ const request = require('request');
|
|||
const _ = require('lodash');
|
||||
const logger = require('./logger');
|
||||
|
||||
const handleNotify = function(metadata, notifyEntry) {
|
||||
const handleNotify = function(metadata, notifyEntry, publisherInfo) {
|
||||
let regex;
|
||||
if (metadata.name && notifyEntry.packagePattern) {
|
||||
// FUTURE: comment out due https://github.com/verdaccio/verdaccio/pull/108#issuecomment-312421052
|
||||
|
@ -15,7 +15,12 @@ const handleNotify = function(metadata, notifyEntry) {
|
|||
}
|
||||
|
||||
const template = Handlebars.compile(notifyEntry.content);
|
||||
const content = template( metadata );
|
||||
|
||||
// don't override 'publisher' if package.json already has that
|
||||
if (!metadata.publisher) {
|
||||
metadata = {...metadata, publisher: publisherInfo};
|
||||
}
|
||||
const content = template(metadata);
|
||||
|
||||
const options = {
|
||||
body: content,
|
||||
|
@ -40,20 +45,20 @@ const handleNotify = function(metadata, notifyEntry) {
|
|||
|
||||
options.method = notifyEntry.method;
|
||||
|
||||
if ( notifyEntry.endpoint ) {
|
||||
if (notifyEntry.endpoint) {
|
||||
options.url = notifyEntry.endpoint;
|
||||
}
|
||||
|
||||
return new Promise(( resolve, reject) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
request(options, function(err, response, body) {
|
||||
if (err || response.statusCode >= 400) {
|
||||
const errorMessage = _.isNil(err) ? response.statusMessage : err;
|
||||
logger.logger.error({err: errorMessage}, ' notify error: @{err.message}' );
|
||||
logger.logger.error({err: errorMessage}, ' notify error: @{err.message}');
|
||||
reject(errorMessage);
|
||||
} else {
|
||||
logger.logger.info({content: content}, 'A notification has been shipped: @{content}');
|
||||
if (body) {
|
||||
logger.logger.debug({body: body}, ' body: @{body}' );
|
||||
logger.logger.debug({body: body}, ' body: @{body}');
|
||||
}
|
||||
resolve(_.isNil(body) === false ? body : null);
|
||||
}
|
||||
|
@ -61,13 +66,13 @@ const handleNotify = function(metadata, notifyEntry) {
|
|||
});
|
||||
};
|
||||
|
||||
const notify = function(metadata, config) {
|
||||
const notify = function(metadata, config, publisherInfo) {
|
||||
if (config.notify) {
|
||||
if (config.notify.content) {
|
||||
return handleNotify(metadata, config.notify);
|
||||
return handleNotify(metadata, config.notify, publisherInfo);
|
||||
} else {
|
||||
// multiple notifications endpoints PR #108
|
||||
return Promise.all(_.map(config.notify, (key) => handleNotify(metadata, key)));
|
||||
return Promise.all(_.map(config.notify, (key) => handleNotify(metadata, key, publisherInfo)));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -12,10 +12,14 @@ export default function(express) {
|
|||
'Content-Type': HEADERS.JSON
|
||||
}],
|
||||
endpoint: "http://localhost:55550/api/notify",
|
||||
content: '{"color":"green","message":"New package published: * {{ name }}*","notify":true,"message_format":"text"}'
|
||||
content: '{"color":"green","message":"New package published: * {{ name }}*. Publisher name: * {{ publisher.name }} *.","notify":true,"message_format":"text"}'
|
||||
}
|
||||
};
|
||||
|
||||
const publisherInfo = {
|
||||
name: "publisher-name-test"
|
||||
};
|
||||
|
||||
describe('notifications', () => {
|
||||
|
||||
beforeAll(function () {
|
||||
|
@ -33,10 +37,14 @@ export default function(express) {
|
|||
name: "pkg-test"
|
||||
};
|
||||
|
||||
notify(metadata, config).then(function (body) {
|
||||
notify(metadata, config, publisherInfo).then(function (body) {
|
||||
const jsonBody = JSON.parse(body);
|
||||
assert.ok(`New package published: * ${metadata.name}*` === jsonBody.message,
|
||||
'Body notify message should be equal');
|
||||
assert.ok(
|
||||
`New package published: * ${metadata.name}*. Publisher name: * ${
|
||||
publisherInfo.name
|
||||
} *.` === jsonBody.message,
|
||||
"Body notify message should be equal"
|
||||
);
|
||||
done();
|
||||
}, function (err) {
|
||||
assert.fail(err);
|
||||
|
@ -54,10 +62,14 @@ export default function(express) {
|
|||
'Content-Type': HEADERS.JSON
|
||||
};
|
||||
|
||||
notify(metadata, configMultipleHeader).then(function (body) {
|
||||
notify(metadata, configMultipleHeader, publisherInfo).then(function (body) {
|
||||
const jsonBody = JSON.parse(body);
|
||||
assert.ok(`New package published: * ${metadata.name}*` === jsonBody.message,
|
||||
'Body notify message should be equal');
|
||||
assert.ok(
|
||||
`New package published: * ${metadata.name}*. Publisher name: * ${
|
||||
publisherInfo.name
|
||||
} *.` === jsonBody.message,
|
||||
"Body notify message should be equal"
|
||||
);
|
||||
done();
|
||||
}, function (err) {
|
||||
assert.fail(err);
|
||||
|
@ -85,11 +97,15 @@ export default function(express) {
|
|||
multipleNotificationsEndpoint.notify.push(notificationSettings);
|
||||
}
|
||||
|
||||
notify(metadata, multipleNotificationsEndpoint).then(function (body) {
|
||||
notify(metadata, multipleNotificationsEndpoint, publisherInfo).then(function (body) {
|
||||
body.forEach(function(notification) {
|
||||
const jsonBody = JSON.parse(notification);
|
||||
assert.ok(`New package published: * ${metadata.name}*` === jsonBody.message,
|
||||
'Body notify message should be equal');
|
||||
assert.ok(
|
||||
`New package published: * ${metadata.name}*. Publisher name: * ${
|
||||
publisherInfo.name
|
||||
} *.` === jsonBody.message,
|
||||
"Body notify message should be equal"
|
||||
);
|
||||
});
|
||||
done();
|
||||
}, function (err) {
|
||||
|
@ -105,7 +121,7 @@ export default function(express) {
|
|||
const configFail = _.cloneDeep(config);
|
||||
configFail.notify.endpoint = "http://localhost:55550/api/notify/bad";
|
||||
|
||||
notify(metadata, configFail).then(function () {
|
||||
notify(metadata, configFail, publisherInfo).then(function () {
|
||||
assert.equal(false, 'This service should fails with status code 400');
|
||||
done();
|
||||
}, function (err) {
|
||||
|
@ -114,5 +130,31 @@ export default function(express) {
|
|||
});
|
||||
});
|
||||
|
||||
test("publisher property should not be overridden if it exists in metadata", done => {
|
||||
const metadata = {
|
||||
name: "pkg-test",
|
||||
publisher: {
|
||||
name: "existing-publisher-name"
|
||||
}
|
||||
};
|
||||
|
||||
notify(metadata, config, publisherInfo).then(
|
||||
function(body) {
|
||||
const jsonBody = JSON.parse(body);
|
||||
assert.ok(
|
||||
`New package published: * ${metadata.name}*. Publisher name: * ${
|
||||
metadata.publisher.name
|
||||
} *.` === jsonBody.message,
|
||||
"Body notify message should be equal"
|
||||
);
|
||||
done();
|
||||
},
|
||||
function(err) {
|
||||
assert.fail(err);
|
||||
done();
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue