UNPKG

hubot-stackstorm-rocketchat

Version:

A hubot plugin for integrating with StackStorm event-driven infrastructure automation platform with support for RocketChat

186 lines (157 loc) 5.17 kB
/* 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"; var _ = require('lodash'), truncate = require('truncate'), util = require('util'), utils = require('./utils.js'); var env = process.env; /* HipChatFormatter. */ function HipChatFormatter(robot) { this.robot = robot; } HipChatFormatter.prototype.formatData = function(data) { if (utils.isNull(data)) { return ""; } // HipChat has "show more" capability in messages so no truncation. return '/code ' + data; }; HipChatFormatter.prototype.formatRecepient = function(recepient) { var robot_name = env.HUBOT_HIPCHAT_JID.split("_")[0]; var hipchat_domain = (env.HUBOT_HIPCHAT_XMPP_DOMAIN === 'btf.hipchat.com') ? 'conf.btf.hipchat.com' : 'conf.hipchat.com'; return util.format('%s_%s@%s', robot_name, recepient, hipchat_domain); }; HipChatFormatter.prototype.normalizeCommand = function(command) { return command; }; /* MattermostFormatter. */ function MattermostFormatter(robot) { this.robot = robot; } MattermostFormatter.prototype.formatData = function(data) { if (utils.isNull(data)) { return ""; } // For slack we do not truncate or format the result. This is because // data is posted to slack as a message attachment. return data; }; MattermostFormatter.prototype.formatRecepient = function(recepient) { return recepient; }; MattermostFormatter.prototype.normalizeCommand = function(command) { // replace left double quote with regular quote command = command.replace(/\u201c/g, '\u0022'); // replace right double quote with regular quote command = command.replace(/\u201d/g, '\u0022'); // replace left single quote with regular apostrophe command = command.replace(/\u2018/g, '\u0027'); // replace right single quote with regular apostrophe command = command.replace(/\u2019/g, '\u0027'); return command; }; /* RocketChatFormatter. */ function RocketChatFormatter(robot) { this.robot = robot; } RocketChatFormatter.prototype.formatData = function(data) { if (utils.isNull(data)) { return ""; } // For Rocket we do not truncate or format the result. This is because // data is posted to RocketChat as a message attachment. return data; }; RocketChatFormatter.prototype.formatRecepient = function(recepient) { return recepient; }; RocketChatFormatter.prototype.normalizeCommand = function(command) { return command; }; /* SlackFormatter. */ function SlackFormatter(robot) { this.robot = robot; } SlackFormatter.prototype.formatData = function(data) { if (utils.isNull(data)) { return ""; } // For slack we do not truncate or format the result. This is because // data is posted to slack as a message attachment. return data; }; SlackFormatter.prototype.formatRecepient = function(recepient) { return recepient; }; SlackFormatter.prototype.normalizeCommand = function(command) { // replace left double quote with regular quote command = command.replace(/\u201c/g, '\u0022'); // replace right double quote with regular quote command = command.replace(/\u201d/g, '\u0022'); // replace left single quote with regular apostrophe command = command.replace(/\u2018/g, '\u0027'); // replace right single quote with regular apostrophe command = command.replace(/\u2019/g, '\u0027'); return command; }; /* DefaultFormatter. */ function DefaultFormatter(robot) { this.robot = robot; // Limit the size of a message. this.truncate_length = env.ST2_MAX_MESSAGE_LENGTH; } DefaultFormatter.prototype.formatData = function(data) { if (utils.isNull(data)) { return ""; } if (this.truncate_length > 0) { data = truncate(data, this.truncate_length); } return data; }; DefaultFormatter.prototype.formatRecepient = function(recepient) { return recepient; }; DefaultFormatter.prototype.normalizeCommand = function(command) { return command; }; var formatters = { 'hipchat': HipChatFormatter, 'mattermost': MattermostFormatter, 'matteruser': MattermostFormatter, 'rocketchat': RocketChatFormatter, 'slack': SlackFormatter, 'default': DefaultFormatter }; module.exports.getFormatter = function(adapterName, robot) { if (!(adapterName in formatters)) { robot.logger.warning( util.format('No supported formatter found for %s. Using DefaultFormatter.', adapterName)); adapterName = 'default'; } return new formatters[adapterName](robot); };