Skip to content

Commit 9815ee7

Browse files
authored
Merge pull request #192 from StackStorm/fix-mattermost-adapter
Fix Mattermost adapter
2 parents 11aa5c2 + 44ca041 commit 9815ee7

File tree

5 files changed

+321
-225
lines changed

5 files changed

+321
-225
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ in development
1111
(improvement)
1212
* Split out the functionality of ``src/stackstorm.js`` into ``stackstorm_api.js`` and refactor it
1313
to be a JS old style class with a wrapper [PR #187, PR #190] (improvement)
14+
* Fix the Mattermost adapter to use the ``adapter.post`` function instead of emitting the
15+
``slack-attachment`` event [PR #192] (bug fix)
1416

1517
0.9.6
1618
-----

src/lib/adapters/mattermost.js

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
var env = process.env;
1818
var util = require('util');
1919
var utils = require('./../utils');
20+
var messages = require('./../slack-messages');
2021
var SlackLikeAdapter = require('./slack-like');
2122

2223

@@ -29,15 +30,21 @@ util.inherits(MattermostAdapter, SlackLikeAdapter);
2930

3031
MattermostAdapter.prototype.postData = function(data) {
3132
var self = this;
32-
33-
var recipient, attachment_color, split_message,
33+
var attachment_color, split_message, envelope,
3434
attachment, pretext = "";
35+
// We capture robot here so the `sendChunk` closure captures the correct
36+
// `this`
37+
var robot = self.robot;
3538

3639
// If we are supposed to whisper to a single user, use a direct message
3740
if (data.whisper && data.user) {
38-
recipient = data.user;
41+
envelope = {
42+
room: data.user
43+
};
3944
} else { // Otherwise, message the channel
40-
recipient = data.channel;
45+
envelope = {
46+
room: data.channel
47+
};
4148
// If we aren't supposed to whisper, then we at least at-mention the user
4249
pretext = (data.user && !data.whisper) ? util.format('@%s: ', data.user) : "";
4350
}
@@ -56,7 +63,6 @@ MattermostAdapter.prototype.postData = function(data) {
5663
}
5764

5865
split_message = utils.splitMessage(self.formatData(data.message));
59-
6066
if (split_message.text) {
6167
// Default values
6268
var content = {
@@ -65,12 +71,57 @@ MattermostAdapter.prototype.postData = function(data) {
6571
};
6672
// Override the default values with values from `data.extra.mattermost`
6773
if (data.extra && data.extra.mattermost) {
74+
// Backwards compatibility
75+
76+
// Action:
77+
//
78+
// result:
79+
// format: ...
80+
// message: Message text
81+
// extra:
82+
// mattermost:
83+
// author_name: Jira_Bot
84+
// author_link: "https://stackstorm.com"
85+
// author_icon: "https://stackstorm.com/favicon.ico"
86+
// color: "#042A60"
87+
// fallback: "Info about Jira ticket {{ execution.result.result.key }}"
88+
// title: "{{ execution.result.result.key }}"
89+
// title_link: "{{ execution.result.result.url }}"
90+
// fields:
91+
// -
92+
// title: Summary
93+
// value: "{{ execution.result.result.summary }}"
94+
// short: false
95+
//
96+
// becomes:
97+
//
98+
// {
99+
// "message": "Message text",
100+
// "props": {
101+
// "attachments": [
102+
// {
103+
// "author_name": "Jira Bot",
104+
// "author_link": "https://stackstorm.com",
105+
// "author_icon": "https://stackstorm.com/favicon.ico",
106+
// "color": "#042A60",
107+
// "fallback": "Info about Jira ticket {{ execution.result.result.key }}",
108+
// "title": "{{ execution.result.result.key }}",
109+
// "title_link": "{{ execution.result.result.url }}",
110+
// "fields": [
111+
// {
112+
// "title": "Summary",
113+
// "value": "{{ execution.result.result.summary }}",
114+
// "short": false
115+
// }
116+
// ]
117+
// }
118+
// ]
119+
// }
120+
// }
121+
68122
for (var attrname in data.extra.mattermost) { content[attrname] = data.extra.mattermost[attrname]; }
69123
}
70124

71-
// We capture robot here so the `sendMessage` closure captures the correct
72-
// `this`
73-
var robot = self.robot;
74125
var chunks = split_message.text.match(/[\s\S]{1,3800}/g);
75126

76127
// We define a recursive closure that calls itself with the next data to
@@ -85,23 +136,25 @@ MattermostAdapter.prototype.postData = function(data) {
85136
Inorder to accept the matteruser adapter message attachments changed the attachement json
86137
*/
87138

88-
attachment = {
89-
room: recipient,
90-
attachments: content.attachments ? content.attachments : content,
139+
var message = {
140+
props: {
141+
attachments: (content.attachments ? content.attachments : [content])
142+
},
91143
// There is likely a bug here - `split_message.text` being a true-y
92144
// value does not imply that `split_message.pretext` is also non-empty,
93145
// but we unconditionally set `text` to
94146
// `pretext + split_message.pretext` on the first message
95-
text: i === 0 ? pretext + split_message.pretext : null
147+
message: i === 0 ? pretext + split_message.pretext : null
96148
};
97-
robot.emit('slack-attachment', attachment);
149+
150+
robot.adapter.send(envelope, message);
98151
if (chunks.length > ++i) {
99152
setTimeout(function(){ sendChunk(i); }, 300);
100153
}
101154
};
102155
sendChunk(0);
103156
} else {
104-
self.robot.messageRoom.call(self.robot, recipient, pretext + split_message.pretext);
157+
robot.messageRoom.call(self.robot, envelope.room, pretext + split_message.pretext);
105158
}
106159
};
107160

src/lib/slack-messages.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
var utils = require('./utils.js');
1818

19+
// NOTE: This is being reused between the Slack adapter and the Mattermost
20+
// adapter. If we need to tweak it for Mattermost, we should refactor it
21+
// into a class, then inherit from it and override specific methods in
22+
// Mattermost adapter.
23+
1924
var buildMessagesWithChunkedFieldValue = function (msg) {
2025
var msgs;
2126

test/dummy-adapters.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ function SlackBot(logger) {
2727
this.client = new MockSlackClient(logger);
2828
}
2929

30+
function MockMattermostAdapter(logger) {
31+
this.logger = logger;
32+
}
33+
34+
MockMattermostAdapter.prototype.send = function(envelope, message) {
35+
this.logger.debug('Sending ' + JSON.stringify(message) + ' to ' + JSON.stringify(envelope));
36+
};
37+
3038
function MockBotFrameworkAdapter(logger) {
3139
this.logger = logger;
3240
}
@@ -35,5 +43,8 @@ MockBotFrameworkAdapter.prototype.send = function(envelope, message) {
3543
this.logger.info('Sending ' + JSON.stringify(message) + ' to ' + JSON.stringify(envelope));
3644
};
3745

38-
module.exports.MockSlackAdapter = SlackBot;
39-
module.exports.MockBotFrameworkAdapter = MockBotFrameworkAdapter;
46+
module.exports = {
47+
MockSlackAdapter: SlackBot,
48+
MockMattermostAdapter: MockMattermostAdapter,
49+
MockBotFrameworkAdapter: MockBotFrameworkAdapter
50+
};

0 commit comments

Comments
 (0)