-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
310 lines (251 loc) · 7.37 KB
/
app.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
const axios = require('axios'),
inquirer = require('inquirer'),
config = require('./config.json'),
baseUrl = config.rallyBaseUrl,
headers = {
headers: {
zsessionid: config.rallyApiKey
}
};
start();
function start() {
let copyFromFolder = {},
copyToFolder = {},
deleteFolder = {};
inquirer.prompt([{
type: 'list',
name: 'operation',
message: "What would you like to do?",
choices: ['Copy', 'Delete']
}]).then(answer => {
if(answer.operation == 'Copy') {
inquirer.prompt([{
type: 'input',
name: 'fromFolderId',
message: 'Please type in the ID of the folder you wish to copy from',
validate: function(value) {
//return value == "" ? "Please enter an ID" : true;
return getFolderName(value).then(res => {
if(res.hasOwnProperty('error')) {
return res.error;
} else {
copyFromFolder.id = value;
copyFromFolder.name = res;
return true;
}
});
}
},
{
type: 'input',
name: 'copyToFolderId',
message: "Please type in the ID of the folder you wish to copy to",
validate: function(value) {
//return value == "" ? "Please enter an ID" : true;
return getFolderName(value).then(res => {
if(res.hasOwnProperty('error')) {
return res.error;
} else {
copyToFolder.id = value;
copyToFolder.name = res;
return true;
}
});
}
}]).then(answer => {
copyAllTestCases(copyFromFolder, copyToFolder);
});
} else {
inquirer.prompt([{
type: 'input',
name: 'deleteFolderId',
message: "Please type in the ID of the folder you wish to delete the contents of",
validate: function(value) {
//return value == "" ? "Please enter an ID" : true;
return getFolderName(value).then(res => {
if(res.hasOwnProperty('error')) {
return res.error;
} else {
deleteFolder.id = value;
deleteFolder.name = res;
return true;
}
});
}
}]).then(answer => {
deleteAllTestCases(deleteFolder);
});
}
});
}
// Copy all Test Cases in a folder to another folder
function copyAllTestCases(copyFromFolder, copyToFolder) {
let promises = [];
getChildTestCases(copyFromFolder.id).then(res => {
let question = {
type: 'confirm',
name: 'toBeCopied',
message: `Are you sure you want to copy all ${ res.length } test cases from ${ copyFromFolder.name } to ${ copyToFolder.name }`,
default: false
}
return inquirer.prompt([question]).then(answer => {
if(answer.toBeCopied) {
console.log(`\nCopying all ${ res.length } test cases from ${ copyFromFolder.name } to ${ copyToFolder.name }...\n`);
for(let value of res) {
promises.push(copySingleTestCase(value._ref, value.Name, copyToFolder.id))
}
return Promise.all(promises);
} else {
return Promise.reject("Cancelled")
}
});
}).then((res) => {
let count = 0;
for(let value of res) {
count++;
if(value.CreateResult.Errors.length == 0){
console.log(`${ count }. ${ value.CreateResult.Object._refObjectName } testcase created successfully in folder ${ value.CreateResult.Object.TestFolder._refObjectName }`);
} else {
console.log(`${ count }. Error creating testcase:`);
console.log(value.CreateResult.Errors);
}
}
}).catch((error) => {
console.log(new Error(error));
});
}
// Delete all Test Cases in a folder
function deleteAllTestCases(deleteFolder) {
let promises = [];
getChildTestCases(deleteFolder.id).then((res) => {
let question = {
type: 'confirm',
name: 'toBeDeleted',
message: `Are you sure you want to delete all ${ res.length } test cases from ${ deleteFolder.name }`,
default: false
}
return inquirer.prompt([question]).then(answer => {
if(answer.toBeDeleted) {
console.log(`\nDeleting all ${ res.length } test cases from ${ deleteFolder.name }...\n`);
for(let value of res) {
promises.push(deleteSingleTestCase(value._ref));
}
return Promise.all(promises);
} else {
return Promise.reject("Cancelled")
}
});
}).then((res) => {
let count = 0;
for(let value of res) {
count++;
if(value.data.OperationResult.Errors.length == 0){
console.log(`${ count }. ${ value.testCaseName } testcase deleted from ${ deleteFolder.name }`);
} else {
console.log(`${ count }. Error deleting testcase:`);
console.log(value.data.OperationResult.Errors);
}
}
}).catch((error) => {
console.log(error);
});
}
// Input Folder ID of folder with child test cases
// Returns an array of child test cases
function getChildTestCases(testCaseFolderId) {
return new Promise((resolve, reject) => {
let url = baseUrl + "/testfolder/" + testCaseFolderId + '/Descendants' + '?pagesize=200';
let results = []
get(url);
function get(url) {
axios.get(url, headers).then((response) => {
results.push(response.data.QueryResult.Results);
if(response.data.QueryResult.Results.length != response.data.QueryResult.TotalResultCount) {
reject(new Error("Needs to run again, page size is not big enough or need to implement index changes"));
} else {
if(results[0].length >= 1) {
resolve(results[0]);
} else {
reject('Folder is empty');
}
}
}).catch((error) => {
reject(new Error(error));
});
}
});
}
// Input Folder ID
// Returns an string with the folder name
function getFolderName(testCaseFolderId) {
return new Promise((resolve, reject) => {
let url;
//Handle if it is a url or a id
if(testCaseFolderId.indexOf(baseUrl) >= 0) {
// has full url do nothing
} else {
url = baseUrl + "/testfolder/" + testCaseFolderId;
}
axios.get(url, headers).then(res => {
if('OperationResult' in res.data) {
if(res.data.OperationResult.Errors[0] == 'Cannot find object to read') {
resolve({error: 'Folder does not exist'})
}
}
resolve(res.data.TestFolder._refObjectName);
}).catch((error) => {
console.log(error);
reject(new Error(error));
});
});
}
// Input Folder ID
// Returns an string with the testcase name
function getTestCaseName(testCaseId) {
return new Promise((resolve, reject) => {
//Handle if it is a url or a id
if(testCaseId.indexOf(baseUrl) >= 0) {
// has full url do nothing
} else {
testCaseId = baseUrl + "/testcase/" + testCaseId;
}
axios.get(testCaseId, headers).then((response) => {
resolve(response.data.TestCase.Name);
}).catch((error) => {
reject(new Error(error));
});
});
}
function copySingleTestCase(url, testCaseName, copyToFolder) {
return new Promise((resolve, reject) => {
url = url + '/copy';
copyToFolder = baseUrl + '/testfolder/' + copyToFolder;
let bodyData = {
"TestCase": {
"Name": testCaseName,
"TestFolder": {
"_ref": copyToFolder
}
}
}
axios.post(url, bodyData, headers).then((response) => {
resolve(response.data);
}).catch((error) => {
reject(new Error(error));
});
});
}
//Input - testCase ID
function deleteSingleTestCase(testCaseUrl) {
return new Promise((resolve, reject) => {
let testCaseName;
getTestCaseName(testCaseUrl).then((res) => {
testCaseName = res;
return axios.delete(testCaseUrl, headers)
}).then((res) => {
resolve({testCaseName, data: res.data});
}).catch((error) => {
reject(new Error(error));
});
});
}