sn.js
19.1 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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
var servicenow = require('servicenow');
var config=require('config');
var JiraApi = require('jira').JiraApi;
module.exports={
info:{
summary:"tools per servicenow",
state:"ok".red
},
execute:function(process,tool){
var configSN = {
instance: config.get('servicenowConfig.serviceNowUrl'),
username: config.get('servicenowConfig.username'),
password: config.get('servicenowConfig.password'),
sessionID: config.get('servicenowConfig.sessionID'),
};
var jira = new JiraApi(config.get('jira4servicenow.protocolJira'), config.get('jira4servicenow.urlJira'),config.get('jira4servicenow.portJira'), config.get('jira4servicenow.userJira'), config.get('jira4servicenow.passwordJira'), config.get('jira4servicenow.jiraApiVersion'),true);
var client = new servicenow.Client(configSN);
var sn=this;
var env = "operations";
var op = process.argv[3];
var incId = process.argv[4];
var assignee = process.argv[5];
var prioritySn=["1"]
if(env!=null && op!=null){
//console.log("finding server "+name.red+" on "+env.red);
var operations;
if(env==="operations") operations = sn.list.operations;
for(i in operations){
if(operations[i].name===op){
switch(op){
case "assigns_us":
var o = {
"state": "2",
"assigned_to": config.get('servicenowConfig.assignee_us')
}
console.log("assigns_us "+incId+" to "+config.get('servicenowConfig.assignee_us'));
client.update("incident","number="+incId+"",o,function(error,result) {
if(!error) {
if(!error) {
console.log(result);
// call succeded
}else{
console.log("ERRORE: Qualcosa non è andato a buon fine !!");
}
}else{
console.log("errore "+error+" - "+result);
}
});
break;
case "assign_to":
var o = {
"state": "2",
"assigned_to":assignee
}
client.update("incident","number="+incId+"",o,function(error,result) {
if(!error) {
if(!error) {
console.log(result);
}else{
console.log("ERRORE: Qualcosa non è andato a buon fine !!");
}
}
});
break;
case "createTask":
client.getKeys("incident","number="+incId+"",function(error,result) {
var incId;
if(!error) {
console.log(result.records[0]);
incId=result.records[0];
var inp = {
"assignment_group":config.get('servicenowConfig.assignment_group'),
"assigned_to":assignee,
"parent":incId
}
client.insert("u_incident_task",inp,function(error,result) {
if(!error) {
console.log("risultato inserimento: "+JSON.stringify(result));
}else{
console.log("errore inserimento "+error);
}
});
}else{
console.log("record non trovato");
incId="";
}
});
break;
case "createIncTask":
createIncTask(0,incId);
/*
client.getKeys("incident","number="+incId,function(error,result) {
if(!error) {
var inp = {
"assigned_to":assignee,
"parent":result.records[0]
}
console.log("in createIncTask inp: "+JSON.stringify(inp));
client.insert("u_incident_task",inp,function(error,result) {
if(!error) {
console.log("risultato inserimento: "+JSON.stringify(result));
}else{
console.log("errore inserimento "+error);
}
});
}else{
console.log("incident non trovato");
}
});
*/
/*
var o = {
"sys_id":"9d8b2754dbcd22009855d5dcaf961987"
}
client.deleteRecord("incident",o,function(error,result) {
if(!error) {
console.log("risultato delete: "+result);
}else{
console.log("errore delete "+error);
}
});
*/
//console.log("incid ritornato :"+incIdd);
//test();
/*
function waitFor(testFx, onReady, timeOutMillis) {
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, //< Default Max Timout is 3s
start = new Date().getTime(),
condition = false,
interval = setInterval(function() {
if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
// If not time-out yet and condition not yet fulfilled
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
} else {
if(!condition) {
// If condition still not fulfilled (timeout but condition is 'false')
console.log("'waitFor()' timeout");
phantom.exit(1);
} else {
// Condition fulfilled (timeout and/or condition is 'true')
console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
clearInterval(interval); //< Stop this interval
}
}
}, 250); //< repeat check every 250ms
};
var page = require('webpage').create();
page.open('https://bancamediolanum.service-now.com/navpage.do', function() {
waitFor(function() {
// Check in the page if a specific element is now visible
return page.evaluate(function() {
return $("#ctl00_ContentPlaceHolder1_SubmitButton").is(":visible");
});
}, function() {
var foo = page.evaluate(function() {
return $("#ctl00_ContentPlaceHolder1_SubmitButton").text;
});
// ...
phantom.exit();
});
});
*/
break;
case "getIncidentID":
client.getKeys("incident","number="+incId+"",function(error,result) {
if(!error) {
console.log(result.records[0]);
}else{
console.log("record non trovato");
}
});
break;
case "creaTaskJira":
creaTaskJira("titolo test automatico task da nodejs","descrizione automatica task jira")
break;
case "creaIncidentJira":
var priorityJira=getPriorityBySNPriority("4 - Bassa");
//console.log("priorityJira :"+priorityJira);
creaIncidentJira("titolo test automatico incident da nodejs 8_9 titolo","descrizione automatica incident jira 8_9 descrizione","12725",priorityJira);
break;
case "getAllIncidentOpenNexus":
getAllIncidentOpenNexus();
break;
case "getAllIncidentOpenNexusJson":
getAllIncidentOpenNexusJson();
case "monitoraggioSn":
monitoraggioSn();
/*
var minutes = 1, the_interval = minutes * 60 * 1000;
setInterval(function() {
monitoraggioSn();
// do your stuff here
}, the_interval);
*/
break;
case "findIssueJira":
findIssueJira(incId);
break;
case "test":
console.log("input "+process.argv[4]);
var obb=getComponentIdBySNChannel(process.argv[4]);
console.log(obb.component_name);
break;
default:
console.log("default case");
}
}
}
}
if(op==null){
console.log("\n--------------------"+"OPERAZIONI PER SERVICENOW".red+"----------------------------");
tool.task(sn.list.operations);
}
/**
* [monitoraggioSn description]
* @return {[null]} []
*/
function monitoraggioSn(){
client.getRecords("task_list","assignment_group="+config.get('servicenowConfig.nexusgroupId')+"^state=2^ORstate=1^assigned_to=",function(errorsn,result) {
if(!errorsn) {
console.log("record trovati "+result.records.length);
if(result.records.length > 0){
console.log("\007");
for(i in result.records){
titolo=result.records[i].short_description;
descrizione=result.records[i].description;
console.log("*****************************************************");
console.log("TITOLO INCIDENT : "+titolo);
console.log("DESCRIZIONE INCIDENT : "+descrizione);
console.log("*****************************************************");
console.log("");
// modificare l'assignee tramite la funzione assigns_us
// creare un inctask legandolo all'incident in questione tramite la funzione createIncTask
// creare un task su jira (tramite creaTaskJira) assegnandolo allo stesso user a cui viene assegnato l'inctask e con i campi settati in maniera opportuna
//verifico se si tratta di un task o di un incident
//if(result.records[i].sys_class_name==="Incidente"){
//var priorityJira=getPriorityBySNPriority(result.records[i].priority);
//var componentobj=getComponentIdBySNChannel(result.records[i].cmdb_ci);
//creaIncidentJira(titolo,descrizione,componentobj.componentId,priorityJira);
//}else if(result.records[i].sys_class_name==="Attività catalogo"){
//creaTaskJira(titolo,descrizione);
//}
}
}
return result.records[0];
}else{
console.log("monitoraggioSn errore durante la lettura degli incident : "+JSON.stringify(errorsn));
}
});
}
/**
* [getAllIncidentOpenNexusJson rileva l'insieme degli incident aperti e non ancora gestiti per nexus]
* @return {[array]} [json contenente un array di incident]
*/
function getAllIncidentOpenNexusJson(){
client.getRecords("task_list","assignment_group=6921ecd16fe8a10023fbda385e3ee48d^state=2^ORstate=1^assigned_to=",function(error,result) {
if(!error) {
console.log(JSON.stringify(result));
return result.records[0];
}else{
console.log("errore durante la lettura degli incident : "+JSON.stringify(error));
}
});
}
/**
* [getAllIncidentOpenNexus stampa a video titolo e descrizione degli incident aperti e non ancora gestiti per nexus]
* @return {array} [array di incident]
*/
function getAllIncidentOpenNexus(){
client.getRecords("task_list","assignment_group="+config.get('servicenowConfig.nexusgroupId')+"^state=2^ORstate=1^assigned_to=",function(error,result) {
if(!error) {
console.log("record trovati "+result.records.length);
if(result.records.length > 0){
for(i in result.records){
titolo=result.records[i].short_description;
descrizione=result.records[i].description;
console.log("*****************************************************");
console.log("TITOLO INCIDENT : "+titolo);
console.log("DESCRIZIONE INCIDENT : "+descrizione);
console.log("*****************************************************");
console.log("");
}
}
}else{
console.log("errore durante la lettura degli incident : "+JSON.stringify(error));
}
});
}
/**
* [createIncTask crea un Incident Task legandolo all'incident <INCXXX> passato in input e lo assegna a <vedi configurazione (servicenowConfig.default_assignee_incTask)>]
* @param {[type]} tent [inizializzazione del tentativo di connessione (impostare sempre a zero)]
* @param {[type]} _incId [incident Id (es:INC0159870)]
* @return {[null]} []
*/
function createIncTask(tent,_incId){
if(tent<config.get('servicenowConfig.maxAttemptNumber')){
var _tent=tent+1;
console.log("tentativo : "+tent);
client.getKeys("incident","number="+_incId+"",function(error,result) {
if(!error) {
console.log("trovato "+result.records[0]);
var input = {
"assigned_to":config.get('servicenowConfig.default_assignee_incTask'),
"parent":result.records[0]
}
client.insert("u_incident_task",input,function(error,result) {
if(!error) {
console.log("risultato inserimento: "+JSON.stringify(result));
}else{
console.log("errore inserimento "+error);
}
});
}else{
console.log("record non trovato");
createIncTask(_tent,_incId);
}
});
}else{
console.log("numero tentativi superato");
}
}
/**
* [findIssueJira effettua la ricerca di un issue jira ]
* @param {[type]} issueId [issudeId (es:MOB-6822)]
* @return {[boolean]} [ritorna true se la trova e stampa a video]
*/
function findIssueJira(issueId){
jira.findIssue(issueId, function(error, issue) {
if(!error) {
console.log('ISSUE: ' + JSON.stringify(issue));
return true;
}else{
console.log("errore ISSUE non trovata "+error);
return false;
}
});
}
/**
* [creaTaskJira crea un task di tipo task su jira]
* @param {[type]} titolo [titolo della issue]
* @param {[type]} descrizione [descrizione della issue]
* @return {[type]} [description]
*/
function creaTaskJira(titolo,descrizione){
var dataissue={"fields": {
"assignee": {"name": config.get('jira4servicenow.assignee_task')},
"project": {"id": config.get('jira4servicenow.taskAccountId')},
"summary": titolo,
"description": descrizione,
"issuetype": {"id": "3"},
"customfield_10602":4
}
}
jira.addNewIssue(dataissue,function(error, response) {
console.log('error'+JSON.stringify(error));
});
}
/**
* [creaIncidentJira crea un task di tipo incident su jira]
* @param {[type]} titolo [titolo della issue]
* @param {[type]} descrizione [descrizione della issue]
* @return {[type]} [description]
*/
function creaIncidentJira(titolo,descrizione,componentId,jiraPriority){
var componentDetail=config.get('jira4servicenow.mapComponent')[componentId];
var dataissue={"fields": {
"assignee": {"name": config.get('jira4servicenow.default_assignee_incident')},
"project": {"id": config.get('jira4servicenow.taskAccountId')},
"summary": titolo,
"description": descrizione,
"issuetype": {"id": "10300"},
"components": [{"id":componentId}],
"customfield_10602":4,
"priority": {"name": jiraPriority}
}
}
jira.addNewIssue(dataissue,function(error, response) {
console.log('error'+JSON.stringify(error));
});
}
function getComponentIdBySNChannel(channelin){
var channel=channelin.toLowerCase();
var mapComponent=config.get('jira4servicenow.mapComponent');
var gstComponent= {"assegneeJira":config.get('jira4servicenow.default_assignee_incident'),"assegneeSn":config.get('servicenowConfig.default_assignee_incTask'),"codComponentJira":config.get('jira4servicenow.default_component_jira')};
var codComponentJira=0;
if(channel.match(/mobile banking/g)){
//############# MOBILE BANKING #########
if (channel.match(/mobile banking - android/g))
{
//title="AppFull.Android - Applicazione Banca Full x Android" value="12725"
codComponentJira = "12725";
}else if (channel.match(/mobile banking - ios/g))
{
//title="AppFull.iOS - Applicazione Banca Full x iOS" value="12726"
codComponentJira = "12726";
}else if (channel.match(/android/g))
{
//title="Android " value="14503"
codComponentJira = "14503";
}else{
//mobile
codComponentJira = "13210";
}
}else if(channel.match(/banca su tablet/g)){
//############# BANCA SU TABLET ########
if (channel.match(/ios/g))
{
//AppBST.iOS - Applicazione Banca su Tablet x iOS
codComponentJira = "12728";
}else if (channel.match(/android/g))
{
//title="AppBST.Android " value="17802"
codComponentJira = "17802";
}
}else if (channel.match(/wallet/g))
{
//############# WALLET ########
if (channel.match(/android/g))
{
//title="AppWallet.Android - Applicazione Wallet x Android" value="12729"
codComponentJira = "12729";
}else if (channel.match(/ios/g))
{
//title="AppWallet.iOS " value="15300"
codComponentJira = "15300";
}else{
//mobile
codComponentJira = "13210";
}
}else if (channel.match(/windows/g) || channel.match(/wp/g) || channel.match(/wphone/g))
{
//title="AppFull.WP - Applicazione Banca Full x Window Mobile" value="12727"
codComponentJira = "12727"
}else if (channel.match(/hce/g))
{
//title="HCE " value="15301"
codComponentJira = "15301"
}else if (channel.match(/smart tv/g))
{
//title="SmartTV " value="13317"
codComponentJira = "13317"
}else if (channel.match(/p2p/g))
{
//title="P2P " value="13005"
codComponentJira = "13005"
}else if (channel.match(/family/g))
{
//title="appFB " value="18100"
codComponentJira = "18100"
}else if (channel.match(/project/g) || channel.match(/customer/g) || channel.match(/portale/g) || channel.match(/rete/g) || channel.match(/vendita/g) || channel.match(/bmedNet/g))
{
//title="NAC " value="13700"
codComponentJira = "13700"
}
return mapComponent[codComponentJira];
}
/**
* [getPriorityBysSNPriority ritorna la priorità formato servicenow]
* @param {[string]} prioritySn [priorita formati DISPLAY ServiceNow]
* @return {[string]} [priorita per jira]
*/
function getPriorityBySNPriority(prioritySn){
//console.log("in getPriorityBySN "+prioritySn);
var priorities = config.get('jira4servicenow.mapPriority');
var priorityJira = priorities[prioritySn];
//console.log("priorityJira "+priorityJira);
return priorityJira;
}
},
list:{operations:[{
name:"assigns_us",
summary:"presa in carico dell'inc <par1> (assegnato a (vedi configurazione <servicenowConfig.assignee_us>))",
state:"1"
},
{
name:"assign_to",
summary:"assegnazione dell'inc <par1> allo user <par2>",
state:"1"
},
{
name:"createIncTask",
summary:"crea un Incident Task legandolo all'incident <par1> passato in input e lo assegna a : vedi configurazione <servicenowConfig.default_assignee_incTask>",
state:"1"
},
{
name:"getIncidentID",
summary:"ritorna l'ID di sistema dell'incident <par1> passato in input",
state:"1"
},
{
name:"creaTaskJira",
summary:"effettua la creazione di un incident per jira",
state:"1"
},
{
name:"monitoraggioSn",
summary:"lancia un task di monitoraggio ServiceNow per la gestione degli incident",
state:"1"
},
{
name:"getAllIncidentOpenNexus",
summary:"rileva l'insieme degli incident aperti e non ancora gestiti per nexus",
state:"1"
},
{
name:"getAllIncidentOpenNexusJson",
summary:"rileva l'insieme degli incident aperti e non ancora gestiti per nexus e li stampa come json",
state:"1"
},
{
name:"findIssueJira",
summary:"cerca e stampa il JSON relativo all'issue passata in input su jira",
state:"1"
},
{
name:"creaIncidentJira",
summary:"ccrea un incident su jira avente <par1> come titolo e <par2> come descrizione e lo assegna ",
state:"1"
},
{
name:"test",
summary:"test case",
state:"1"
}
]
}
}