Skip to content

Commit 1ca5298

Browse files
committed
feat: added information about package publisher for notifications
1 parent 2208b4e commit 1ca5298

File tree

4 files changed

+96
-23
lines changed

4 files changed

+96
-23
lines changed

docs/notifications.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,32 @@ notify:
3838
content: '{"color":"green","message":"New package published: * {{ name }}*","notify":true,"message_format":"text"}'
3939
```
4040
41+
### Publisher information
42+
43+
You can access to the package publisher information in the `content` of a webhook using the `publisher` object.
44+
45+
See below the `publisher` object type:
46+
47+
```
48+
{
49+
name: string,
50+
groups: string[],
51+
real_groups: string[]
52+
}
53+
```
54+
55+
An example:
56+
57+
```
58+
notify:
59+
method: POST
60+
headers: [{'Content-Type': 'application/json'}]
61+
endpoint: https://usagge.hipchat.com/v2/room/3729485/notification?auth_token=mySecretToken
62+
content: '{"color":"green","message":"New package published: * {{ name }}*. Publisher name: * {{ publisher.name }} *.","notify":true,"message_format":"text"}'
63+
```
64+
65+
**Note:** it's not possible to get the publisher information if the `package.json` file already has the `publisher` property.
66+
4167
## Configuration
4268

4369
Property | Type | Required | Support | Default | Description
@@ -47,4 +73,4 @@ packagePattern| string | No | all | | Only run this notification if the package
4773
packagePatternFlags| string | No | all | | Any flags to be used with the regular expression
4874
headers| array/object | Yes | all | | If this endpoint requires specific headers, set them here as an array of key: value objects.
4975
endpoint| string | Yes | all | | set the URL endpoint for this call
50-
content| string | Yes | all | | any [handlebar](https://handlebarsjs.com/) expressions
76+
content| string | Yes | all | | any [handlebar](https://handlebarsjs.com/) expressions

src/api/endpoint/api/publish.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export default function(router: Router, auth: IAuth, storage: IStorageHandler, c
9191
if (err) {
9292
return next(err);
9393
}
94-
notify(metadata, config);
94+
notify(metadata, config, req.remote_user);
9595
res.status(201);
9696
return next({ok: ok_message, success: true});
9797
});

src/lib/notify.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const request = require('request');
33
const _ = require('lodash');
44
const logger = require('./logger');
55

6-
const handleNotify = function(metadata, notifyEntry) {
6+
const handleNotify = function(metadata, notifyEntry, publisherInfo) {
77
let regex;
88
if (metadata.name && notifyEntry.packagePattern) {
99
// FUTURE: comment out due https://github.com/verdaccio/verdaccio/pull/108#issuecomment-312421052
@@ -15,7 +15,12 @@ const handleNotify = function(metadata, notifyEntry) {
1515
}
1616

1717
const template = Handlebars.compile(notifyEntry.content);
18-
const content = template( metadata );
18+
19+
// don't override 'publisher' if package.json already has that
20+
if (!metadata.publisher) {
21+
metadata = {...metadata, publisher: publisherInfo};
22+
}
23+
const content = template(metadata);
1924

2025
const options = {
2126
body: content,
@@ -28,7 +33,7 @@ const handleNotify = function(metadata, notifyEntry) {
2833
if (Object.is(item, item)) {
2934
for (const key in item) {
3035
if (item.hasOwnProperty(key)) {
31-
header[key] = item[key];
36+
header[key] = item[key];
3237
}
3338
}
3439
}
@@ -40,34 +45,34 @@ const handleNotify = function(metadata, notifyEntry) {
4045

4146
options.method = notifyEntry.method;
4247

43-
if ( notifyEntry.endpoint ) {
48+
if (notifyEntry.endpoint) {
4449
options.url = notifyEntry.endpoint;
4550
}
4651

47-
return new Promise(( resolve, reject) => {
52+
return new Promise((resolve, reject) => {
4853
request(options, function(err, response, body) {
4954
if (err || response.statusCode >= 400) {
5055
const errorMessage = _.isNil(err) ? response.statusMessage : err;
51-
logger.logger.error({err: errorMessage}, ' notify error: @{err.message}' );
56+
logger.logger.error({err: errorMessage}, ' notify error: @{err.message}');
5257
reject(errorMessage);
5358
} else {
5459
logger.logger.info({content: content}, 'A notification has been shipped: @{content}');
5560
if (body) {
56-
logger.logger.debug({body: body}, ' body: @{body}' );
61+
logger.logger.debug({body: body}, ' body: @{body}');
5762
}
5863
resolve(_.isNil(body) === false ? body : null);
5964
}
6065
});
6166
});
6267
};
6368

64-
const notify = function(metadata, config) {
69+
const notify = function(metadata, config, publisherInfo) {
6570
if (config.notify) {
6671
if (config.notify.content) {
67-
return handleNotify(metadata, config.notify);
72+
return handleNotify(metadata, config.notify, publisherInfo);
6873
} else {
6974
// multiple notifications endpoints PR #108
70-
return Promise.all(_.map(config.notify, (key) => handleNotify(metadata, key)));
75+
return Promise.all(_.map(config.notify, (key) => handleNotify(metadata, key, publisherInfo)));
7176
}
7277
}
7378
};

test/functional/notifications/notify.js

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ export default function(express) {
1212
'Content-Type': HEADERS.JSON
1313
}],
1414
endpoint: "http://localhost:55550/api/notify",
15-
content: '{"color":"green","message":"New package published: * {{ name }}*","notify":true,"message_format":"text"}'
15+
content: '{"color":"green","message":"New package published: * {{ name }}*. Publisher name: * {{ publisher.name }} *.","notify":true,"message_format":"text"}'
1616
}
1717
};
1818

19+
const publisherInfo = {
20+
name: "publisher-name-test"
21+
};
22+
1923
describe('notifications', () => {
2024

2125
beforeAll(function () {
@@ -33,10 +37,14 @@ export default function(express) {
3337
name: "pkg-test"
3438
};
3539

36-
notify(metadata, config).then(function (body) {
40+
notify(metadata, config, publisherInfo).then(function (body) {
3741
const jsonBody = JSON.parse(body);
38-
assert.ok(`New package published: * ${metadata.name}*` === jsonBody.message,
39-
'Body notify message should be equal');
42+
assert.ok(
43+
`New package published: * ${metadata.name}*. Publisher name: * ${
44+
publisherInfo.name
45+
} *.` === jsonBody.message,
46+
"Body notify message should be equal"
47+
);
4048
done();
4149
}, function (err) {
4250
assert.fail(err);
@@ -54,10 +62,14 @@ export default function(express) {
5462
'Content-Type': HEADERS.JSON
5563
};
5664

57-
notify(metadata, configMultipleHeader).then(function (body) {
65+
notify(metadata, configMultipleHeader, publisherInfo).then(function (body) {
5866
const jsonBody = JSON.parse(body);
59-
assert.ok(`New package published: * ${metadata.name}*` === jsonBody.message,
60-
'Body notify message should be equal');
67+
assert.ok(
68+
`New package published: * ${metadata.name}*. Publisher name: * ${
69+
publisherInfo.name
70+
} *.` === jsonBody.message,
71+
"Body notify message should be equal"
72+
);
6173
done();
6274
}, function (err) {
6375
assert.fail(err);
@@ -85,11 +97,15 @@ export default function(express) {
8597
multipleNotificationsEndpoint.notify.push(notificationSettings);
8698
}
8799

88-
notify(metadata, multipleNotificationsEndpoint).then(function (body) {
100+
notify(metadata, multipleNotificationsEndpoint, publisherInfo).then(function (body) {
89101
body.forEach(function(notification) {
90102
const jsonBody = JSON.parse(notification);
91-
assert.ok(`New package published: * ${metadata.name}*` === jsonBody.message,
92-
'Body notify message should be equal');
103+
assert.ok(
104+
`New package published: * ${metadata.name}*. Publisher name: * ${
105+
publisherInfo.name
106+
} *.` === jsonBody.message,
107+
"Body notify message should be equal"
108+
);
93109
});
94110
done();
95111
}, function (err) {
@@ -105,7 +121,7 @@ export default function(express) {
105121
const configFail = _.cloneDeep(config);
106122
configFail.notify.endpoint = "http://localhost:55550/api/notify/bad";
107123

108-
notify(metadata, configFail).then(function () {
124+
notify(metadata, configFail, publisherInfo).then(function () {
109125
assert.equal(false, 'This service should fails with status code 400');
110126
done();
111127
}, function (err) {
@@ -114,5 +130,31 @@ export default function(express) {
114130
});
115131
});
116132

133+
test("publisher property should not be overridden if it exists in metadata", done => {
134+
const metadata = {
135+
name: "pkg-test",
136+
publisher: {
137+
name: "existing-publisher-name"
138+
}
139+
};
140+
141+
notify(metadata, config, publisherInfo).then(
142+
function(body) {
143+
const jsonBody = JSON.parse(body);
144+
assert.ok(
145+
`New package published: * ${metadata.name}*. Publisher name: * ${
146+
metadata.publisher.name
147+
} *.` === jsonBody.message,
148+
"Body notify message should be equal"
149+
);
150+
done();
151+
},
152+
function(err) {
153+
assert.fail(err);
154+
done();
155+
}
156+
);
157+
});
158+
117159
});
118160
}

0 commit comments

Comments
 (0)