forked from jmbauguess/ServiceNowScriptDocumenter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
176 lines (170 loc) · 7.35 KB
/
index.js
File metadata and controls
176 lines (170 loc) · 7.35 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
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
(function () {
var fs = require('fs'),
request = require('request'),
options = require('minimist')(process.argv.slice(2));
if (process.argv.length < 4) {
console.log("Please enter {instance} {username} {password}");
process.exit(1);
}
var include = "sys_updated_by=162107",
requestsToMake = [
{
"type": "sys_script_include",
"filter" : include,
"extension" : ".js"
},
{
"type": "sys_ui_script",
"filter" : include,
"extension" : ".js"
},
{
"type": "u_scheduled_unit_test",
"filter" : include,
"extension" : ".js"
},
{
"type": "sys_update_set",
"filter" : "name=" + options.update_set,
"extension" : ".html"
}
];
for (var req in requestsToMake) {
if (requestsToMake.hasOwnProperty(req)) {
var current = requestsToMake[req];
makeRequest(current.type, current.filter, current.extension);
}
}
saveRemoteUpdateSet(options.update_set);
/**
* @description Main method - Makes HTTP Get Request to ServiceNow
* @param {string} scriptType A type of script in ServiceNow
* @param {string} include A query to add to the HTTP Request
* @param {string} extension the file extension to give to the file
*/
function makeRequest(scriptType, include, extension) {
request.get('http://' + options.instance + '.service-now.com/' +
scriptType + '.do?JSON&sysparm_query=' + include,
function(error, response, body) {
try {
var data = JSON.parse(body);
var records = data.records;
for (var i = 0; i < records.length; i++) {
checkDirectory(records[i], scriptType, extension);
}
} catch (e) {
console.log('oops - ' + e)
}
}).auth(options.username, options.password, false);
}
/**
* @description Getting an actual copy of remote update set requires the sys_id of the remote,
* which we must retrieve by name, then feed into another function
* @param {string} name The name of the update set to retrieve
*/
function saveRemoteUpdateSet(name) {
request.get("https://" + options.instance + '.service-now.com/' +
'sys_remote_update_set.do?JSON&sysparm_action=getRecords' +
'&sysparm_query=name=' + name,
function(error, response, body) {
try {
var data = JSON.parse(body);
var records = data.records;
for (var i = 0; i < records.length; i++) {
createRemoteUpdateSet(records[i].sys_id, name);
}
} catch (e) {
console.log('Couldnt find remote update set for ' + name);
}
}).auth(options.username, options.password, false);
}
/**
* @description Once the sys_id of the remote update is known, it can be read from export_update_set.do
* @param {string} sysId The sys_id of the update set
* @param {string} name The name of the update set
*/
function createRemoteUpdateSet(sysId, name) {
request.get("http://" + options.instance + ".service-now.com/" +
"export_update_set.do?sysparm_delete_when_done=false&sysparm_sys_id=" + sysId,
function(error, response, body) {
try {
var data = (body);
var directory = __dirname + "/sys_remote_update_set";
try {
var stats = fs.lstatSync(directory);
if (stats.isDirectory()){
fs.writeFile(directory + "/" + cleanName(name) +
".xml", data,
function(err) {
if (err)
return console.log(err);
});
}
} catch (e) {
fs.mkdirSync(directory);
fs.writeFile(directory + "/" + cleanName(name) +
".xml", data,
function(err) {
if (err)
return console.log(err);
});
}
} catch (e) {
console.log("Problem retrieving update set");
}
}).auth(options.username, options.password, false);
}
/**
* @description Checks to see if a directory exists; makes it if it does not
* @param {object} record an object containing information from a GlideRecord about a script
* @param {string} scriptType the type of script in ServiceNow
* @param {string} extension the file extension to give to the file
*/
function checkDirectory(record, scriptType, extension) {
var directory = __dirname + "/" + scriptType;
try {
var stats = fs.lstatSync(directory);
if (stats.isDirectory()){
writeAFile(record, scriptType, extension);
}
} catch (e) {
fs.mkdirSync(__dirname + "/" + scriptType);
writeAFile(record, scriptType, extension);
}
}
/**
* @description Creates a javascript file using what was in the ServiceNow instance
* @param {object} record an object containing information from a GlideRecord about a script
* @param {string} scriptType the type of script in ServiceNow
* @param {string} extension the file extension to give to the file
*/
function writeAFile(record, scriptType, extension) {
var html,
heading = "<!doctype html><html lang='en'><head><title>" +
"Update Information About : " + record.name + " for Release " +
record.u_release_name + "</title><meta name='viewport' content='" +
"width=device-width, initial-scale=1'><link rel='stylesheet' type" +
"='text/css' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/" +
"css/bootstrap.min.css'/></head><body><div class='container'>",
footing = "</div><script src='https://maxcdn.bootstrapcdn.com/" +
"bootstrap/3.3.5/js/bootstrap.min.js'></script></body></html>";
if (record.u_migration_plan) {
html = heading + record.u_migration_plan + footing;
}
fs.writeFile(__dirname + "/" + scriptType + "/" +
cleanName(record.name) + extension,
(record.script || record.payload || html), function(err) {
if (err) {
return console.log(err);
}
});
}
/**
* @description Makes a valid filename by removing whitespace and special characters
* @param {string} name The name of a script in the system
* @return {string} The name without whitespace and special characters
*/
function cleanName(name) {
return name.replace(/\s\W/g, '');
}
})();