-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.js
executable file
·540 lines (512 loc) · 17.2 KB
/
utils.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
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
const exec = require('child_process').exec;
const fs = require("fs")
const path = require("path")
const crypto = require("crypto");
var jwtSecret = crypto.randomBytes(32).toString("hex");
// var jwtSecret = "_JWT_SECRET_";
const JiraSettings = require('./jiraSettings');
let host = JiraSettings.host;
/**
* Look ma, it's cp -R.
* @param {string} src The path to the thing to copy.
* @param {string} dest The path to the new copy.
*/
var copyRecursiveSync = function(src, dest) {
var exists = fs.existsSync(src);
var stats = exists && fs.statSync(src);
var isDirectory = exists && stats.isDirectory();
if (isDirectory) {
try {
fs.mkdirSync(dest);
} catch (e) {};
fs.readdirSync(src).forEach(function(childItemName) {
copyRecursiveSync(path.join(src, childItemName),
path.join(dest, childItemName));
});
} else if (exists) {
fs.copyFileSync(src, dest);
}
};
async function execCmdExecutor (cmdStr, maxBuffer = 1024 * 1024 * 10) {
let p, f;
exec(cmdStr, { maxBuffer: maxBuffer }, (error, stdout, stderr) => {
if (error) {
//console.log(`\n${Date()}: execCmdExecutor failed!: ${cmdStr}: ${error}\n`);
//throw error;
//XXX: need to send error alert in a better way
//console.log(stdout);
f("err");
return;
}
f(stdout);
});
p = new Promise((resolve, reject) => {
f = (ret) => {
resolve(ret);
};
});
return p;
}
function getRecFromJiraIssue(issue) {
let jiraUrl = "https://" + host;
let rec = {};
rec.key = issue.key;
rec.summary = issue.fields.summary;
rec.type = issue.fields.issuetype.name;
rec.jiraSummary = `${rec.summary}\n(${rec.key})`
rec.assignee = issue.fields.assignee ? issue.fields.assignee.name : "NotSet";
rec.status = issue.fields.status.name;
rec.priority = issue.fields.priority.name;
rec.reporter = issue.fields.reporter.name;
if (issue.fields.votes)
rec.votes = issue.fields.votes.votes;
rec.updated = issue.fields.updated.split('T')[0];
if (issue.fields.customfield_25570)
rec.systemFeature = issue.fields.customfield_25570.value;
else
rec.systemFeature = "NotSet";
if (issue.fields.customfield_11504)
rec.severity = issue.fields.customfield_11504.value;
else
rec.severity = "NotSet";
// This is an idiosyncracy in our jira installation
if (rec.type === 'Feature')
rec.severity = '';
if (issue.fields.customfield_25802)
rec.foundInRls = issue.fields.customfield_25802.value;
else
rec.foundInRls = "NotSet";
rec.created = issue.fields.created.split('T')[0];
if (issue.fields.customfield_25907)
rec.rrtTargetRls = issue.fields.customfield_25907.value;
else
rec.rrtTargetRls = "NotSet";
if (issue.fields.customfield_22013)
rec.targetRls = issue.fields.customfield_22013.name;
else
rec.targetRls = "NotSet";
if (issue.fields.customfield_25588)
rec.feature = issue.fields.customfield_25588.value;
else
rec.feature = "NotSet";
if (issue.fields.customfield_25791)
rec.rzFeature = issue.fields.customfield_25791.value;
else
rec.rzFeature = "NotSet";
if (issue.fields.customfield_25693)
rec.phaseBugIntroduced = issue.fields.customfield_25693.value;
else
rec.phaseBugIntroduced = "NotSet";
if (issue.fields.customfield_25518)
rec.phaseBugFound = issue.fields.customfield_25518.value;
else
rec.phaseBugFound = "NotSet";
if (issue.fields.duedate)
rec.duedate = issue.fields.duedate.split('T')[0];
else
rec.duedate = "NotSet";
if (issue.fields.customfield_25555 && issue.fields.customfield_25555.length) {
rec.targetRlsGx = "";
for (let i = 0; i < issue.fields.customfield_25555.length; i++) {
rec.targetRlsGx += issue.fields.customfield_25555[i].value + ' ';
}
}
else
rec.targetRlsGx = "NotSet";
if (issue.fields.labels && issue.fields.labels.length) {
rec.labels = "";
for (let i = 0; i < issue.fields.labels.length; i++) {
rec.labels += issue.fields.labels[i];
if (i + 1 < issue.fields.labels.length) rec.labels += ', '
}
}
if (issue.fields.versions && issue.fields.versions.length) {
rec.versions = "";
for (let i = 0; i < issue.fields.versions.length; i++) {
rec.versions += issue.fields.versions[i].name + ' ';
}
}
if (issue.fields.fixVersions && issue.fields.fixVersions.length) {
rec.fixVersions = "";
for (let i = 0; i < issue.fields.fixVersions.length; i++) {
rec.fixVersions += issue.fields.fixVersions[i].name + ',';
}
}
if (issue.fields.customfield_25800 && issue.fields.customfield_25800.key) {
rec["Assignee Manager"] = issue.fields.customfield_25800.key;
} else {
rec["Assignee Manager"] = "NotSet";
}
if (issue.fields.customfield_25609 && issue.fields.customfield_25609.length) {
rec["Dev RCA Comments"] = issue.fields.customfield_25609;
} else {
rec["Dev RCA Comments"] = "NotSet";
}
if (issue.fields.customfield_28096 && issue.fields.customfield_28096.value) {
rec["Agile Team"] = issue.fields.customfield_28096.value;
} else {
rec["Agile Team"] = "NotSet";
}
if (issue.fields.customfield_25523) {
rec["Acceptance Criteria"] = issue.fields.customfield_25523;
} else {
rec["Acceptance Criteria"] = "NotSet";
}
if (issue.fields.customfield_28097 && issue.fields.customfield_28097.length) {
rec["Agile Commit"] = "";
for (let i = 0; i < issue.fields.customfield_28097.length; i++) {
rec["Agile Commit"] += issue.fields.customfield_28097[i].name + ',';
}
rec["Agile Commit"] = rec["Agile Commit"].slice(0, -1);
} else {
rec["Agile Commit"] = "NotSet";
}
if (issue.fields.parent) {
rec.parentKey = `[${issue.fields.parent.key}](${jiraUrl + '/browse/' + issue.fields.parent.key})`
try {
if (issue.fields.parent.fields.summary) {
rec.parentSummary = `${issue.fields.parent.fields.summary}`
} else {
rec.parentSummary = ""
}
} catch (e) { rec.parentSummary = "" }
} else {
rec.parentKey = "";
rec.parentSummary = ""
}
if (rec.parentKey != "") {
if (rec.parentSummary != "") {
rec.parent = `${rec.parentKey} (${rec.parentSummary})`
} else {
rec.parent = `${rec.parentKey}`
}
} else {
rec.parent = ""
}
if (issue.fields.customfield_12790) {
rec.epic = `[${issue.fields.customfield_12790}](${jiraUrl + '/browse/' + issue.fields.customfield_12790})`
} else {
rec.epic = ""
}
if (issue.fields.description)
rec.description = issue.fields.description;
else
rec.description = "";
if (issue.fields.customfield_11890) {
rec['Story Points'] = issue.fields.customfield_11890
} else {
rec['Story Points'] = 0
}
if (issue.fields.customfield_11990 && issue.fields.customfield_11990.length > 0) {
let sprintDetails = issue.fields.customfield_11990[0]
let sprintNameMatchArr = sprintDetails.match(/name=([^,]*)/)
if (sprintNameMatchArr && sprintNameMatchArr.length >= 2) {
rec["Sprint Name"] = sprintNameMatchArr[1]
} else {
rec["Sprint Name"] = ""
}
} else {
rec["Sprint Name"] = ""
}
if (issue.fields.subtasks && issue.fields.subtasks.length) {
rec.subtasks = "[";
for (let i = 0; i < issue.fields.subtasks.length; i++) {
rec.subtasks += issue.fields.subtasks[i].key;
if (i + 1 < issue.fields.subtasks.length)
rec.subtasks += ", ";
}
rec.subtasks += "]";
}
if (issue.fields.subtasks && issue.fields.subtasks.length) {
//rec.subtasksDetails = getSubTasksDetailsInTable(issue);
rec.subtasksDetails = getSubTasksDetailsInList(issue);
}
if (issue.fields.issuelinks && issue.fields.issuelinks.length) {
let name, details;
// Depends links
let dependsLinks = "";
[name, details] = getIssueLinksInList(issue.fields.issuelinks, "Depends", "inward");
if (name) {
dependsLinks += details;
}
[name, details] = getIssueLinksInList(issue.fields.issuelinks, "Depends", "outward");
if (name) {
dependsLinks += details;
}
if (dependsLinks !== "")
rec.dependsLinks = dependsLinks;
// Implement links
let implementLinks = "";
[name, details] = getIssueLinksInList(issue.fields.issuelinks, "Implement", "inward");
if (name) {
implementLinks += details;
}
[name, details] = getIssueLinksInList(issue.fields.issuelinks, "Implement", "outward");
if (name) {
implementLinks += details;
}
if (implementLinks !== "")
rec.implementLinks = implementLinks;
// Package links
let packageLinks = "";
[name, details] = getIssueLinksInList(issue.fields.issuelinks, "Package", "inward");
if (name) {
packageLinks += details;
}
[name, details] = getIssueLinksInList(issue.fields.issuelinks, "Package", "outward");
if (name) {
packageLinks += details;
}
if (packageLinks !== "")
rec.packageLinks = packageLinks;
// Relates links
let relatesLinks = "";
[name, details] = getIssueLinksInList(issue.fields.issuelinks, "Relates", "inward");
if (name) {
relatesLinks += details;
}
[name, details] = getIssueLinksInList(issue.fields.issuelinks, "Relates", "outward");
if (name) {
relatesLinks += details;
}
if (relatesLinks !== "")
rec.relatesLinks = relatesLinks;
// Test links
let testLinks = "";
[name, details] = getIssueLinksInList(issue.fields.issuelinks, "Test", "inward");
if (name) {
testLinks += details;
}
[name, details] = getIssueLinksInList(issue.fields.issuelinks, "Test", "outward");
if (name) {
testLinks += details;
}
if (testLinks !== "")
rec.testLinks = testLinks;
// Covers links
let coversLinks = "";
[name, details] = getIssueLinksInList(issue.fields.issuelinks, "Covers", "inward");
if (name) {
coversLinks += details;
}
[name, details] = getIssueLinksInList(issue.fields.issuelinks, "Covers", "outward");
if (name) {
coversLinks += details;
}
if (coversLinks !== "")
rec.coversLinks = coversLinks;
// Defect links
let defectLinks = "";
[name, details] = getIssueLinksInList(issue.fields.issuelinks, "Defect", "inward");
if (name) {
defectLinks += details;
}
[name, details] = getIssueLinksInList(issue.fields.issuelinks, "Defect", "outward");
if (name) {
defectLinks += details;
}
if (defectLinks !== "")
rec.defectLinks = defectLinks;
// Automates links
let automatesLinks = "";
[name, details] = getIssueLinksInList(issue.fields.issuelinks, "Automates", "inward");
if (name) {
automatesLinks += details;
}
[name, details] = getIssueLinksInList(issue.fields.issuelinks, "Automates", "outward");
if (name) {
automatesLinks += details;
}
if (automatesLinks !== "")
rec.automatesLinks = automatesLinks;
}
return rec
}
function getSubTasksDetailsInList(issue) {
let jiraUrl = "https://" + host;
let subtasksDetails = "\n";
if (issue.fields.subtasks && issue.fields.subtasks.length) {
for (let i = 0; i < issue.fields.subtasks.length; i++) {
subtasksDetails += "1. [";
//subtasksDetails += issue.fields.subtasks[i].key + ", ";
subtasksDetails += `[${issue.fields.subtasks[i].key}](${jiraUrl + '/browse/' + issue.fields.subtasks[i].key}), `;
if (issue.fields.subtasks[i].fields.issuetype)
subtasksDetails += issue.fields.subtasks[i].fields.issuetype.name + ", ";
else
subtasksDetails += ", ";
//subtasksDetails += `<span style="color: ${issue.fields.subtasks[i].fields.status.statusCategory.colorName}; background-color: lightgrey">${issue.fields.subtasks[i].fields.status.name}</span>` + ", ";
if (issue.fields.subtasks[i].fields.status)
subtasksDetails += issue.fields.subtasks[i].fields.status.name + ", ";
else
subtasksDetails += ", ";
if (issue.fields.subtasks[i].fields.priority)
subtasksDetails += issue.fields.subtasks[i].fields.priority.name + "] ";
else
subtasksDetails += ", ";
subtasksDetails += issue.fields.subtasks[i].fields.summary + "\n";
}
}
return subtasksDetails + "\n";
}
function getIssueLinksInList(issueLinks, type, dir) {
let jiraUrl = "https://" + host;
let details = "\n"; let dirName = "";
for (let i = 0; i < issueLinks.length; i++) {
if (issueLinks[i].type.name !== type)
continue;
if (dir == "inward" && !issueLinks[i].inwardIssue)
continue;
if (dir == "outward" && !issueLinks[i].outwardIssue)
continue;
let issue = null;
if (dir == "inward") {
dirName = issueLinks[i].type.inward;
issue = issueLinks[i].inwardIssue;
} else {
dirName = issueLinks[i].type.outward;
issue = issueLinks[i].outwardIssue;
}
details += "1. [";
details += `[${issue.key}](${jiraUrl + '/browse/' + issue.key}), `;
if (issue.fields.issuetype)
details += issue.fields.issuetype.name + ", ";
else
details += ", ";
if (issue.fields.status)
details += issue.fields.status.name + ", ";
else
details += ", ";
if (issue.fields.priority)
details += issue.fields.priority.name + "] ";
else
details += ", ";
details += issue.fields.summary + "\n";
}
if (details == "\n") {
details = "";
} else {
details = `<u>${dirName}</u>` + ":\n" + details + '\n\n';
}
return [dirName, details];
}
/**
* Given an object with key-value pair. This function searches for all the values in the object recursively,
* if the value is of string type, it trims the value.
* @param {object} objData
*/
function sanitizeData(objData) {
try {
if (objData) {
for (let key of Object.keys(objData)) {
if (typeof (objData[key]) == "string") {
objData[key] = objData[key].trim()
} else if (typeof (objData[key] == "object")) {
sanitizeData(objData[key])
}
}
}
} catch (e) {
console.log("Failed to sanitize data", e);
}
}
function getRevContentMap(jiraConfig) {
let jiraFieldMapping = jiraConfig.jiraFieldMapping
jiraFieldMapping = JSON.parse(JSON.stringify(jiraFieldMapping));
delete jiraFieldMapping.key;
let revContentMap = {};
for (let key in jiraFieldMapping) {
let dsField = jiraFieldMapping[key];
if (!revContentMap[dsField])
revContentMap[dsField] = 1;
else
revContentMap[dsField] = revContentMap[dsField] + 1;
}
return revContentMap
}
function getKeyByValue(object, value) {
return Object.keys(object).find(key => object[key] === value);
}
/**Given a mongodb record, and the reverse contentMapping and jiraFieldMapping.
* It gives back the jira rec object.
*/
function parseRecord(dbRecord, revContentMap, jiraFieldMapping) {
let dbKeys = Object.keys(dbRecord)
let rec = {}
let parseSuccess = true;
let jiraUrl = "https://" + host;
for (let dbKey of dbKeys) {
let recKey = getKeyByValue(jiraFieldMapping, dbKey)
if (!recKey) continue
if (!revContentMap[dbKey]) {
let recVal = dbRecord[dbKey]
if (typeof recVal == 'string') {
let regex = new RegExp(`${jiraUrl}/browse/(.*)\\)`)
let jiraIssueIdMatchArr = recVal.match(regex)
if (jiraIssueIdMatchArr && jiraIssueIdMatchArr.length >= 2) {
recVal = jiraIssueIdMatchArr[1].trim()
}
}
rec[recKey] = recVal
continue
}
if (revContentMap[dbKey] == 1) {
let recVal = dbRecord[dbKey]
if (typeof recVal == 'string') {
let regex = new RegExp(`${jiraUrl}/browse/(.*)\\)`)
let jiraIssueIdMatchArr = recVal.match(regex)
if (jiraIssueIdMatchArr && jiraIssueIdMatchArr.length >= 2) {
recVal = jiraIssueIdMatchArr[1].trim()
}
}
if (recKey == 'jiraSummary') {
let arr = recVal.split('\n');
if (arr.length >= 2) {
const output = recVal.split('\n')[0];
rec['summary'] = output
} else {
const regex = /\s*\([^)]*\)/;
const output = recVal.replace(regex, '');
rec['summary'] = output
}
}
rec[recKey] = recVal
} else {
let dbVal = dbRecord[dbKey]
let dbValArr = dbVal.split("<br/>")
for (let eachEntry of dbValArr) {
let eachEntryKeyMatchArr = eachEntry.match(/\*\*(.*)\*\*:(.*)/s)
if (eachEntryKeyMatchArr && eachEntryKeyMatchArr.length >= 3) {
let recKey = eachEntryKeyMatchArr[1].trim()
let recVal = eachEntryKeyMatchArr[2].trim()
let regex = new RegExp(`${jiraUrl}/browse/(.*)\\)`)
let jiraIssueIdMatchArr = recVal.match(regex)
if (recKey == 'key' && jiraIssueIdMatchArr && jiraIssueIdMatchArr.length >= 2) {
recVal = jiraIssueIdMatchArr[1]
}
if (recKey == 'jiraSummary') {
let arr = recVal.split('\n');
if (arr.length >= 2) {
const output = recVal.split('\n')[0];
rec['summary'] = output
} else {
const regex = /\s*\([^)]*\)/;
const output = recVal.replace(regex, '');
rec['summary'] = output
}
}
rec[recKey] = recVal
}
}
}
}
return { rec, parseSuccess }
}
module.exports = {
execCmdExecutor,
copyRecursiveSync,
getRecFromJiraIssue,
sanitizeData,
getRevContentMap,
parseRecord,
jwtSecret
};