-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathformat_data.js
More file actions
126 lines (106 loc) · 3.47 KB
/
format_data.js
File metadata and controls
126 lines (106 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
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;
/*
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;
};
/*
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;
};
/*
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 = {
'slack': SlackFormatter,
'hipchat': HipChatFormatter,
'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);
};