Skip to content

Commit

Permalink
Merge pull request #214 from cognifloyd/slack-nbsp
Browse files Browse the repository at this point in the history
Handle slack injected nbsp
  • Loading branch information
blag authored May 24, 2021
2 parents 562c570 + e193858 commit e565c50
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ in development
--------------
* Update ``lodash`` and ``st2client`` dependencies (improvement)

0.12.0
------

* Clean slack-injected latin1 nbsp character (/xA0) from command to improve command recognition.
Slack will sometimes replace a space with /xA0, often near something that renders as a link.
Now, when someone copies that command, they can paste it and still have hubot recognize it.
(improvement) #214

0.11.2
------

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.11.3",
"version": "0.12.0",
"author": "StackStorm, Inc. <[email protected]>",
"license": "Apache-2.0",
"keywords": [
Expand Down
11 changes: 11 additions & 0 deletions src/lib/adapters/slack.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,15 @@ SlackAdapter.prototype.postData = function(data) {
}
};

SlackAdapter.prototype.normalizeCommand = function(command) {
var self = this;
command = SlackLikeAdapter.prototype.normalizeCommand.call(self, command);
// replace non-breaking space with regular space
// first the unicode variant, so we don't cut it in half,
// then the latin1 variant (thanks to slack)
command = command.replace(/\u00A0/g, ' ');
command = command.replace(/\xA0/g, ' ');
return command;
}

module.exports = SlackAdapter;
24 changes: 24 additions & 0 deletions test/test-formatdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,30 @@ describe('SlackFormatter', function() {
expect(o).to.equal('run remote \'uname -a\' \'localhost, 127.0.0.1\'');
});

// copy/paste a command in slack. Sometimes (esp w/ URLs) it replaces a space with this char.
it('should normalize command with slack-injected latin1 non-breaking space', function() {
var adapter = adapters.getAdapter(adapterName, robot);
var o = adapter.normalizeCommand('run local\xA0"uname -a"');
expect(o).to.be.an('string');
expect(o).to.equal('run local "uname -a"');
});

// slack injects the latin1 variant (above) but we can't break the unicode variant.
it('should normalize command with unicode non-breaking space', function() {
var adapter = adapters.getAdapter(adapterName, robot);
var o = adapter.normalizeCommand('run local\u00A0"uname -a"');
expect(o).to.be.an('string');
expect(o).to.equal('run local "uname -a"');
});

// not likely. Just making sure this doesn't mangle the unicode chars.
it('should normalize command with mixed latin1/unicode non-breaking space', function() {
var adapter = adapters.getAdapter(adapterName, robot);
var o = adapter.normalizeCommand('run\xA0\u00A0\xA0local\u00A0\xA0\u00A0"uname -a"');
expect(o).to.be.an('string');
expect(o).to.equal('run local "uname -a"');
});

it('should normalize the addressee', function() {
var adapter = adapters.getAdapter(adapterName, robot);
var msg = {
Expand Down

0 comments on commit e565c50

Please sign in to comment.