Skip to content

Commit 25e5ff4

Browse files
committed
slack escaping
1 parent 902c6f4 commit 25e5ff4

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

lib/models/notifications/notifier.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ Handlebars.registerHelper('encode', function (str) {
2727
return encodeURIComponent(str);
2828
});
2929

30+
/**
31+
* Slack requires light escaping with just 3 rules:
32+
* & replaced with &
33+
* < replaced with &lt;
34+
* > replaced with &gt;
35+
*/
36+
var ampRegExp = new RegExp('&', 'g');
37+
var ltRegExp = new RegExp('<', 'g');
38+
var gtRegExp = new RegExp('>', 'g');
39+
Handlebars.registerHelper('slackEscape', function (str) {
40+
return str.replace(ampRegExp, '&amp;').replace(ltRegExp, '&lt;').replace(gtRegExp, '&gt;');
41+
});
3042

3143
var onBuildTpls = {};
3244
var onInstanceTpls = {};

templates/slack_on_build.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
{{user.login}}'s <{{headCommit.url}}|changes> ({{headCommit.message}}{{{moreChangesSlack repo commitLog}}}) to {{repo}} ({{branch}}) are ready.
1+
{{user.login}}'s <{{headCommit.url}}|changes> ({{{slackEscape headCommit.message}}}{{{moreChangesSlack repo commitLog}}}) to {{repo}} ({{branch}}) are ready.
22
<http://{{domain}}/{{owner.login}}/boxSelection/{{repoName}}/{{encode branch}}/{{encode headCommit.message}}/{{headCommit.id}}|Choose a server to run {{branch}}>.

templates/slack_on_instances.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{user.login}}'s <{{headCommit.url}}|changes> ({{headCommit.message}}{{{moreChangesSlack repo commitLog}}}) to {{repo}} ({{branch}}) are deployed on servers:
1+
{{user.login}}'s <{{headCommit.url}}|changes> ({{{slackEscape headCommit.message}}}{{{moreChangesSlack repo commitLog}}}) to {{repo}} ({{branch}}) are deployed on servers:

unit/notifier.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,18 @@ describe('Notifier', function () {
4545
slack.send = function (text, cb) {
4646
var message = 'podviaznikov\'s ';
4747
message += '<' + headCommit.url + '|changes>';
48-
message += ' (init me) to CodeNow/api (develop) are ready.\n';
48+
message += ' (init &amp; commit &amp; push) to CodeNow/api (develop) are ready.\n';
4949
message += '<http://runnable3.net/';
50-
message += 'podviaznikov/boxSelection/api/develop/init%20me/a240edf982d467201845b3bf10ccbe16f6049ea9';
50+
message += 'podviaznikov/boxSelection/api/develop/init%20%26%20commit%20%26%20push';
51+
message += '/a240edf982d467201845b3bf10ccbe16f6049ea9';
5152
message += '|Choose a server to run develop>.';
5253
expect(text).to.equal(message);
5354
cb();
5455
};
5556

5657
var headCommit = {
5758
id: 'a240edf982d467201845b3bf10ccbe16f6049ea9',
58-
message: 'init me',
59+
message: 'init & commit & push',
5960
url: 'https://github.com/CodeNow/api/commit/a240edf982d467201845b3bf10ccbe16f6049ea9'
6061
};
6162
var githubPushInfo = {
@@ -81,7 +82,8 @@ describe('Notifier', function () {
8182
slack.send = function (message, cb) {
8283
var text = 'tjmehta\'s ';
8384
text += '<' + headCommit.url + '|changes>';
84-
text += ' (init repo and <https://github.com/CodeNow/api/compare/b240edf982d4...a240edf982d4|1 more>)';
85+
text += ' (init &amp; commit &lt;p&gt;Hello&lt;/p&gt; and ';
86+
text += '<https://github.com/CodeNow/api/compare/b240edf982d4...a240edf982d4|1 more>)';
8587
text += ' to CodeNow/api (develop) are deployed on servers:';
8688
expect(text).to.equal(message.text);
8789
expect(message.attachments.length).to.equal(1);
@@ -101,7 +103,7 @@ describe('Notifier', function () {
101103
];
102104
var headCommit = {
103105
id: 'b240edf982d467201845b3bf10bbbe16f6049eb1',
104-
message: 'init repo',
106+
message: 'init & commit <p>Hello</p>',
105107
url: 'https://github.com/CodeNow/api/commit/b240edf982d467201845b3bf10bbbe16f6049eb1'
106108
};
107109
var githubPushInfo = {
@@ -202,7 +204,7 @@ describe('Notifier', function () {
202204
hipchat.notifyOnInstances(githubPushInfo, instances, done);
203205
});
204206

205-
it('should send message to HipChat', {timeout: 2000}, function (done) {
207+
it('should send message to HipChat', {timeout: 4000}, function (done) {
206208
var hipchat = new HipChat({authToken: 'a4bcd2c7007379398f5158d7785fa0', roomId: '1076330'});
207209
var randomUsername = 'user' + uuid();
208210
var instances = [
@@ -250,7 +252,7 @@ describe('Notifier', function () {
250252
});
251253
done();
252254
});
253-
}, 800);
255+
}, 2200);
254256
});
255257
});
256258
});

0 commit comments

Comments
 (0)