-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathissues.js
More file actions
516 lines (446 loc) · 13 KB
/
Copy pathissues.js
File metadata and controls
516 lines (446 loc) · 13 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
// Packages
import inquirer from 'inquirer';
import color from 'chalk';
import Table from 'cli-table3';
import moment from 'moment';
import opn from 'opn';
// Local
import jira from './jira';
export default class JiraIssues {
/**
* Get required meta data to create issues
*/
getMetaData() {
return jira.apiRequest('/issue/createmeta');
}
/**
* Get custom fields
*/
getFields() {
return jira.apiRequest('/field');
}
/**
* Crate a new issue
* Docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-createIssue
*/
createIssue( newIssue ) {
// Create new issue
jira.api.addNewIssue( newIssue )
.then(function( issue ) {
let config = jira.config.defaults;
console.log('');
console.log('New issue: ' + color.bold.red(issue.key));
console.log(config.protocol + '://' + config.host + '/browse/' + issue.key);
console.log('');
})
.catch(function( res ) {
jira.showErrors( res );
});
}
/**
* Crate a new issue object
* Docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-createIssue
*/
createIssueObj( options = {} ) {
const _this = this;
this.getMetaData().then(function( meta ){
var projects = [];
var keys = [];
var issueTypes = [];
var selectedProject;
// Populate projects, keys and their respective issue types
for ( var index in meta.projects ){
projects.push(meta.projects[index].name);
keys.push(meta.projects[index].key);
issueTypes.push(meta.projects[index].issuetypes);
}
// Create the project question
var project = [
{
type: 'list',
name: 'project',
message: 'Project: ',
choices: projects,
filter: function( val ){
selectedProject = projects.indexOf( val );
return keys[selectedProject];
}
}
];
inquirer.prompt( project ).then(function( answers1 ){
var projectIssueTypes = [];
// Get issue types of the selected project
for ( var i in issueTypes[selectedProject] ){
projectIssueTypes.push({
id: issueTypes[selectedProject][i].id,
name: issueTypes[selectedProject][i].name,
subtask: issueTypes[selectedProject][i].subtask
});
}
var questions = [
{
type: 'list',
name: 'issueType',
message: 'Issue type: ',
choices: projectIssueTypes,
filter: function( val ){
return projectIssueTypes.find(function( obj ){
return obj.name == val;
});
}
},
{
type: 'input',
name: 'issueName',
message: 'Please provide the issue name :',
default: 'New Issue'
}
];
// Ask for the issue name and type
inquirer.prompt( questions ).then(function( answers2 ){
// Create the issue object
var newIssue = {
fields: {
project: {
key: answers1.project
},
summary: answers2.issueName,
issuetype: {
id: answers2.issueType.id
}
}
};
// Assign the issue to the current user if self option is passed
if( typeof options.self !== 'undefined' ) {
newIssue.fields.assignee = { name: jira.config.defaults.username };
}
if( answers2.issueType.subtask ){
var question = [
{
type: 'input',
name: 'issueParentName',
message: 'Please provide the parent issue key:'
}
];
inquirer.prompt( question ).then(function( answers3 ){
newIssue.fields.parent = {
key: answers3.issueParentName
};
_this.createIssue( newIssue );
});
} else if( answers2.issueType.name == 'Epic' ) {
// Get epic name custom field
_this.getFields().then(function(data){
let epicNameField = data.filter(function(val){
return val.custom && val.name == 'Epic Name';
});
var question = [
{
type: 'input',
name: 'epicName',
message: 'Epic name:'
}
];
inquirer.prompt( question ).then(function( answers3 ){
newIssue.fields[epicNameField[0].id] = answers3.epicName;
_this.createIssue( newIssue );
});
});
} else {
_this.createIssue( newIssue );
}
}).catch((err) => {
console.log(err);
process.exit();
});
});
});
}
/**
* Search issues
*/
search ( args ) {
const _this = this;
jira.api.searchJira( "summary ~ '" + args + "'" ).then(function( r ){
if( r.total ){
_this.showIssues( r.issues );
console.log( color.bold( " Total issues found: " + color.green( r.total ) ) );
console.log('');
} else {
jira.showError( "No issues found with search terms: '" + args + "'" );
}
}).catch(function( res ){
jira.showErrors( res );
process.exit();
});
}
/**
* Open issue in default browser
*/
openIssue( issue ) {
const _this = this;
let config = jira.config.defaults;
jira.api.findIssue( issue ).then(function(){
opn( config.protocol + '://' + config.host + '/browse/' + issue, {wait: false});
}).catch(function( res ){
jira.showErrors( res );
process.exit();
});
}
/**
* Show issues in a table format
*/
showIssues( issues ) {
const _this = this;
let issueSelectableList = [];
issues.forEach(function( issue){
const rowTable = new Table({
chars: { 'top': '' , 'top-mid': '' , 'top-left': '' , 'top-right': ''
, 'bottom': '' , 'bottom-mid': '' , 'bottom-left': '' , 'bottom-right': ''
, 'left': '' , 'left-mid': '' , 'mid': '' , 'mid-mid': ''
, 'right': '' , 'right-mid': '' , 'middle': ' ' },
style: { 'padding-left': 0, 'padding-right': 0 }
});
rowTable.push([color.blue( issue.key ), color.green( issue.fields.status.name ), issue.fields.summary ]);
issueSelectableList.push({ name: rowTable.toString(), key: issue.key});
});
var question = [
{
type: 'list',
name: 'selectedIssue',
message: 'Select Issue: ',
choices: issueSelectableList,
filter: function( val ){
return issueSelectableList.find(function( obj ){
return obj.name == val;
});
}
}
];
inquirer.prompt( question )
.then(function( res ){
_this.findIssue(res.selectedIssue.key);
});
}
/**
* Show issue detail in pretty format
*/
showIssue( issue ) {
const table = new Table({ chars: jira.tableChars });
const detailTable = new Table({ chars: jira.tableChars });
let status;
// Set status format
switch( issue.fields.status.name ) {
case 'Done':
status = color.green( 'Done' );
break;
case 'In Progress':
status = color.yellow( 'In Progress' );
break;
case 'To Do':
status = color.blue( 'To Do' );
break;
default:
status = issue.fields.status.name;
break;
}
table.push(
{ 'Summary': issue.fields.summary.trim() }
, { 'Status': status }
, { 'Type': issue.fields.issuetype.name }
, { 'Project': issue.fields.project.name + ' (' + issue.fields.project.key + ')' }
, { 'Reporter': issue.fields.reporter.name });
// If issue has assignee
if( issue.fields.assignee != null ) {
table.push( { 'Assignee': issue.fields.assignee.name } );
}
table.push(
{ 'Priority': issue.fields.priority.name }
);
// Start with detail table
table.push(
{ '': '' }
, { 'Id': issue.id }
, { 'Created on': moment( issue.fields.created ).format('MMMM Do YYYY, h:mm:ss a') }
, { 'Updated on': moment( issue.fields.updated ).format('MMMM Do YYYY, h:mm:ss a') }
);
// Fixes versions
if ( issue.fields.fixVersions.length ) {
let versions = [];
issue.fields.fixVersions.forEach(function( version ){
versions.push( version.name );
});
table.push( { 'Fix Versions:': versions.join( ', ' ) } );
}
// If issue has resolution
if( issue.fields.resolution != null ) {
table.push( { 'Resolution': issue.fields.resolution.name } );
}
console.log( table.toString() );
}
/**
* Get default issues summary
*/
summary( user ) {
const _this = this;
let jql;
if ( user ) {
jql = 'assignee = ' + user + ' and resolution = Unresolved';
} else {
jql = 'assignee = currentUser() and resolution = Unresolved';
}
jira.api.searchJira( jql ).then(function( r ){
if ( typeof r.warningMessages !== 'undefined' ) {
jira.showErrors( r );
} else {
if( r.total ){
_this.showIssues( r.issues );
}
}
}).catch(function( r ){
jira.showErrors( r );
});
}
/**
* Get release issues
*/
getReleaseIssues( options ) {
const _this = this;
jira.api.searchJira('project = ' + options.project + ' and fixVersion = ' + options.release ).then(function( r ){
if( r.total ){
_this.showIssues( r.issues );
}
}).catch(function( res ){
jira.showErrors( res );
process.exit();
});
}
/**
* Find specific issue
*/
findIssue( issue ) {
const _this = this;
jira.api.findIssue( issue ).then(function( r ){
_this.showIssue( r );
}).catch(function( res ){
jira.showErrors( res );
process.exit();
});
}
/**
* Assign issue to user
*/
assignIssue( issue, user ) {
jira.apiRequest(`/issue/${issue}/assignee`,{
method: 'PUT',
followAllRedirects: true,
body: {
name: user
}
}).then(function(){
console.log();
console.log( color.green(` Issue ${issue} successfully assigned to ${user}`) );
console.log();
}).catch(function( res ){
jira.showErrors( res );
});
}
/**
* Get project issues
*/
getProjectIssues( project ) {
const _this = this;
jira.api.searchJira( 'project = ' + project + ' and resolution = Unresolved').then(function( r ){
if( r.total ){
_this.showIssues( r.issues );
}
}).catch(function( res ){
jira.showErrors( res );
process.exit();
});
}
/**
* Get issue transitions
*/
async getIssueTransitions( issueId ) {
return jira.api.listTransitions( issueId )
.then(function( res ) {
let transitions = [];
const transitionObj = res.transitions;
for (var index in transitionObj){
transitions.push({
id: transitionObj[index].id,
name: transitionObj[index].name,
to: transitionObj[index].to.name
});
}
return transitions;
})
.catch(function(err) {
jira.showErrors(err);
});
}
/**
* API issue transition
*/
transitionIssue( issueId, transitionObj ){
return jira.api.transitionIssue( issueId, transitionObj )
.then( function(res){
console.log();
console.log( ' Issue [' + issueId + '] moved to ' + color.green.bold(transitionObj.to) + '.' );
console.log();
})
.catch( function (res){
jira.showErrors(res);
});
}
/**
* Make issue transition
*/
async makeTransition( issueId ){
const transitions = await this.getIssueTransitions(issueId);
const _this = this;
if ( transitions.length == 1 ) {
const obj = {
transition: {
id: transitions[0].id
},
to: transitions[0].to
};
return this.transitionIssue( issueId, obj );
} else {
var question = [
{
type: 'list',
name: 'transition',
message: 'Transitions: ',
choices: transitions,
filter: function(val){
return transitions.find(function(obj){
return obj.name == val;
});
}
}
];
inquirer.prompt(question).then(function( res ) {
const obj = {
transition: {
id: res.transition.id
},
to: res.transition.to
};
return _this.transitionIssue( issueId, obj );
});
}
}
/**
* Add comment to issue
*/
addComment( issueId, comment ){
return jira.api.addComment( issueId, comment ).then(function (res) {
console.log(' Issue ' + issueId + ' successfully updated with comment:\n ' + comment);
}).catch(function (err) {
jira.showErrors(res);
});
}
}