Skip to content

Commit

Permalink
Merge pull request #138 from StackStorm/issue_137/slack_post_data
Browse files Browse the repository at this point in the history
Slack post data should use new chat postMessage API
  • Loading branch information
armab authored Aug 17, 2017
2 parents 05c7ab0 + 751f2be commit 4b96637
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 75 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.6.0
-----
* Update Slack to use new chat postMessage API from 'hubot-slack' v4

0.5.1
-----
* Update uuid to version 3.0.0
Expand Down
26 changes: 16 additions & 10 deletions lib/post_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,21 @@ function SlackDataPostHandler(robot, formatter) {
SlackDataPostHandler.prototype.postData = function(data) {
var recipient, attachment_color, split_message,
attachment, pretext = "";
var envelope;

if (data.whisper && data.user) {
recipient = data.user;
envelope = {
"user": data.user
};
} else {
recipient = data.channel;
pretext = (data.user && !data.whisper) ? util.format('@%s: ', data.user) : "";
envelope = {
"room": data.channel,
"id": data.channel,
"user": data.user,
};
}

if (data.extra && data.extra.color) {
Expand All @@ -62,21 +71,18 @@ SlackDataPostHandler.prototype.postData = function(data) {
var robot = this.robot;
var chunks = split_message.text.match(/[\s\S]{1,7900}/g);
var sendChunk = function (i) {
content.pretext = i === 0 ? pretext + split_message.pretext : null;
content.text = chunks[i];
content.fallback = chunks[i];
attachment = {
channel: recipient,
content: content,
text: i === 0 ? pretext + split_message.pretext : null
};
robot.emit('slack-attachment', attachment);
robot.adapter.client.send(envelope, {'attachments': [content]});

if (chunks.length > ++i) {
setTimeout(function(){ sendChunk(i); }, 300);
}
};
sendChunk(0);
} else {
this.robot.messageRoom.call(this.robot, recipient, pretext + split_message.pretext);
this.robot.adapter.client.send(envelope, pretext + split_message.pretext);
}
};

Expand Down Expand Up @@ -231,14 +237,14 @@ SparkDataPostHandler.prototype.postData = function(data) {
text = "";

if (data.whisper && data.user) {
recipient = { user: data.user }
recipient = { user: data.user };
} else {
recipient = { channel: data.channel }
recipient = { channel: data.channel };
text = (data.user && !data.whisper) ? util.format('%s: ', data.user) : "";
}

recipient = this.formatter.formatRecepient(recipient);
recipient.extra = data.extra
recipient.extra = data.extra;
text += this.formatter.formatData(data.message);

// Ignore the delimiter in the default formatter and just concat parts.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hubot-stackstorm",
"description": "A hubot plugin for integrating with StackStorm event-driven infrastructure automation platform.",
"version": "0.5.1",
"version": "0.6.0",
"author": "StackStorm, Inc. <[email protected]>",
"license": "Apache-2.0",
"keywords": [
Expand Down
34 changes: 34 additions & 0 deletions tests/dummy-adapters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Licensed to the StackStorm, Inc ('StackStorm') under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/


"use strict";

function MockSlackClient(logger) {
this.logger = logger;
}

MockSlackClient.prototype.send = function(envelope, message) {
this.logger.info('Sending ' + JSON.stringify(message) + ' to ' + JSON.stringify(envelope));
};

function MockSlackAdapter(logger) {
this.logger = logger;
this.client = new MockSlackClient(logger);
}

module.exports = MockSlackAdapter;
3 changes: 2 additions & 1 deletion tests/dummy-robot.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ function Logger(enabled) {

}

function Robot(name, enable_logging) {
function Robot(name, adapter, enable_logging) {
this.logger = new Logger(enable_logging);
this.name = name;
this.commands = [];
this.adapter = adapter;
}

module.exports = Robot;
2 changes: 1 addition & 1 deletion tests/test-formatdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('HipChatFormatter', function() {

describe('DefaultFormatter', function() {
var adapterName = 'unknown';
var robot = new DummyRobot('dummy', false);
var robot = new DummyRobot('dummy', null, false);

it('should create a snippet for non-empty', function() {
var formatter = formatData.getFormatter(adapterName, robot);
Expand Down
136 changes: 74 additions & 62 deletions tests/test-postdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ var chai = require("chai"),
env = process.env,
expect = chai.expect,
Robot = require('./dummy-robot.js'),
MockSlackAdapter = require('./dummy-adapters.js'),
Log = require('log'),
formatData = require('../lib/format_data.js'),
postData = require('../lib/post_data.js'),
sinon = require('sinon'),
Expand All @@ -34,16 +36,16 @@ chai.use(sinonChai);

describe("slack post data", function() {
var robot, formatter, postDataHandler;

robot = new Robot(false);
var logger = new Log('info');
robot = new Robot(false, new MockSlackAdapter(logger));
formatter = formatData.getFormatter('slack', robot);
postDataHandler = postData.getDataPostHandler('slack', robot, formatter);

env.ST2_SLACK_SUCCESS_COLOR = 'dfdfdf';
env.ST2_SLACK_FAIL_COLOR = 'danger';

it('should post success formatted slack-attachment', function() {
robot.emit = sinon.spy();
it('should post success formatted slack attachment', function() {
robot.adapter.client.send = sinon.spy();
var input = {
user: 'stanley',
channel: '#stackstorm',
Expand All @@ -54,23 +56,25 @@ describe("slack post data", function() {
whisper: false
};
postDataHandler.postData(input);
expect(robot.emit).to.have.been.calledOnce;
expect(robot.emit).to.have.been.calledWith(
'slack-attachment', {
channel: input.channel,
content: {
color: env.ST2_SLACK_SUCCESS_COLOR,
mrkdwn_in: ["text", "pretext"],
text: input.message,
fallback: input.message
},
text: "@stanley: "
expect(robot.adapter.client.send).to.have.been.calledOnce;
expect(robot.adapter.client.send).to.have.been.calledWith(
{ "id": "#stackstorm", "room": "#stackstorm", "user": "stanley" },
{
"attachments":[
{
"color": "dfdfdf",
"mrkdwn_in": ["text","pretext"],
"pretext": "@stanley: ",
"text": input.message,
"fallback": input.message
}
]
}
);
});

it('should post fail formatted slack-attachment', function() {
robot.emit = sinon.spy();
it('should post fail formatted slack attachment', function() {
robot.adapter.client.send = sinon.spy();
var input = {
user: 'stanley',
channel: '#stackstorm',
Expand All @@ -81,24 +85,26 @@ describe("slack post data", function() {
whisper: false
};
postDataHandler.postData(input);
expect(robot.emit).to.have.been.calledOnce;
expect(robot.emit).to.have.been.calledWith(
'slack-attachment', {
channel: input.channel,
content: {
color: env.ST2_SLACK_FAIL_COLOR,
mrkdwn_in: ["text", "pretext"],
text: input.message,
fallback: input.message
},
text: "@stanley: "
expect(robot.adapter.client.send).to.have.been.calledOnce;
expect(robot.adapter.client.send).to.have.been.calledWith(
{ "id": "#stackstorm", "room": "#stackstorm", "user": "stanley" },
{
"attachments": [
{
"color": "danger",
"text": input.message,
"fallback": input.message,
"mrkdwn_in": ["text", "pretext"],
"pretext": "@stanley: "
}
]
}
);
});

it('should split a long slack-attachment into chunks', function() {
this.clock = sinon.useFakeTimers();
robot.emit = sinon.spy();
robot.adapter.client.send = sinon.spy();
var input = {
user: 'stanley',
channel: '#stackstorm',
Expand All @@ -107,37 +113,41 @@ describe("slack post data", function() {
};
var chunks = input.message.match(/[\s\S]{1,7900}/g);
postDataHandler.postData(input);
expect(robot.emit).to.have.been.calledWith(
'slack-attachment', {
channel: input.channel,
content: {
color: env.ST2_SLACK_SUCCESS_COLOR,
mrkdwn_in: ["text", "pretext"],
text: chunks[0],
fallback: chunks[0]
},
text: "@stanley: "
expect(robot.adapter.client.send).to.have.been.calledWith(
{ "id": "#stackstorm", "room": "#stackstorm", "user": "stanley" },
{
"attachments": [
{
"color": env.ST2_SLACK_SUCCESS_COLOR,
"mrkdwn_in": ["text", "pretext"],
"pretext": "@stanley: ",
"text": chunks[0],
"fallback": chunks[0]
}
]
}
);
this.clock.tick(500);
expect(robot.emit).to.have.been.calledWith(
'slack-attachment', {
channel: input.channel,
content: {
color: env.ST2_SLACK_SUCCESS_COLOR,
mrkdwn_in: ["text", "pretext"],
text: chunks[1],
fallback: chunks[1]
},
text: null
expect(robot.adapter.client.send).to.have.been.calledWith(
{ "id": "#stackstorm", "room": "#stackstorm", "user": "stanley" },
{
"attachments": [
{
"pretext": null,
"color": env.ST2_SLACK_SUCCESS_COLOR,
"mrkdwn_in": ["text", "pretext"],
"text": chunks[1],
"fallback": chunks[1]
}
]
}
);
expect(robot.emit).to.have.been.calledTwice;
expect(robot.adapter.client.send).to.have.been.calledTwice;
this.clock.restore();
});

it('should whisper a slack-attachment', function() {
robot.emit = sinon.spy();
robot.adapter.client.send = sinon.spy();
var input = {
user: 'stanley',
channel: '#stackstorm',
Expand All @@ -148,17 +158,19 @@ describe("slack post data", function() {
whisper: true
};
postDataHandler.postData(input);
expect(robot.emit).to.have.been.calledOnce;
expect(robot.emit).to.have.been.calledWith(
'slack-attachment', {
channel: input.user,
content: {
color: env.ST2_SLACK_SUCCESS_COLOR,
mrkdwn_in: ["text", "pretext"],
text: input.message,
fallback: input.message
},
text: ""
expect(robot.adapter.client.send).to.have.been.calledOnce;
expect(robot.adapter.client.send).to.have.been.calledWith(
{ "user": "stanley" },
{
"attachments":[
{
"color": "dfdfdf",
"mrkdwn_in": ["text","pretext"],
"pretext": "",
"text": input.message,
"fallback": input.message
}
]
}
);
});
Expand Down

0 comments on commit 4b96637

Please sign in to comment.