forked from AlchimiaMx/jira-connectors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.gs
643 lines (568 loc) · 19.9 KB
/
main.gs
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/**
* @fileoverview Connector for Jira cloud plataform. Analyze the issues of a JQL
*
*/
/** define namespace */
var connector = connector || {};
/** @const */
connector.defaultPackage = 'googleapis';
/** @const dictionary for fields */
var fieldsDictionary = {
'keys' : 'key',
'summary': 'summary',
'issuetype': 'issuetype',
'creator': 'creator',
'assignee': 'assignee',
'reporter': 'reporter',
'priority': 'priority',
'status': 'status',
'statusName': 'status',
'created': 'created',
'duedate': 'duedate',
'resolutiondate': 'resolutiondate',
'component': 'components',
'label': 'labels',
'epic': '',
'project': 'project',
'satisfaction': 'customfield_10204',
'timespent': 'aggregatetimespent',
'issues': '',
'timeFirstResponse': 'customfield_10208',
'breachedFirstResponse': 'customfield_10208',
'timeResolution': 'customfield_10207',
'breachedtimeResolution': 'customfield_10207',
'changeSatartDate': 'customfield_10214',
'changeCompletionDate': 'customfield_10215',
'createdDayOfWeek': 'created',
'createdHour': 'created',
'resolutiondateDayOfWeek': 'resolutiondate',
'resolutiondateHour': 'resolutiondate',
'parent_key': 'parent',
'parent_summary': 'parent',
'parent_status': 'parent',
'parent_status_name': 'parent',
'parent_priority' : 'parent',
'parent_issuetype': 'parent',
'createdWorklogDayOfWeek': 'worklog',
'createdWorklogHour': 'worklog',
'createdWorklog': 'worklog',
'startedWorklogDayOfWeek': 'worklog',
'startedWorklogHour': 'worklog',
'startedWorklog': 'worklog',
'timespentUser': 'worklog',
'authorWorklog': 'worklog',
'requestType':'customfield_10202'
}
/**
* Returns the authentication method required by the connector to authorize the
* third-party service.
*
* @returns {Object} `AuthType` used by the connector.
*/
function getAuthType() {
return { type: 'NONE' };
}
/**
* Returns the user configurable options for the connector.
*
* @param {Object} request Config request parameters.
* @returns {Object} Connector configuration to be displayed to the user.
*/
function getConfig() {
var cc = DataStudioApp.createCommunityConnector();
var config = cc.getConfig();
config.newInfo()
.setId('Auth')
.setText('Enter your credentials for the REST API of Jira Cloud Plataform');
config.newTextInput()
.setId('User')
.setName('User')
.setPlaceholder('e.g. email@domain.com')
.setHelpText('The user must have permissions to see the project');
config.newTextInput()
.setId('Token')
.setName('API Token')
.setHelpText('Go to API tokens in manage your account in Jira');
config.newTextInput()
.setId('Subdomain')
.setName('Subdomain Jira Cloud')
.setHelpText('https:// + subdomain + .atlassian.net');
config.newInfo()
.setId('Filter')
.setText('Master filter to define projects');
config.newTextInput()
.setId('JQL')
.setName('Jira Query Language (JQL)')
.setPlaceholder('project = NAME');
config.newCheckbox()
.setId('worklogs')
.setName('Based on worklogs')
config.newInfo()
.setId('moreInfo')
.setText('For more information consult https://github.com/AlchimiaMx/jira-connectors/tree/master#readme')
config.setDateRangeRequired(true);
return config.build();
}
/**
* Returns the fields for the connector.
*
* @returns {Object} The fields for the connector.
*/
function getFields() {
var cc = DataStudioApp.createCommunityConnector();
var fields = cc.getFields();
var types = cc.FieldType;
fields.newDimension()
.setId('keys')
.setName('Keys')
.setType(types.TEXT);
fields.newDimension()
.setId('summary')
.setName('Summary')
.setType(types.TEXT);
fields.newDimension()
.setId('issuetype')
.setName('Issuetype')
.setType(types.TEXT);
fields.newDimension()
.setId('creator')
.setName('Creator')
.setType(types.TEXT);
fields.newDimension()
.setId('assignee')
.setName('Assignee')
.setType(types.TEXT);
fields.newDimension()
.setId('reporter')
.setName('Reporter')
.setType(types.TEXT);
fields.newDimension()
.setId('priority')
.setName('Priority')
.setType(types.TEXT);
fields.newDimension()
.setId('status')
.setName('Status')
.setType(types.TEXT);
fields.newDimension()
.setId('statusName')
.setName('Status Name')
.setType(types.TEXT);
fields.newDimension()
.setId('created')
.setName('Created')
.setType(types.YEAR_MONTH_DAY);
fields.newDimension()
.setId('createdDayOfWeek')
.setName('Created (day of week)')
.setType(types.DAY_OF_WEEK);
fields.newDimension()
.setId('createdHour')
.setName('Created (hour)')
.setType(types.HOUR);
fields.newDimension()
.setId('duedate')
.setName('Due Date')
.setType(types.YEAR_MONTH_DAY);
fields.newDimension()
.setId('resolutiondate')
.setName('Resolution Date')
.setType(types.YEAR_MONTH_DAY);
fields.newDimension()
.setId('resolutiondateDayOfWeek')
.setName('Resolution Date (day of week)')
.setType(types.DAY_OF_WEEK);
fields.newDimension()
.setId('resolutiondateHour')
.setName('Resolution Date (hour)')
.setType(types.HOUR);
fields.newDimension()
.setId('changeSatartDate')
.setName('Change Start Date')
.setType(types.YEAR_MONTH_DAY);
fields.newDimension()
.setId('changeCompletionDate')
.setName('Change Completion Date')
.setType(types.YEAR_MONTH_DAY);
fields.newDimension()
.setId('component')
.setName('Component')
.setType(types.TEXT);
fields.newDimension()
.setId('label')
.setName('Label')
.setType(types.TEXT);
// fields.newDimension()
// .setId('epic')
// .setName('Epic')
// .setType(types.TEXT);
fields.newDimension()
.setId('project')
.setName('Project')
.setType(types.TEXT);
fields.newMetric()
.setId('satisfaction')
.setName('Satisfaction')
.setType(types.NUMBER);
fields.newMetric()
.setId('timespent')
.setName('Time Spent (s)')
.setType(types.DURATION);
fields.newMetric()
.setId('timeFirstResponse')
.setName('Time to First Response (s)')
.setType(types.DURATION);
fields.newMetric()
.setId('timeResolution')
.setName('Time to Resolution (s)')
.setType(types.DURATION);
fields.newDimension()
.setId('breachedFirstResponse')
.setName('Breached First Response')
.setType(types.BOOLEAN);
fields.newDimension()
.setId('breachedtimeResolution')
.setName('Breached Time Resolution')
.setType(types.BOOLEAN);
fields.newMetric()
.setId('issues')
.setName('Issues')
.setType(types.NUMBER);
fields.newDimension()
.setId('parent_key')
.setName('Parent Key')
.setType(types.TEXT)
fields.newDimension()
.setId('parent_summary')
.setName('Parent Summary')
.setType(types.TEXT)
fields.newDimension()
.setId('parent_status')
.setName('Parent Status')
.setType(types.TEXT)
fields.newDimension()
.setId('parent_status_name')
.setName('Parent Status Name')
.setType(types.TEXT)
fields.newDimension()
.setId('parent_priority')
.setName('Parent Priority')
.setType(types.TEXT)
fields.newDimension()
.setId('parent_issuetype')
.setName('Parent Issue Type')
.setType(types.TEXT)
fields.newMetric()
.setId('timespentUser')
.setName('Time Spent (s) by worklogs')
.setType(types.DURATION);
fields.newDimension()
.setId('createdWorklog')
.setName('Created Worklog')
.setType(types.YEAR_MONTH_DAY);
fields.newDimension()
.setId('createdWorklogDayOfWeek')
.setName('Created Worklog (day of week)')
.setType(types.DAY_OF_WEEK);
fields.newDimension()
.setId('createdWorklogHour')
.setName('Created Worklog (hour)')
.setType(types.HOUR);
fields.newDimension()
.setId('startedWorklog')
.setName('Started Worklog')
.setType(types.YEAR_MONTH_DAY);
fields.newDimension()
.setId('startedWorklogDayOfWeek')
.setName('Started Worklog (day of week)')
.setType(types.DAY_OF_WEEK);
fields.newDimension()
.setId('startedWorklogHour')
.setName('Started Worklog (hour)')
.setType(types.HOUR);
fields.newDimension()
.setId('authorWorklog')
.setName('Author Worklog')
.setType(types.TEXT);
fields.newDimension()
.setId('requestType')
.setName('Request Type')
.setType(types.TEXT);
return fields;
}
/**
* Returns the schema for the given request.
*
* @param {Object} request Schema request parameters.
* @returns {Object} Schema for the given request.
*/
function getSchema(request) {
return { schema: getFields().build() };
}
/**
* Returns Date format according to the Time Zone Script
*
* @param {String} date date in UTC
* @param {String} format desired date format
* @returns {String} date in new format
*/
function formatTypeDate(data, format) {
var result = Utilities.formatDate(new Date(data), Session.getScriptTimeZone(), format || "yyyyMMdd")
return ( format == 'u' && result == '7' ) ? '0': result
}
/**
* Formats a single row of data into the required format.
*
* @param {Object} requestedFields Fields requested in the getData request.
* @param {Object} response Contains the response data for jira cloud.
* @returns {Array} Rows values for requested fields in predefined format.
*/
function responseToRows(requestedFields, issues) {
return issues.map(function(issue) {
var row = [];
requestedFields.asArray().forEach(function (field) {
switch (field.getId()) {
case 'keys':
row.push(issue.key || undefined);
break;
case 'summary':
row.push(issue.fields.summary || undefined);
break;
case 'issuetype':
row.push(issue.fields.issuetype? issue.fields.issuetype.name : undefined);
break;
case 'creator':
row.push(issue.fields.creator? issue.fields.creator.displayName : undefined);
break;
case 'assignee':
row.push(issue.fields.assignee? issue.fields.assignee.displayName : undefined);
break;
case 'reporter':
row.push(issue.fields.reporter? issue.fields.reporter.displayName : undefined);
break;
case 'priority':
row.push(issue.fields.priority? issue.fields.priority.name : undefined);
break;
case 'status':
row.push(issue.fields.status? issue.fields.status.statusCategory.name : undefined);
break;
case 'statusName':
row.push(issue.fields.status? issue.fields.status.name : undefined);
break;
case 'created':
row.push(issue.fields.created? formatTypeDate(issue.fields.created) : undefined);
break;
case 'createdDayOfWeek':
row.push(issue.fields.created? formatTypeDate(issue.fields.created, 'u') : undefined);
break;
case 'createdHour':
row.push(issue.fields.created? formatTypeDate(issue.fields.created, 'HH') : undefined);
break;
case 'duedate':
row.push(issue.fields.duedate? formatTypeDate(issue.fields.duedate) : undefined);
break;
case 'resolutiondate':
row.push(issue.fields.resolutiondate? formatTypeDate(issue.fields.resolutiondate) : undefined);
break;
case 'resolutiondateDayOfWeek':
row.push(issue.fields.resolutiondate? formatTypeDate(issue.fields.resolutiondate, 'u') : undefined);
break;
case 'resolutiondateHour':
row.push(issue.fields.resolutiondate? formatTypeDate(issue.fields.resolutiondate, 'HH') : undefined);
break;
case 'component':
row.push(issue.fields.components[0]? issue.fields.components[0].name: '');
break;
case 'label':
row.push(issue.fields.labels? issue.fields.labels.join(): '');
break;
// case 'epic':
// row.push(issue.fields.labels[0]? issue.fields.labels[0]: '');
// break;
case 'project':
row.push(issue.fields.project? issue.fields.project.key : undefined);
break;
case 'satisfaction':
row.push(issue.fields.customfield_10204? issue.fields.customfield_10204.rating : undefined);
break;
case 'timespent':
row.push(issue.fields.aggregatetimespent? issue.fields.aggregatetimespent + '': undefined);
break;
case 'issues':
row.push(1);
break;
case 'timeFirstResponse':
row.push(issue.fields.customfield_10208? issue.fields.customfield_10208.completedCycles? issue.fields.customfield_10208.completedCycles[0]? (Math.round(issue.fields.customfield_10208.completedCycles[0].elapsedTime.millis/1000)) +'': undefined: undefined : undefined );
break;
case 'breachedFirstResponse':
row.push(issue.fields.customfield_10208? issue.fields.customfield_10208.completedCycles? issue.fields.customfield_10208.completedCycles[0]? issue.fields.customfield_10208.completedCycles[0].breached: undefined: undefined: undefined );
break;
case 'timeResolution':
row.push(issue.fields.customfield_10207? issue.fields.customfield_10207.completedCycles? issue.fields.customfield_10207.completedCycles[0]? (Math.round(issue.fields.customfield_10207.completedCycles[0].elapsedTime.millis/1000)) +'' : undefined: undefined :undefined);
break;
case 'changeSatartDate':
row.push(issue.fields.customfield_10214? formatTypeDate(issue.fields.customfield_10214): undefined);
break;
case 'changeCompletionDate':
row.push(issue.fields.customfield_10215? formatTypeDate(issue.fields.customfield_10215): undefined);
break;
case 'breachedtimeResolution':
row.push(issue.fields.customfield_10207? issue.fields.customfield_10207.completedCycles? issue.fields.customfield_10207.completedCycles[0]? issue.fields.customfield_10207.completedCycles[0].breached: undefined: undefined: undefined );
break;
case 'parent_key':
row.push(issue.fields.parent? issue.fields.parent.key? issue.fields.parent.key : undefined : undefined);
break;
case 'parent_summary':
row.push(issue.fields.parent? issue.fields.parent.fields? issue.fields.parent.fields.summary? issue.fields.parent.fields.summary : undefined: undefined: undefined );
break;
case 'parent_status':
row.push(issue.fields.parent? issue.fields.parent.fields? issue.fields.parent.fields.status? issue.fields.parent.fields.status.statusCategory? issue.fields.parent.fields.status.statusCategory.name : undefined : undefined: undefined: undefined );
break;
case 'parent_status_name':
row.push(issue.fields.parent? issue.fields.parent.fields? issue.fields.parent.fields.status? issue.fields.parent.fields.status.name : undefined: undefined: undefined );
break;
case 'parent_priority':
row.push(issue.fields.parent? issue.fields.parent.fields? issue.fields.parent.fields.priority? issue.fields.parent.fields.priority.name : undefined: undefined: undefined );
break;
case 'parent_issuetype':
row.push(issue.fields.parent? issue.fields.parent.fields? issue.fields.parent.fields.issuetype? issue.fields.parent.fields.issuetype.name : undefined: undefined: undefined );
break;
case 'timespentUser':
row.push(issue.timeSpentSeconds? issue.timeSpentSeconds + '': undefined);
break;
case 'createdWorklog':
row.push(issue.created? formatTypeDate(issue.created) : undefined);
break;
case 'createdWorklogDayOfWeek':
row.push(issue.created? formatTypeDate(issue.created, 'u') : undefined);
break;
case 'createdWorklogHour':
row.push(issue.created? formatTypeDate(issue.created, 'HH') : undefined);
break;
case 'startedWorklog':
row.push(issue.created? formatTypeDate(issue.created) : undefined);
break;
case 'startedWorklogDayOfWeek':
row.push(issue.created? formatTypeDate(issue.created, 'u') : undefined);
break;
case 'startedWorklogHour':
row.push(issue.created? formatTypeDate(issue.created, 'HH') : undefined);
break;
case 'authorWorklog':
row.push(issue.author? issue.author.displayName: undefined)
break;
case 'requestType':
row.push(issue.fields.customfield_10202? issue.fields.customfield_10202.requestType? issue.fields.customfield_10202.requestType.name : undefined : undefined )
break;
default:
row.push('');
}
});
return { values: row };
});
}
/**
* iterates to evade the query limit
*
* @param {Array} fieldsToRequest array of fields for consultation
* @param {Object} configParams config request parameters
* @param {Number} startIssues start of issues to consult
* @param {Array} accumulated cumulative issues of the query
* @returns {Array} accumulated all issues of the query
*/
function getFullIssuesByAPI(fieldsToRequest, request, startIssues, accumulated){
var maxResults = 100;
var data = {
'jql': 'created >= "' + request.dateRange.startDate + '" AND created <= "' + request.dateRange.endDate + ' 23:59" AND ' + request.configParams.JQL,
'maxResults': maxResults,
'startAt': startIssues,
'fields': fieldsToRequest
};
var auth = Utilities.base64Encode(request.configParams.User + ':' + request.configParams.Token);
var opts = {
'method': 'post',
'contentType': 'application/json',
'headers': {
'Accept': 'application/json',
'Authorization': 'Basic ' + auth,
'cache-control': 'no-cache'
},
'payload': JSON.stringify(data),
'muteHttpExceptions': true
};
var url = 'https://' + request.configParams.Subdomain + '.atlassian.net/rest/api/2/search';
try {
var response = UrlFetchApp.fetch(url, opts);
var parsedResponse = JSON.parse(response);
accumulated = accumulated.concat(parsedResponse.issues);
if (parsedResponse.total - (startIssues + maxResults) > 0 ){
return getFullIssuesByAPI(fieldsToRequest, request, startIssues + maxResults, accumulated );
} else {
return accumulated;
}
} catch (e) {
connector.throwError(e, true);
}
}
/**
* Returns the tabular data for the given request.
*
* @param {Object} request Data request parameters.
* @returns {Object} Contains the schema and data for the given request.
*/
function getData(request) {
var requestedFieldIds = request.fields.map(function(field) {
return field.name;
});
var requestedFields = getFields().forIds(requestedFieldIds);
var fieldsToRequest = request.configParams.worklogs? ['worklog'] : []
requestedFields.asArray().forEach(function (field) {
if ( fieldsToRequest.indexOf( fieldsDictionary[field.getId()] == -1 ) ) {
fieldsToRequest.push(fieldsDictionary[field.getId()])
}
})
try {
var responseFullIssues = getFullIssuesByAPI(fieldsToRequest, request, 0, []);
} catch (e) {
connector.throwError(e, true);
}
if (request.configParams.worklogs) {
try {
var unwind = responseFullIssues.map(function(issue){
if (issue.fields.worklog){
issue.fields.worklog.worklogs.map(function(worklog){
worklog.fields = issue.fields
worklog.key = issue.key
return worklog
});
}
return issue
})
responseFullIssues = unwind.reduce(function(ac,cu){
if (cu.fields.worklog) return ac.concat(cu.fields.worklog.worklogs)
else return ac
}, []);
} catch (e) {
connector.throwError(e, true);
}
}
try{
var rows = responseToRows(requestedFields, responseFullIssues);
} catch (e) {
connector.throwError(e, true);
}
return {
schema: requestedFields.build(),
rows: rows
};
}
function isAdminUser() {
return true;
}
connector.throwError = function(message, userSafe) {
console.log(message)
if (userSafe) {
message = 'DS_USER:' + message;
}
throw new Error(message);
};