-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
211 lines (188 loc) · 5.89 KB
/
index.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"use strict";
var soap = require("soap");
var _ = require("underscore");
var moment = require("moment");
var async = require("async");
var request = require("request");
// TODO: release this as a separate module.
var config = require("./config");
// TODO: this would probably be cleaner with a promise based API.
// Call callback with a SOAP client object that can talk to our
// servicenow instance.
function getClient(table, callback) {
// URL is based on http://wiki.servicenow.com/index.php?title=SOAP_Web_Service
var url = config.ROOT_URL + "/" + table + ".do?WSDL";
var username = process.env.HUBOT_SERVICENOW_USER;
var password = process.env.HUBOT_SERVICENOW_PASSWORD;
// We explicitly configure request, because some servicenow
// instances require authentication even to download the WSDL.
var requestWithCreds = request.defaults({
auth: {
user: username,
pass: password,
sendImmediately: false
}
});
soap.createClient(
url,
{
escapeXML: true,
request: requestWithCreds
},
function(err, client) {
if (err) {
callback(err);
return;
}
client.setSecurity(new soap.BasicAuthSecurity(username, password));
callback(null, client);
}
);
}
// Retrieve all the servicenow records this table that match parameters.
// Official docs: http://wiki.servicenow.com/index.php?title=SOAP_Direct_Web_Service_API#getRecords
function getRecordsWhere(table, parameters, callback) {
return getClient(table, function(err, client) {
if (err) {
callback(err);
return;
}
client.getRecords(parameters, function(err, result) {
if (err) {
callback(err);
return;
}
if (result && result.getRecordsResult) {
callback(null, result.getRecordsResult);
} else {
callback(null, []);
}
});
});
}
// Given an ID, e.g. 'CR1234', call the callback with an object describing
// all its properties.
function getRecordById(id, callback) {
// Arguments based on http://wiki.servicenow.com/index.php?title=SOAP_Direct_Web_Service_API#getRecords
var args = { number: id };
getRecordsWhere(config.tableName(id), args, function(err, records) {
if (err) {
callback(err);
return;
}
if (records && records.length > 0) {
callback(null, records[0]);
} else {
callback(null, null);
}
});
}
function recordId(record) {
return record.number;
}
// Given two arrays of tickets, return the combined array, sorted by date.
function mergeRecordArrays(array1, array2) {
var result = array1.concat(array2);
// Sort results newest first.
// TODO: this is sorting tickets incorrectly when we have multiple
// tickets for the same day. We should also sort by ID.
result = _.sortBy(result, function(item) {
var id = recordId(item);
var field = config.dateField(id);
return moment(item[field]).toDate();
});
return result.reverse();
}
// Get all records created by this username.
function recordsCreatedBy(creator, callback) {
// For every table type, get all the records created by the current user.
function recordsInTable(table, cb) {
return getRecordsWhere(table, { sys_created_by: creator }, cb);
}
// recordsInTable "change_request", callback
async.map(config.TABLE_NAMES, recordsInTable, function(err, results) {
if (err) {
callback(err);
return;
}
var merged = _.reduce(results, mergeRecordArrays);
callback(null, merged);
});
}
// Retrieve work notes for a sys ID. This does not work for record IDs
// (so '123456789123456789abcdef12345678' not 'CR1234').
function workNotesBySysId(sysId, callback) {
// Arguments based on https://community.servicenow.com/thread/163805
var args = {
element_id: sysId,
element: "work_notes"
};
getRecordsWhere("sys_journal_field", args, callback);
}
// Given an object specifying fields for a ticket, create it.
function createTicket(params, prefix, callback) {
getClient(config.tableName(prefix), function(err, client) {
if (err) {
callback(err, null);
return;
}
client.insert(params, callback);
});
}
// Find all items in this table that match the string `searchTerm`.
function searchRecords(table, searchTerm, callback) {
var prefix = config.prefixFromTableName(table);
var args = {
// This weird API is based on doing a search in the web UI, and extracting the encoded query,
// as documented in:
// http://wiki.servicenow.com/index.php?title=Encoded_Query_Strings#Generating_Encoded_Query_Strings_through_a_Filter
// plus the discussion here https://community.servicenow.com/thread/165927
__encoded_query: "123TEXTQUERY321=" + searchTerm,
// Other API parameters documented at
// http://wiki.servicenow.com/?title=Direct_Web_Services#Extended_Query_Parameters
__order_by_desc: config.dateField(prefix),
__limit: 15
};
getRecordsWhere(table + "_list", args, callback);
}
// Find all tickets matching the string `searchTerm`.
function search(searchTerm, callback) {
function searchRecordsInTable(table, cb) {
searchRecords(table, searchTerm, cb);
}
async.map(config.TABLE_NAMES, searchRecordsInTable, function(err, results) {
if (err) {
callback(err);
return;
}
var merged = _.reduce(results, mergeRecordArrays);
callback(null, merged);
});
}
// Find the servicenow details for the requested user.
// e.g. 'jsmith' -> {sys_id: '12345678901234abcdef', ...}
function getUser(queryUser, callback) {
return getRecordsWhere(
"sys_user",
{
user_name: queryUser
},
function(err, results) {
if (err) {
callback(err);
return;
}
callback(null, results[0]);
}
);
}
module.exports = {
recordId: recordId,
getRecordById: getRecordById,
recordsCreatedBy: recordsCreatedBy,
workNotesBySysId: workNotesBySysId,
createTicket: createTicket,
search: search,
getUser: getUser,
config: config
};