jira-pilot 2.0.1 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,707 +1,925 @@
1
- import { Command } from 'commander';
2
- import chalk from 'chalk';
3
- import { table } from 'table';
4
- import { api } from '../services/api-service.js';
5
- import ora from 'ora';
6
- import enquirer from 'enquirer';
7
- import { parseADF } from '../utils/adf-parser.js';
8
- import { textToADF } from '../utils/text-to-adf.js';
9
- import { validateIssueKey } from '../utils/validators.js';
10
-
11
- export function registerIssueCommand(program) {
12
- const issueCmd = new Command('issue')
13
- .description('Manage Jira issues')
14
- .addHelpText('after', `
15
- Common Actions:
16
- $ jira issue list # List assigned issues
17
- $ jira issue view <KEY> # View issue details
18
- $ jira issue create # Create new issue (interactive)
19
- $ jira issue transition <KEY> # Move issue status
20
- `);
21
-
22
- issueCmd
23
- .command('list')
24
- .description('List issues')
25
- .option('-j, --jql <query>', 'JQL query to filter issues')
26
- .option('-l, --limit <number>', 'Limit results', '20')
27
- .option('-p, --project <key>', 'Filter by project')
28
- .option('-a, --assignee <id>', 'Filter by assignee (use "currentUser" for self)')
29
- .option('-s, --status <status>', 'Filter by status')
30
- .option('-e, --export <format>', 'Export output (json, md)')
31
- .addHelpText('after', `
32
- Examples:
33
- $ jira issue list --project PROJ --status "In Progress"
34
- $ jira issue list --assignee currentUser --limit 10
35
- $ jira issue list --jql "created >= -7d"
36
- $ jira issue list --export json
37
- `)
38
- .action(async (options) => {
39
- const spinner = ora('Fetching issues...').start();
40
- try {
41
- const jqlParts = [];
42
- if (options.project) jqlParts.push(`project = "${options.project}"`);
43
- if (options.assignee) jqlParts.push(`assignee = ${options.assignee === 'currentUser' ? 'currentUser()' : `"${options.assignee}"`}`);
44
- if (options.status) jqlParts.push(`status = "${options.status}"`);
45
- if (options.jql) jqlParts.push(options.jql);
46
-
47
- // Order by updated desc by default if no JQL
48
- if (!options.jql && jqlParts.length === 0) {
49
- jqlParts.push('order by updated DESC');
50
- } else if (jqlParts.length > 0 && !options.jql) {
51
- // Add order if not custom jql
52
- // jqlParts.push('order by updated DESC');
53
- }
54
-
55
- const jql = jqlParts.join(' AND ');
56
-
57
- const searchApi = '/search/jql';
58
- const body = {
59
- jql: jql || 'created is not empty',
60
- maxResults: parseInt(options.limit),
61
- fields: ['summary', 'status', 'assignee', 'created', 'updated', 'description']
62
- };
63
-
64
- const data = await api.post(searchApi, body);
65
- spinner.stop();
66
-
67
- if (!data.issues || data.issues.length === 0) {
68
- console.log(chalk.yellow('No issues found.'));
69
- return;
70
- }
71
-
72
- // Handling Export
73
- if (options.export) {
74
- const fs = await import('fs');
75
- const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
76
-
77
- if (options.export === 'json') {
78
- const filename = `issues-${timestamp}.json`;
79
- fs.writeFileSync(filename, JSON.stringify(data.issues, null, 2));
80
- console.log(chalk.green(`\nExported ${data.issues.length} issues to ${chalk.bold(filename)}`));
81
- return;
82
- }
83
-
84
- if (options.export === 'md') {
85
- const filename = `issues-${timestamp}.md`;
86
- let mdContent = `# Jira Issues Export\nGenerated: ${new Date().toLocaleString()}\n\n`;
87
- mdContent += `| Key | Summary | Status | Assignee |\n`;
88
- mdContent += `|---|---|---|---|\n`;
89
-
90
- data.issues.forEach(i => {
91
- const key = i.key;
92
- const summary = i.fields.summary || '';
93
- const status = i.fields.status?.name || '';
94
- const assignee = i.fields.assignee?.displayName || 'Unassigned';
95
- mdContent += `| ${key} | ${summary} | ${status} | ${assignee} |\n`;
96
- });
97
-
98
- fs.writeFileSync(filename, mdContent);
99
- console.log(chalk.green(`\nExported ${data.issues.length} issues to ${chalk.bold(filename)}`));
100
- return;
101
- }
102
- }
103
-
104
- const tableData = [
105
- [chalk.bold('Key'), chalk.bold('Summary'), chalk.bold('Status'), chalk.bold('Assignee'), chalk.bold('Created'), chalk.bold('Updated')]
106
- ];
107
-
108
- data.issues.forEach(i => {
109
- tableData.push([
110
- chalk.cyan(i.key),
111
- i.fields.summary ? (i.fields.summary.length > 50 ? i.fields.summary.substring(0, 47) + '...' : i.fields.summary) : '',
112
- i.fields.status ? i.fields.status.name : '',
113
- i.fields.assignee ? i.fields.assignee.displayName : 'Unassigned',
114
- i.fields.created ? i.fields.created.split('T')[0] : '',
115
- i.fields.updated ? i.fields.updated.split('T')[0] : ''
116
- ]);
117
- });
118
-
119
- console.log(table(tableData));
120
-
121
- } catch (e) {
122
- spinner.fail('Failed to list issues');
123
- if (e.response) {
124
- console.error(chalk.red(`Error ${e.response.status}: `), e.response.data);
125
- } else {
126
- console.error(chalk.red(e.message));
127
- }
128
- }
129
- });
130
-
131
- issueCmd
132
- .command('view')
133
- .description('View issue details')
134
- .argument('<issueKey>', 'Issue Key')
135
- .addHelpText('after', `
136
- Examples:
137
- $ jira issue view PROJ-123
138
- `)
139
- .action(async (issueKey) => {
140
- const check = validateIssueKey(issueKey);
141
- if (!check.valid) { console.error(chalk.red(check.message)); return; }
142
- const spinner = ora(`Fetching issue ${issueKey}...`).start();
143
- try {
144
- const issue = await api.get(`/issue/${issueKey}`);
145
- spinner.stop();
146
-
147
- console.log(chalk.bold(`\n${issue.key}: ${issue.fields.summary}`));
148
- console.log(chalk.grey(`${issue.fields.issuetype.name} - ${issue.fields.status.name} - ${issue.fields.priority ? issue.fields.priority.name : 'No Priority'}`));
149
- console.log(chalk.bold('\nDescription:'));
150
- console.log(parseADF(issue.fields.description) || 'No description provided.');
151
-
152
- if (issue.fields.assignee) {
153
- console.log(chalk.bold('\nAssignee: ') + issue.fields.assignee.displayName);
154
- }
155
-
156
- if (issue.fields.comment && issue.fields.comment.comments.length > 0) {
157
- console.log(chalk.bold('\nComments:'));
158
- issue.fields.comment.comments.forEach(c => {
159
- console.log(chalk.cyan(c.author.displayName) + ': ' + c.body);
160
- });
161
- }
162
- console.log('');
163
- } catch (e) {
164
- spinner.fail('Failed to fetch issue');
165
- if (e.response) {
166
- if (e.response.status === 404) {
167
- console.error(chalk.red(`Issue "${issueKey}" not found.`));
168
- } else {
169
- console.error(chalk.red(`Error ${e.response.status}: `), e.response.data);
170
- }
171
- } else {
172
- console.error(chalk.red(e.message));
173
- }
174
- }
175
- });
176
-
177
- // ── CREATE ────────────────────────────────────────────────────────
178
- issueCmd
179
- .command('create')
180
- .description('Create a new Jira issue')
181
- .option('-p, --project <key>', 'Project key')
182
- .option('-t, --type <type>', 'Issue type (e.g., Bug, Story, Task)')
183
- .option('-s, --summary <text>', 'Issue summary')
184
- .option('-d, --description <text>', 'Issue description')
185
- .option('--priority <name>', 'Priority name (e.g., High, Medium, Low)')
186
- .option('-a, --assignee <id>', 'Assignee account ID (use "me" for self)')
187
- .addHelpText('after', `
188
- Examples:
189
- $ jira issue create # Interactive wizard
190
- $ jira issue create -p PROJ -s "Fix login bug" # Quick create
191
- $ jira issue create -p PROJ -t Bug -s "Crash on save" --priority High
192
- $ jira issue create -p PROJ -s "New feature" -a me
193
- `)
194
- .action(async (options) => {
195
- try {
196
- // ── Step 1: Select Project ──────────────────────────
197
- let projectKey = options.project;
198
- if (!projectKey) {
199
- const spinner = ora('Fetching projects...').start();
200
- const projectData = await api.get('/project/search');
201
- spinner.stop();
202
-
203
- if (!projectData.values || projectData.values.length === 0) {
204
- console.error(chalk.red('No projects found. Check your permissions.'));
205
- return;
206
- }
207
-
208
- const projectChoices = projectData.values.map(p => ({
209
- name: p.key,
210
- message: `${p.key} ${p.name}`
211
- }));
212
-
213
- const { selectedProject } = await enquirer.prompt({
214
- type: 'select',
215
- name: 'selectedProject',
216
- message: 'Select Project:',
217
- choices: projectChoices
218
- });
219
- projectKey = selectedProject;
220
- }
221
-
222
- // ── Step 2: Select Issue Type ───────────────────────
223
- let issueTypeName = options.type;
224
- if (!issueTypeName) {
225
- const spinner = ora('Fetching issue types...').start();
226
- let issueTypes = [];
227
- try {
228
- // Jira Cloud v3 - createmeta endpoint
229
- const metaData = await api.get(`/issue/createmeta/${projectKey}/issuetypes`);
230
- issueTypes = metaData.issueTypes || metaData.values || [];
231
- } catch (metaErr) {
232
- // Fallback: use project-level issue types
233
- try {
234
- const projectInfo = await api.get(`/project/${projectKey}`);
235
- issueTypes = projectInfo.issueTypes || [];
236
- } catch {
237
- issueTypes = [
238
- { name: 'Task' }, { name: 'Bug' },
239
- { name: 'Story' }, { name: 'Epic' }
240
- ];
241
- }
242
- }
243
- spinner.stop();
244
-
245
- if (issueTypes.length === 0) {
246
- issueTypes = [
247
- { name: 'Task' }, { name: 'Bug' },
248
- { name: 'Story' }, { name: 'Epic' }
249
- ];
250
- }
251
-
252
- // Filter out sub-tasks if present
253
- const filteredTypes = issueTypes.filter(t => !t.subtask);
254
- const typeChoices = (filteredTypes.length > 0 ? filteredTypes : issueTypes)
255
- .map(t => ({ name: t.name, message: t.name }));
256
-
257
- const { selectedType } = await enquirer.prompt({
258
- type: 'select',
259
- name: 'selectedType',
260
- message: 'Select Issue Type:',
261
- choices: typeChoices
262
- });
263
- issueTypeName = selectedType;
264
- }
265
-
266
- // ── Step 3: Summary (required) ──────────────────────
267
- let summary = options.summary;
268
- if (!summary) {
269
- const { inputSummary } = await enquirer.prompt({
270
- type: 'input',
271
- name: 'inputSummary',
272
- message: 'Summary (required):',
273
- validate: (val) => val.trim().length > 0 || 'Summary cannot be empty'
274
- });
275
- summary = inputSummary;
276
- }
277
-
278
- // ── Step 4: Description (optional) ──────────────────
279
- let description = options.description;
280
- if (description === undefined) {
281
- const { inputDescription } = await enquirer.prompt({
282
- type: 'input',
283
- name: 'inputDescription',
284
- message: 'Description (optional, press Enter to skip):'
285
- });
286
- description = inputDescription || null;
287
- }
288
-
289
- // ── Step 5: Priority ────────────────────────────────
290
- let priorityName = options.priority;
291
- if (!priorityName) {
292
- const spinner = ora('Fetching priorities...').start();
293
- try {
294
- const priorities = await api.get('/priority');
295
- spinner.stop();
296
-
297
- if (Array.isArray(priorities) && priorities.length > 0) {
298
- const priorityChoices = priorities.map(p => ({
299
- name: p.name,
300
- message: p.name
301
- }));
302
-
303
- const { selectedPriority } = await enquirer.prompt({
304
- type: 'select',
305
- name: 'selectedPriority',
306
- message: 'Select Priority:',
307
- choices: priorityChoices
308
- });
309
- priorityName = selectedPriority;
310
- }
311
- } catch {
312
- spinner.stop();
313
- // Priority endpoint may not be available; skip
314
- }
315
- }
316
-
317
- // ── Step 6: Assignee ────────────────────────────────
318
- let assigneeId = options.assignee;
319
- if (!assigneeId) {
320
- const { assigneeChoice } = await enquirer.prompt({
321
- type: 'select',
322
- name: 'assigneeChoice',
323
- message: 'Assign to:',
324
- choices: [
325
- { name: 'me', message: 'Myself' },
326
- { name: 'unassigned', message: 'Leave Unassigned' },
327
- { name: 'search', message: 'Search for a user...' }
328
- ]
329
- });
330
-
331
- if (assigneeChoice === 'me') {
332
- const spinner = ora('Fetching your account...').start();
333
- try {
334
- const myself = await api.get('/myself');
335
- assigneeId = myself.accountId;
336
- spinner.stop();
337
- } catch {
338
- spinner.fail('Could not fetch your account. Leaving unassigned.');
339
- assigneeId = null;
340
- }
341
- } else if (assigneeChoice === 'search') {
342
- const { searchQuery } = await enquirer.prompt({
343
- type: 'input',
344
- name: 'searchQuery',
345
- message: 'Search user by name or email:'
346
- });
347
-
348
- if (searchQuery.trim()) {
349
- const spinner = ora('Searching users...').start();
350
- try {
351
- const users = await api.get(`/user/search?query=${encodeURIComponent(searchQuery)}`);
352
- spinner.stop();
353
-
354
- if (Array.isArray(users) && users.length > 0) {
355
- const userChoices = users.map(u => ({
356
- name: u.accountId,
357
- message: `${u.displayName} (${u.emailAddress || u.accountId})`
358
- }));
359
-
360
- const { selectedUser } = await enquirer.prompt({
361
- type: 'select',
362
- name: 'selectedUser',
363
- message: 'Select User:',
364
- choices: userChoices
365
- });
366
- assigneeId = selectedUser;
367
- } else {
368
- console.log(chalk.yellow('No users found. Leaving unassigned.'));
369
- assigneeId = null;
370
- }
371
- } catch {
372
- spinner.fail('User search failed. Leaving unassigned.');
373
- assigneeId = null;
374
- }
375
- }
376
- } else {
377
- assigneeId = null;
378
- }
379
- } else if (assigneeId === 'me') {
380
- // --assignee me flag: resolve to account ID
381
- const spinner = ora('Fetching your account...').start();
382
- try {
383
- const myself = await api.get('/myself');
384
- assigneeId = myself.accountId;
385
- spinner.stop();
386
- } catch {
387
- spinner.fail('Could not fetch your account. Leaving unassigned.');
388
- assigneeId = null;
389
- }
390
- }
391
-
392
- // ── Confirmation ────────────────────────────────────
393
- console.log(chalk.blue('\n── Issue Summary ──────────────────'));
394
- console.log(` Project: ${chalk.cyan(projectKey)}`);
395
- console.log(` Type: ${issueTypeName}`);
396
- console.log(` Summary: ${summary}`);
397
- console.log(` Description: ${description || chalk.grey('(none)')}`);
398
- console.log(` Priority: ${priorityName || chalk.grey('(default)')}`);
399
- console.log(` Assignee: ${assigneeId || chalk.grey('Unassigned')}`);
400
- console.log(chalk.blue('──────────────────────────────────\n'));
401
-
402
- const { confirmed } = await enquirer.prompt({
403
- type: 'confirm',
404
- name: 'confirmed',
405
- message: 'Create this issue?',
406
- initial: true
407
- });
408
-
409
- if (!confirmed) {
410
- console.log(chalk.yellow('Issue creation cancelled.'));
411
- return;
412
- }
413
-
414
- // ── Build Request Body ──────────────────────────────
415
- const issueBody = {
416
- fields: {
417
- project: { key: projectKey },
418
- issuetype: { name: issueTypeName },
419
- summary: summary
420
- }
421
- };
422
-
423
- if (description) {
424
- issueBody.fields.description = textToADF(description);
425
- }
426
-
427
- if (priorityName) {
428
- issueBody.fields.priority = { name: priorityName };
429
- }
430
-
431
- if (assigneeId) {
432
- issueBody.fields.assignee = { accountId: assigneeId };
433
- }
434
-
435
- // ── Create Issue ────────────────────────────────────
436
- const spinner = ora('Creating issue...').start();
437
- const result = await api.post('/issue', issueBody);
438
- spinner.succeed(chalk.green(`Issue created: ${chalk.bold(result.key)}`));
439
-
440
- console.log(chalk.grey(`View it: jira issue view ${result.key}`));
441
-
442
- } catch (e) {
443
- if (e === '' || e.message === '') {
444
- // User cancelled prompt (Ctrl+C)
445
- console.log(chalk.yellow('\nCancelled.'));
446
- return;
447
- }
448
- console.error(chalk.red('\nFailed to create issue:'));
449
- if (e.response) {
450
- console.error(chalk.red(`Error ${e.response.status}: `), JSON.stringify(e.response.data, null, 2));
451
- } else {
452
- console.error(chalk.red(e.message));
453
- }
454
- }
455
- });
456
-
457
- // ── TRANSITION ────────────────────────────────────────────────────
458
- issueCmd
459
- .command('transition')
460
- .description('Transition an issue to a new status')
461
- .argument('<issueKey>', 'Issue Key (e.g., PROJ-123)')
462
- .option('-s, --status <name>', 'Target status name (skips interactive selection)')
463
- .addHelpText('after', `
464
- Examples:
465
- $ jira issue transition PROJ-123 # Interactive
466
- $ jira issue transition PROJ-123 --status "In Progress"
467
- $ jira issue transition PROJ-123 -s Done
468
- `)
469
- .action(async (issueKey, options) => {
470
- const check = validateIssueKey(issueKey);
471
- if (!check.valid) { console.error(chalk.red(check.message)); return; }
472
- const spinner = ora(`Fetching transitions for ${issueKey}...`).start();
473
- try {
474
- // Fetch current issue to show context
475
- const issue = await api.get(`/issue/${issueKey}?fields=summary,status`);
476
- const currentStatus = issue.fields.status.name;
477
-
478
- // Fetch available transitions
479
- const transData = await api.get(`/issue/${issueKey}/transitions`);
480
- spinner.stop();
481
-
482
- if (!transData.transitions || transData.transitions.length === 0) {
483
- console.log(chalk.yellow(`No transitions available for ${issueKey} (current status: ${currentStatus}).`));
484
- return;
485
- }
486
-
487
- console.log(chalk.bold(`\n${issue.key}: ${issue.fields.summary}`));
488
- console.log(chalk.grey(`Current Status: ${currentStatus}\n`));
489
-
490
- let targetTransition;
491
-
492
- if (options.status) {
493
- // Non-interactive: find matching transition
494
- targetTransition = transData.transitions.find(
495
- t => t.name.toLowerCase() === options.status.toLowerCase() ||
496
- t.to.name.toLowerCase() === options.status.toLowerCase()
497
- );
498
-
499
- if (!targetTransition) {
500
- console.error(chalk.red(`Status "${options.status}" is not a valid transition from "${currentStatus}".`));
501
- console.log(chalk.grey('Available transitions:'));
502
- transData.transitions.forEach(t => {
503
- console.log(chalk.grey(` • ${t.name} → ${t.to.name}`));
504
- });
505
- return;
506
- }
507
- } else {
508
- // Interactive: show selection
509
- const transitionChoices = transData.transitions.map(t => ({
510
- name: t.id,
511
- message: `${t.name} → ${chalk.cyan(t.to.name)}`
512
- }));
513
-
514
- const { selectedTransition } = await enquirer.prompt({
515
- type: 'select',
516
- name: 'selectedTransition',
517
- message: 'Select transition:',
518
- choices: transitionChoices
519
- });
520
-
521
- targetTransition = transData.transitions.find(t => t.id === selectedTransition);
522
- }
523
-
524
- // Execute transition
525
- const execSpinner = ora(`Transitioning to "${targetTransition.to.name}"...`).start();
526
- await api.post(`/issue/${issueKey}/transitions`, {
527
- transition: { id: targetTransition.id }
528
- });
529
- execSpinner.succeed(chalk.green(`${issueKey} transitioned: ${currentStatus} → ${chalk.bold(targetTransition.to.name)}`));
530
-
531
- } catch (e) {
532
- spinner.stop();
533
- if (e === '' || e.message === '') {
534
- console.log(chalk.yellow('\nCancelled.'));
535
- return;
536
- }
537
- console.error(chalk.red('\nFailed to transition issue:'));
538
- if (e.response) {
539
- if (e.response.status === 404) {
540
- console.error(chalk.red(`Issue "${issueKey}" not found.`));
541
- } else {
542
- console.error(chalk.red(`Error ${e.response.status}: `), e.response.data);
543
- }
544
- } else {
545
- console.error(chalk.red(e.message));
546
- }
547
- }
548
- });
549
- // ── ASSIGN ────────────────────────────────────────────────────────
550
- issueCmd
551
- .command('assign')
552
- .description('Assign or reassign an issue')
553
- .argument('<issueKey>', 'Issue Key (e.g., PROJ-123)')
554
- .option('-a, --assignee <id>', 'Assignee account ID (use "me" for self, "none" to unassign)')
555
- .addHelpText('after', `
556
- Examples:
557
- $ jira issue assign PROJ-123 # Interactive
558
- $ jira issue assign PROJ-123 -a me # Assign to yourself
559
- $ jira issue assign PROJ-123 -a none # Unassign
560
- `)
561
- .action(async (issueKey, options) => {
562
- const check = validateIssueKey(issueKey);
563
- if (!check.valid) { console.error(chalk.red(check.message)); return; }
564
- try {
565
- let assigneeId = options.assignee;
566
-
567
- if (!assigneeId) {
568
- // Interactive selection
569
- const spinner = ora(`Fetching issue ${issueKey}...`).start();
570
- const issue = await api.get(`/issue/${issueKey}?fields=summary,assignee`);
571
- spinner.stop();
572
-
573
- const currentAssignee = issue.fields.assignee?.displayName || 'Unassigned';
574
- console.log(chalk.bold(`\n${issue.key}: ${issue.fields.summary}`));
575
- console.log(chalk.grey(`Current Assignee: ${currentAssignee}\n`));
576
-
577
- const { assignChoice } = await enquirer.prompt({
578
- type: 'select',
579
- name: 'assignChoice',
580
- message: 'Assign to:',
581
- choices: [
582
- { name: 'me', message: 'Myself' },
583
- { name: 'none', message: 'Unassign' },
584
- { name: 'search', message: 'Search for a user...' }
585
- ]
586
- });
587
- assigneeId = assignChoice;
588
- }
589
-
590
- if (assigneeId === 'me') {
591
- const spinner = ora('Fetching your account...').start();
592
- const myself = await api.get('/myself');
593
- assigneeId = myself.accountId;
594
- spinner.stop();
595
- }
596
-
597
- if (assigneeId === 'search') {
598
- const { searchQuery } = await enquirer.prompt({
599
- type: 'input',
600
- name: 'searchQuery',
601
- message: 'Search user by name or email:'
602
- });
603
-
604
- const spinner = ora('Searching users...').start();
605
- const users = await api.get(`/user/search?query=${encodeURIComponent(searchQuery)}`);
606
- spinner.stop();
607
-
608
- if (!Array.isArray(users) || users.length === 0) {
609
- console.log(chalk.yellow('No users found.'));
610
- return;
611
- }
612
-
613
- const { selectedUser } = await enquirer.prompt({
614
- type: 'select',
615
- name: 'selectedUser',
616
- message: 'Select User:',
617
- choices: users.map(u => ({
618
- name: u.accountId,
619
- message: `${u.displayName} (${u.emailAddress || u.accountId})`
620
- }))
621
- });
622
- assigneeId = selectedUser;
623
- }
624
-
625
- const spinner = ora('Updating assignee...').start();
626
- const body = assigneeId === 'none'
627
- ? { accountId: null }
628
- : { accountId: assigneeId };
629
-
630
- await api.put(`/issue/${issueKey}/assignee`, body);
631
- spinner.succeed(chalk.green(`${issueKey} ${assigneeId === 'none' ? 'unassigned' : 'assigned'} successfully.`));
632
-
633
- } catch (e) {
634
- if (e === '' || e.message === '') {
635
- console.log(chalk.yellow('\nCancelled.'));
636
- return;
637
- }
638
- console.error(chalk.red('\nFailed to assign issue:'));
639
- if (e.response) {
640
- console.error(chalk.red(`Error ${e.response.status}: `), e.response.data);
641
- } else {
642
- console.error(chalk.red(e.message));
643
- }
644
- }
645
- });
646
-
647
- // ── COMMENT ───────────────────────────────────────────────────────
648
- issueCmd
649
- .command('comment')
650
- .description('Add a comment to an issue')
651
- .argument('<issueKey>', 'Issue Key (e.g., PROJ-123)')
652
- .option('-m, --message <text>', 'Comment text (skips interactive prompt)')
653
- .addHelpText('after', `
654
- Examples:
655
- $ jira issue comment PROJ-123 # Interactive
656
- $ jira issue comment PROJ-123 -m "Fixed in latest build"
657
- `)
658
- .action(async (issueKey, options) => {
659
- const check = validateIssueKey(issueKey);
660
- if (!check.valid) { console.error(chalk.red(check.message)); return; }
661
- try {
662
- let commentText = options.message;
663
-
664
- if (!commentText) {
665
- // Show issue context first
666
- const spinner = ora(`Fetching issue ${issueKey}...`).start();
667
- const issue = await api.get(`/issue/${issueKey}?fields=summary,status`);
668
- spinner.stop();
669
-
670
- console.log(chalk.bold(`\n${issue.key}: ${issue.fields.summary}`));
671
- console.log(chalk.grey(`Status: ${issue.fields.status.name}\n`));
672
-
673
- const { inputComment } = await enquirer.prompt({
674
- type: 'input',
675
- name: 'inputComment',
676
- message: 'Enter your comment:',
677
- validate: (val) => val.trim().length > 0 || 'Comment cannot be empty'
678
- });
679
- commentText = inputComment;
680
- }
681
-
682
- const spinner = ora('Adding comment...').start();
683
- await api.post(`/issue/${issueKey}/comment`, {
684
- body: textToADF(commentText)
685
- });
686
- spinner.succeed(chalk.green(`Comment added to ${issueKey}.`));
687
-
688
- } catch (e) {
689
- if (e === '' || e.message === '') {
690
- console.log(chalk.yellow('\nCancelled.'));
691
- return;
692
- }
693
- console.error(chalk.red('\nFailed to add comment:'));
694
- if (e.response) {
695
- if (e.response.status === 404) {
696
- console.error(chalk.red(`Issue "${issueKey}" not found.`));
697
- } else {
698
- console.error(chalk.red(`Error ${e.response.status}: `), e.response.data);
699
- }
700
- } else {
701
- console.error(chalk.red(e.message));
702
- }
703
- }
704
- });
705
-
706
- program.addCommand(issueCmd);
707
- }
1
+ import { Command } from 'commander';
2
+ import chalk from 'chalk';
3
+ import { table } from 'table';
4
+ import { api } from '../services/api-service.js';
5
+ import { aiService } from '../services/ai-service.js';
6
+ import ora from 'ora';
7
+ import enquirer from 'enquirer';
8
+ import { parseADF } from '../utils/adf-parser.js';
9
+ import { textToADF } from '../utils/text-to-adf.js';
10
+ import { validateIssueKey } from '../utils/validators.js';
11
+ import { handleCommandError } from '../utils/error-handler.js';
12
+
13
+ export function registerIssueCommand(program) {
14
+ const issueCmd = new Command('issue')
15
+ .description('Manage Jira issues')
16
+ .addHelpText('after', `
17
+ Common Actions:
18
+ $ jira issue list # List assigned issues
19
+ $ jira issue view <KEY> # View issue details
20
+ $ jira issue create # Create new issue (interactive)
21
+ $ jira issue transition <KEY> # Move issue status
22
+ `);
23
+
24
+ issueCmd
25
+ .command('list')
26
+ .description('List issues')
27
+ .option('-j, --jql <query>', 'JQL query to filter issues')
28
+ .option('--ask <query>', 'Filter issues using natural language query (AI)')
29
+ .option('-l, --limit <number>', 'Limit results', '20')
30
+ .option('-p, --project <key>', 'Filter by project')
31
+ .option('-a, --assignee <id>', 'Filter by assignee (use "currentUser" for self)')
32
+ .option('-s, --status <status>', 'Filter by status')
33
+ .option('-e, --export <format>', 'Export output (json, md)')
34
+ .option('-o, --output <format>', 'Output format (json)')
35
+ .addHelpText('after', `
36
+ Examples:
37
+ $ jira issue list --project PROJ --status "In Progress"
38
+ $ jira issue list --assignee currentUser --limit 10
39
+ $ jira issue list --jql "created >= -7d"
40
+ $ jira issue list --export json
41
+ `)
42
+ .action(async (options) => {
43
+ const spinner = ora('Fetching issues...').start();
44
+ try {
45
+ // Natural Language JQL
46
+ if (options.ask) {
47
+ const aiSpinner = ora(`Translating query: "${options.ask}"...`).start();
48
+ try {
49
+ const generatedJql = await aiService.generateJql(options.ask);
50
+ aiSpinner.succeed(`JQL: ${chalk.cyan(generatedJql)}`);
51
+ options.jql = generatedJql; // Override/Set JQL
52
+ } catch (e) {
53
+ aiSpinner.fail('Failed to translate query.');
54
+ console.error(chalk.red(e.message));
55
+ return;
56
+ }
57
+ }
58
+
59
+ const jqlParts = [];
60
+ if (options.project) jqlParts.push(`project = "${options.project}"`);
61
+ if (options.assignee) jqlParts.push(`assignee = ${options.assignee === 'currentUser' ? 'currentUser()' : `"${options.assignee}"`}`);
62
+ if (options.status) jqlParts.push(`status = "${options.status}"`);
63
+ if (options.jql) jqlParts.push(options.jql);
64
+
65
+ // Order by updated desc by default if no JQL
66
+ if (!options.jql && jqlParts.length === 0) {
67
+ jqlParts.push('order by updated DESC');
68
+ } else if (jqlParts.length > 0 && !options.jql) {
69
+ // Add order if not custom jql
70
+ // jqlParts.push('order by updated DESC');
71
+ }
72
+
73
+ const jql = jqlParts.join(' AND ');
74
+
75
+ const searchApi = '/search/jql';
76
+ const body = {
77
+ jql: jql || 'created is not empty',
78
+ maxResults: parseInt(options.limit),
79
+ fields: ['summary', 'status', 'assignee', 'created', 'updated', 'description']
80
+ };
81
+
82
+ const data = await api.post(searchApi, body);
83
+ spinner.stop();
84
+
85
+ if (!data.issues || data.issues.length === 0) {
86
+ console.log(chalk.yellow('No issues found.'));
87
+ return;
88
+ }
89
+
90
+ // Handling Export
91
+ if (options.export) {
92
+ const fs = await import('fs');
93
+ const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
94
+
95
+ if (options.export === 'json') {
96
+ const filename = `issues-${timestamp}.json`;
97
+ fs.writeFileSync(filename, JSON.stringify(data.issues, null, 2));
98
+ console.log(chalk.green(`\nExported ${data.issues.length} issues to ${chalk.bold(filename)}`));
99
+ return;
100
+ }
101
+
102
+ if (options.export === 'md') {
103
+ const filename = `issues-${timestamp}.md`;
104
+ let mdContent = `# Jira Issues Export\nGenerated: ${new Date().toLocaleString()}\n\n`;
105
+ mdContent += `| Key | Summary | Status | Assignee |\n`;
106
+ mdContent += `|---|---|---|---|\n`;
107
+
108
+ data.issues.forEach(i => {
109
+ const key = i.key;
110
+ const summary = i.fields.summary || '';
111
+ const status = i.fields.status?.name || '';
112
+ const assignee = i.fields.assignee?.displayName || 'Unassigned';
113
+ mdContent += `| ${key} | ${summary} | ${status} | ${assignee} |\n`;
114
+ });
115
+
116
+ fs.writeFileSync(filename, mdContent);
117
+ console.log(chalk.green(`\nExported ${data.issues.length} issues to ${chalk.bold(filename)}`));
118
+ return;
119
+ }
120
+ }
121
+
122
+ if (options.output === 'json') {
123
+ console.log(JSON.stringify(data.issues.map(i => ({
124
+ key: i.key, summary: i.fields.summary,
125
+ status: i.fields.status?.name, assignee: i.fields.assignee?.displayName || null,
126
+ created: i.fields.created, updated: i.fields.updated
127
+ })), null, 2));
128
+ return;
129
+ }
130
+
131
+ const tableData = [
132
+ [chalk.bold('Key'), chalk.bold('Summary'), chalk.bold('Status'), chalk.bold('Assignee'), chalk.bold('Created'), chalk.bold('Updated')]
133
+ ];
134
+
135
+ data.issues.forEach(i => {
136
+ tableData.push([
137
+ chalk.cyan(i.key),
138
+ i.fields.summary ? (i.fields.summary.length > 50 ? i.fields.summary.substring(0, 47) + '...' : i.fields.summary) : '',
139
+ i.fields.status ? i.fields.status.name : '',
140
+ i.fields.assignee ? i.fields.assignee.displayName : 'Unassigned',
141
+ i.fields.created ? i.fields.created.split('T')[0] : '',
142
+ i.fields.updated ? i.fields.updated.split('T')[0] : ''
143
+ ]);
144
+ });
145
+
146
+ console.log(table(tableData));
147
+
148
+ } catch (e) {
149
+ handleCommandError(spinner, e, 'Failed to list issues');
150
+ }
151
+ });
152
+
153
+ issueCmd
154
+ .command('view')
155
+ .description('View issue details')
156
+ .argument('<issueKey>', 'Issue Key')
157
+ .option('-o, --output <format>', 'Output format (json)')
158
+ .addHelpText('after', `
159
+ Examples:
160
+ $ jira issue view PROJ-123
161
+ $ jira issue view PROJ-123 --output json
162
+ `)
163
+ .action(async (issueKey, options) => {
164
+ const check = validateIssueKey(issueKey);
165
+ if (!check.valid) { console.error(chalk.red(check.message)); return; }
166
+ const spinner = ora(`Fetching issue ${issueKey}...`).start();
167
+ try {
168
+ const issue = await api.get(`/issue/${issueKey}`);
169
+ spinner.stop();
170
+
171
+ if (options.output === 'json') {
172
+ console.log(JSON.stringify({
173
+ key: issue.key, summary: issue.fields.summary,
174
+ status: issue.fields.status?.name, priority: issue.fields.priority?.name,
175
+ assignee: issue.fields.assignee?.displayName || null,
176
+ type: issue.fields.issuetype?.name,
177
+ description: parseADF(issue.fields.description) || null,
178
+ created: issue.fields.created, updated: issue.fields.updated
179
+ }, null, 2));
180
+ return;
181
+ }
182
+
183
+ console.log(chalk.bold(`\n${issue.key}: ${issue.fields.summary}`));
184
+ console.log(chalk.grey(`${issue.fields.issuetype.name} - ${issue.fields.status.name} - ${issue.fields.priority ? issue.fields.priority.name : 'No Priority'}`));
185
+ console.log(chalk.bold('\nDescription:'));
186
+ console.log(parseADF(issue.fields.description) || 'No description provided.');
187
+
188
+ if (issue.fields.assignee) {
189
+ console.log(chalk.bold('\nAssignee: ') + issue.fields.assignee.displayName);
190
+ }
191
+
192
+ if (issue.fields.comment && issue.fields.comment.comments.length > 0) {
193
+ console.log(chalk.bold('\nComments:'));
194
+ issue.fields.comment.comments.forEach(c => {
195
+ console.log(chalk.cyan(c.author.displayName) + ': ' + c.body);
196
+ });
197
+ }
198
+ console.log('');
199
+ } catch (e) {
200
+ handleCommandError(spinner, e, 'Failed to fetch issue');
201
+ }
202
+ });
203
+
204
+ // ── CREATE ────────────────────────────────────────────────────────
205
+ issueCmd
206
+ .command('create')
207
+ .description('Create a new Jira issue')
208
+ .option('-p, --project <key>', 'Project key')
209
+ .option('-t, --type <type>', 'Issue type (e.g., Bug, Story, Task)')
210
+ .option('-s, --summary <text>', 'Issue summary')
211
+ .option('-d, --description <text>', 'Issue description')
212
+ .option('--priority <name>', 'Priority name (e.g., High, Medium, Low)')
213
+ .option('-a, --assignee <id>', 'Assignee account ID (use "me" for self)')
214
+ .addHelpText('after', `
215
+ Examples:
216
+ $ jira issue create # Interactive wizard
217
+ $ jira issue create -p PROJ -s "Fix login bug" # Quick create
218
+ $ jira issue create -p PROJ -t Bug -s "Crash on save" --priority High
219
+ $ jira issue create -p PROJ -s "New feature" -a me
220
+ `)
221
+ .action(async (options) => {
222
+ try {
223
+ // ── Step 1: Select Project ──────────────────────────
224
+ let projectKey = options.project;
225
+ if (!projectKey) {
226
+ const spinner = ora('Fetching projects...').start();
227
+ const projectData = await api.get('/project/search');
228
+ spinner.stop();
229
+
230
+ if (!projectData.values || projectData.values.length === 0) {
231
+ console.error(chalk.red('No projects found. Check your permissions.'));
232
+ return;
233
+ }
234
+
235
+ const projectChoices = projectData.values.map(p => ({
236
+ name: p.key,
237
+ message: `${p.key} — ${p.name}`
238
+ }));
239
+
240
+ const { selectedProject } = await enquirer.prompt({
241
+ type: 'select',
242
+ name: 'selectedProject',
243
+ message: 'Select Project:',
244
+ choices: projectChoices
245
+ });
246
+ projectKey = selectedProject;
247
+ }
248
+
249
+ // ── Step 2: Select Issue Type ───────────────────────
250
+ let issueTypeName = options.type;
251
+ if (!issueTypeName) {
252
+ const spinner = ora('Fetching issue types...').start();
253
+ let issueTypes = [];
254
+ try {
255
+ // Jira Cloud v3 - createmeta endpoint
256
+ const metaData = await api.get(`/issue/createmeta/${projectKey}/issuetypes`);
257
+ issueTypes = metaData.issueTypes || metaData.values || [];
258
+ } catch (metaErr) {
259
+ // Fallback: use project-level issue types
260
+ try {
261
+ const projectInfo = await api.get(`/project/${projectKey}`);
262
+ issueTypes = projectInfo.issueTypes || [];
263
+ } catch {
264
+ issueTypes = [
265
+ { name: 'Task' }, { name: 'Bug' },
266
+ { name: 'Story' }, { name: 'Epic' }
267
+ ];
268
+ }
269
+ }
270
+ spinner.stop();
271
+
272
+ if (issueTypes.length === 0) {
273
+ issueTypes = [
274
+ { name: 'Task' }, { name: 'Bug' },
275
+ { name: 'Story' }, { name: 'Epic' }
276
+ ];
277
+ }
278
+
279
+ // Filter out sub-tasks if present
280
+ const filteredTypes = issueTypes.filter(t => !t.subtask);
281
+ const typeChoices = (filteredTypes.length > 0 ? filteredTypes : issueTypes)
282
+ .map(t => ({ name: t.name, message: t.name }));
283
+
284
+ const { selectedType } = await enquirer.prompt({
285
+ type: 'select',
286
+ name: 'selectedType',
287
+ message: 'Select Issue Type:',
288
+ choices: typeChoices
289
+ });
290
+ issueTypeName = selectedType;
291
+ }
292
+
293
+ // ── Step 3: Summary (required) ──────────────────────
294
+ let summary = options.summary;
295
+ if (!summary) {
296
+ const { inputSummary } = await enquirer.prompt({
297
+ type: 'input',
298
+ name: 'inputSummary',
299
+ message: 'Summary (required):',
300
+ validate: (val) => val.trim().length > 0 || 'Summary cannot be empty'
301
+ });
302
+ summary = inputSummary;
303
+ }
304
+
305
+ // ── Step 4: Description (optional) ──────────────────
306
+ let description = options.description;
307
+ if (description === undefined) {
308
+ const { inputDescription } = await enquirer.prompt({
309
+ type: 'input',
310
+ name: 'inputDescription',
311
+ message: 'Description (optional, press Enter to skip):'
312
+ });
313
+ description = inputDescription || null;
314
+ }
315
+
316
+ // ── Step 5: Priority ────────────────────────────────
317
+ let priorityName = options.priority;
318
+ if (!priorityName) {
319
+ const spinner = ora('Fetching priorities...').start();
320
+ try {
321
+ const priorities = await api.get('/priority');
322
+ spinner.stop();
323
+
324
+ if (Array.isArray(priorities) && priorities.length > 0) {
325
+ const priorityChoices = priorities.map(p => ({
326
+ name: p.name,
327
+ message: p.name
328
+ }));
329
+
330
+ const { selectedPriority } = await enquirer.prompt({
331
+ type: 'select',
332
+ name: 'selectedPriority',
333
+ message: 'Select Priority:',
334
+ choices: priorityChoices
335
+ });
336
+ priorityName = selectedPriority;
337
+ }
338
+ } catch {
339
+ spinner.stop();
340
+ // Priority endpoint may not be available; skip
341
+ }
342
+ }
343
+
344
+ // ── Step 6: Assignee ────────────────────────────────
345
+ let assigneeId = options.assignee;
346
+ if (!assigneeId) {
347
+ const { assigneeChoice } = await enquirer.prompt({
348
+ type: 'select',
349
+ name: 'assigneeChoice',
350
+ message: 'Assign to:',
351
+ choices: [
352
+ { name: 'me', message: 'Myself' },
353
+ { name: 'unassigned', message: 'Leave Unassigned' },
354
+ { name: 'search', message: 'Search for a user...' }
355
+ ]
356
+ });
357
+
358
+ if (assigneeChoice === 'me') {
359
+ const spinner = ora('Fetching your account...').start();
360
+ try {
361
+ const myself = await api.get('/myself');
362
+ assigneeId = myself.accountId;
363
+ spinner.stop();
364
+ } catch {
365
+ spinner.fail('Could not fetch your account. Leaving unassigned.');
366
+ assigneeId = null;
367
+ }
368
+ } else if (assigneeChoice === 'search') {
369
+ const { searchQuery } = await enquirer.prompt({
370
+ type: 'input',
371
+ name: 'searchQuery',
372
+ message: 'Search user by name or email:'
373
+ });
374
+
375
+ if (searchQuery.trim()) {
376
+ const spinner = ora('Searching users...').start();
377
+ try {
378
+ const users = await api.get(`/user/search?query=${encodeURIComponent(searchQuery)}`);
379
+ spinner.stop();
380
+
381
+ if (Array.isArray(users) && users.length > 0) {
382
+ const userChoices = users.map(u => ({
383
+ name: u.accountId,
384
+ message: `${u.displayName} (${u.emailAddress || u.accountId})`
385
+ }));
386
+
387
+ const { selectedUser } = await enquirer.prompt({
388
+ type: 'select',
389
+ name: 'selectedUser',
390
+ message: 'Select User:',
391
+ choices: userChoices
392
+ });
393
+ assigneeId = selectedUser;
394
+ } else {
395
+ console.log(chalk.yellow('No users found. Leaving unassigned.'));
396
+ assigneeId = null;
397
+ }
398
+ } catch {
399
+ spinner.fail('User search failed. Leaving unassigned.');
400
+ assigneeId = null;
401
+ }
402
+ }
403
+ } else {
404
+ assigneeId = null;
405
+ }
406
+ } else if (assigneeId === 'me') {
407
+ // --assignee me flag: resolve to account ID
408
+ const spinner = ora('Fetching your account...').start();
409
+ try {
410
+ const myself = await api.get('/myself');
411
+ assigneeId = myself.accountId;
412
+ spinner.stop();
413
+ } catch {
414
+ spinner.fail('Could not fetch your account. Leaving unassigned.');
415
+ assigneeId = null;
416
+ }
417
+ }
418
+
419
+ // ── Confirmation ────────────────────────────────────
420
+ console.log(chalk.blue('\n── Issue Summary ──────────────────'));
421
+ console.log(` Project: ${chalk.cyan(projectKey)}`);
422
+ console.log(` Type: ${issueTypeName}`);
423
+ console.log(` Summary: ${summary}`);
424
+ console.log(` Description: ${description || chalk.grey('(none)')}`);
425
+ console.log(` Priority: ${priorityName || chalk.grey('(default)')}`);
426
+ console.log(` Assignee: ${assigneeId || chalk.grey('Unassigned')}`);
427
+ console.log(chalk.blue('──────────────────────────────────\n'));
428
+
429
+ const { confirmed } = await enquirer.prompt({
430
+ type: 'confirm',
431
+ name: 'confirmed',
432
+ message: 'Create this issue?',
433
+ initial: true
434
+ });
435
+
436
+ if (!confirmed) {
437
+ console.log(chalk.yellow('Issue creation cancelled.'));
438
+ return;
439
+ }
440
+
441
+ // ── Build Request Body ──────────────────────────────
442
+ const issueBody = {
443
+ fields: {
444
+ project: { key: projectKey },
445
+ issuetype: { name: issueTypeName },
446
+ summary: summary
447
+ }
448
+ };
449
+
450
+ if (description) {
451
+ issueBody.fields.description = textToADF(description);
452
+ }
453
+
454
+ if (priorityName) {
455
+ issueBody.fields.priority = { name: priorityName };
456
+ }
457
+
458
+ if (assigneeId) {
459
+ issueBody.fields.assignee = { accountId: assigneeId };
460
+ }
461
+
462
+ // ── Create Issue ────────────────────────────────────
463
+ const spinner = ora('Creating issue...').start();
464
+ const result = await api.post('/issue', issueBody);
465
+ spinner.succeed(chalk.green(`Issue created: ${chalk.bold(result.key)}`));
466
+
467
+ console.log(chalk.grey(`View it: jira issue view ${result.key}`));
468
+
469
+ } catch (e) {
470
+ handleCommandError(spinner, e, 'Failed to create issue');
471
+ }
472
+ });
473
+
474
+ // ── TRANSITION ────────────────────────────────────────────────────
475
+ issueCmd
476
+ .command('transition')
477
+ .description('Transition an issue to a new status')
478
+ .argument('<issueKey>', 'Issue Key (e.g., PROJ-123)')
479
+ .option('-s, --status <name>', 'Target status name (skips interactive selection)')
480
+ .addHelpText('after', `
481
+ Examples:
482
+ $ jira issue transition PROJ-123 # Interactive
483
+ $ jira issue transition PROJ-123 --status "In Progress"
484
+ $ jira issue transition PROJ-123 -s Done
485
+ `)
486
+ .action(async (issueKey, options) => {
487
+ const check = validateIssueKey(issueKey);
488
+ if (!check.valid) { console.error(chalk.red(check.message)); return; }
489
+ const spinner = ora(`Fetching transitions for ${issueKey}...`).start();
490
+ try {
491
+ // Fetch current issue to show context
492
+ const issue = await api.get(`/issue/${issueKey}?fields=summary,status`);
493
+ const currentStatus = issue.fields.status.name;
494
+
495
+ // Fetch available transitions
496
+ const transData = await api.get(`/issue/${issueKey}/transitions`);
497
+ spinner.stop();
498
+
499
+ if (!transData.transitions || transData.transitions.length === 0) {
500
+ console.log(chalk.yellow(`No transitions available for ${issueKey} (current status: ${currentStatus}).`));
501
+ return;
502
+ }
503
+
504
+ console.log(chalk.bold(`\n${issue.key}: ${issue.fields.summary}`));
505
+ console.log(chalk.grey(`Current Status: ${currentStatus}\n`));
506
+
507
+ let targetTransition;
508
+
509
+ if (options.status) {
510
+ // Non-interactive: find matching transition
511
+ targetTransition = transData.transitions.find(
512
+ t => t.name.toLowerCase() === options.status.toLowerCase() ||
513
+ t.to.name.toLowerCase() === options.status.toLowerCase()
514
+ );
515
+
516
+ if (!targetTransition) {
517
+ console.error(chalk.red(`Status "${options.status}" is not a valid transition from "${currentStatus}".`));
518
+ console.log(chalk.grey('Available transitions:'));
519
+ transData.transitions.forEach(t => {
520
+ console.log(chalk.grey(` • ${t.name} → ${t.to.name}`));
521
+ });
522
+ return;
523
+ }
524
+ } else {
525
+ // Interactive: show selection
526
+ const transitionChoices = transData.transitions.map(t => ({
527
+ name: t.id,
528
+ message: `${t.name} → ${chalk.cyan(t.to.name)}`
529
+ }));
530
+
531
+ const { selectedTransition } = await enquirer.prompt({
532
+ type: 'select',
533
+ name: 'selectedTransition',
534
+ message: 'Select transition:',
535
+ choices: transitionChoices
536
+ });
537
+
538
+ targetTransition = transData.transitions.find(t => t.id === selectedTransition);
539
+ }
540
+
541
+ // Execute transition
542
+ const execSpinner = ora(`Transitioning to "${targetTransition.to.name}"...`).start();
543
+ await api.post(`/issue/${issueKey}/transitions`, {
544
+ transition: { id: targetTransition.id }
545
+ });
546
+ execSpinner.succeed(chalk.green(`${issueKey} transitioned: ${currentStatus} → ${chalk.bold(targetTransition.to.name)}`));
547
+
548
+ } catch (e) {
549
+ handleCommandError(spinner, e, 'Failed to transition issue');
550
+ }
551
+ });
552
+ // ── ASSIGN ────────────────────────────────────────────────────────
553
+ issueCmd
554
+ .command('assign')
555
+ .description('Assign or reassign an issue')
556
+ .argument('<issueKey>', 'Issue Key (e.g., PROJ-123)')
557
+ .option('-a, --assignee <id>', 'Assignee account ID (use "me" for self, "none" to unassign)')
558
+ .addHelpText('after', `
559
+ Examples:
560
+ $ jira issue assign PROJ-123 # Interactive
561
+ $ jira issue assign PROJ-123 -a me # Assign to yourself
562
+ $ jira issue assign PROJ-123 -a none # Unassign
563
+ `)
564
+ .action(async (issueKey, options) => {
565
+ const check = validateIssueKey(issueKey);
566
+ if (!check.valid) { console.error(chalk.red(check.message)); return; }
567
+ try {
568
+ let assigneeId = options.assignee;
569
+
570
+ if (!assigneeId) {
571
+ // Interactive selection
572
+ const spinner = ora(`Fetching issue ${issueKey}...`).start();
573
+ const issue = await api.get(`/issue/${issueKey}?fields=summary,assignee`);
574
+ spinner.stop();
575
+
576
+ const currentAssignee = issue.fields.assignee?.displayName || 'Unassigned';
577
+ console.log(chalk.bold(`\n${issue.key}: ${issue.fields.summary}`));
578
+ console.log(chalk.grey(`Current Assignee: ${currentAssignee}\n`));
579
+
580
+ const { assignChoice } = await enquirer.prompt({
581
+ type: 'select',
582
+ name: 'assignChoice',
583
+ message: 'Assign to:',
584
+ choices: [
585
+ { name: 'me', message: 'Myself' },
586
+ { name: 'none', message: 'Unassign' },
587
+ { name: 'search', message: 'Search for a user...' }
588
+ ]
589
+ });
590
+ assigneeId = assignChoice;
591
+ }
592
+
593
+ if (assigneeId === 'me') {
594
+ const spinner = ora('Fetching your account...').start();
595
+ const myself = await api.get('/myself');
596
+ assigneeId = myself.accountId;
597
+ spinner.stop();
598
+ }
599
+
600
+ if (assigneeId === 'search') {
601
+ const { searchQuery } = await enquirer.prompt({
602
+ type: 'input',
603
+ name: 'searchQuery',
604
+ message: 'Search user by name or email:'
605
+ });
606
+
607
+ const spinner = ora('Searching users...').start();
608
+ const users = await api.get(`/user/search?query=${encodeURIComponent(searchQuery)}`);
609
+ spinner.stop();
610
+
611
+ if (!Array.isArray(users) || users.length === 0) {
612
+ console.log(chalk.yellow('No users found.'));
613
+ return;
614
+ }
615
+
616
+ const { selectedUser } = await enquirer.prompt({
617
+ type: 'select',
618
+ name: 'selectedUser',
619
+ message: 'Select User:',
620
+ choices: users.map(u => ({
621
+ name: u.accountId,
622
+ message: `${u.displayName} (${u.emailAddress || u.accountId})`
623
+ }))
624
+ });
625
+ assigneeId = selectedUser;
626
+ }
627
+
628
+ const spinner = ora('Updating assignee...').start();
629
+ const body = assigneeId === 'none'
630
+ ? { accountId: null }
631
+ : { accountId: assigneeId };
632
+
633
+ await api.put(`/issue/${issueKey}/assignee`, body);
634
+ spinner.succeed(chalk.green(`${issueKey} ${assigneeId === 'none' ? 'unassigned' : 'assigned'} successfully.`));
635
+
636
+ } catch (e) {
637
+ handleCommandError(spinner, e, 'Failed to assign issue');
638
+ }
639
+ });
640
+
641
+ // ── COMMENT ───────────────────────────────────────────────────────
642
+ issueCmd
643
+ .command('comment')
644
+ .description('Add a comment to an issue')
645
+ .argument('<issueKey>', 'Issue Key (e.g., PROJ-123)')
646
+ .option('-m, --message <text>', 'Comment text (skips interactive prompt)')
647
+ .addHelpText('after', `
648
+ Examples:
649
+ $ jira issue comment PROJ-123 # Interactive
650
+ $ jira issue comment PROJ-123 -m "Fixed in latest build"
651
+ `)
652
+ .action(async (issueKey, options) => {
653
+ const check = validateIssueKey(issueKey);
654
+ if (!check.valid) { console.error(chalk.red(check.message)); return; }
655
+ try {
656
+ let commentText = options.message;
657
+
658
+ if (!commentText) {
659
+ // Show issue context first
660
+ const spinner = ora(`Fetching issue ${issueKey}...`).start();
661
+ const issue = await api.get(`/issue/${issueKey}?fields=summary,status`);
662
+ spinner.stop();
663
+
664
+ console.log(chalk.bold(`\n${issue.key}: ${issue.fields.summary}`));
665
+ console.log(chalk.grey(`Status: ${issue.fields.status.name}\n`));
666
+
667
+ const { inputComment } = await enquirer.prompt({
668
+ type: 'input',
669
+ name: 'inputComment',
670
+ message: 'Enter your comment:',
671
+ validate: (val) => val.trim().length > 0 || 'Comment cannot be empty'
672
+ });
673
+ commentText = inputComment;
674
+ }
675
+
676
+ const spinner = ora('Adding comment...').start();
677
+ await api.post(`/issue/${issueKey}/comment`, {
678
+ body: textToADF(commentText)
679
+ });
680
+ spinner.succeed(chalk.green(`Comment added to ${issueKey}.`));
681
+
682
+ } catch (e) {
683
+ handleCommandError(spinner, e, 'Failed to add comment');
684
+ }
685
+ });
686
+
687
+ // ── EDIT ──────────────────────────────────────────────────────────
688
+ issueCmd
689
+ .command('edit')
690
+ .description('Edit issue fields')
691
+ .argument('<issueKey>', 'Issue Key (e.g., PROJ-123)')
692
+ .option('-s, --summary <text>', 'New summary')
693
+ .option('-d, --description <text>', 'New description')
694
+ .option('--priority <name>', 'New priority')
695
+ .addHelpText('after', `
696
+ Examples:
697
+ $ jira issue edit PROJ-123 # Interactive field picker
698
+ $ jira issue edit PROJ-123 -s "Updated title"
699
+ $ jira issue edit PROJ-123 --priority High
700
+ $ jira issue edit PROJ-123 -d "New description"
701
+ `)
702
+ .action(async (issueKey, options) => {
703
+ const check = validateIssueKey(issueKey);
704
+ if (!check.valid) { console.error(chalk.red(check.message)); return; }
705
+ const spinner = ora(`Fetching issue ${issueKey}...`).start();
706
+ try {
707
+ const issue = await api.get(`/issue/${issueKey}?fields=summary,description,priority`);
708
+ spinner.stop();
709
+
710
+ const updateBody = { fields: {} };
711
+ const hasFlags = options.summary || options.description || options.priority;
712
+
713
+ if (hasFlags) {
714
+ if (options.summary) updateBody.fields.summary = options.summary;
715
+ if (options.description) updateBody.fields.description = textToADF(options.description);
716
+ if (options.priority) updateBody.fields.priority = { name: options.priority };
717
+ } else {
718
+ // Interactive: pick which fields to edit
719
+ console.log(chalk.bold(`\nEditing ${chalk.cyan(issueKey)}: ${issue.fields.summary}\n`));
720
+
721
+ const { Select, Input } = enquirer;
722
+
723
+ const fieldSelect = new Select({
724
+ name: 'fields',
725
+ message: 'Select fields to edit',
726
+ choices: [
727
+ { name: 'summary', message: `Summary: ${issue.fields.summary}` },
728
+ { name: 'description', message: 'Description' },
729
+ { name: 'priority', message: `Priority: ${issue.fields.priority?.name || 'None'}` }
730
+ ],
731
+ multiple: true
732
+ });
733
+ const selectedFields = await fieldSelect.run();
734
+
735
+ if (!selectedFields || selectedFields.length === 0) {
736
+ console.log(chalk.yellow('No fields selected.'));
737
+ return;
738
+ }
739
+
740
+ for (const field of selectedFields) {
741
+ if (field === 'summary') {
742
+ const prompt = new Input({ message: 'New summary', initial: issue.fields.summary });
743
+ updateBody.fields.summary = await prompt.run();
744
+ }
745
+ if (field === 'description') {
746
+ const prompt = new Input({ message: 'New description' });
747
+ const desc = await prompt.run();
748
+ if (desc) updateBody.fields.description = textToADF(desc);
749
+ }
750
+ if (field === 'priority') {
751
+ const priorities = await api.get('/priority');
752
+ const prioSelect = new Select({
753
+ name: 'priority',
754
+ message: 'Select priority',
755
+ choices: priorities.map(p => ({ name: p.name, message: p.name }))
756
+ });
757
+ updateBody.fields.priority = { name: await prioSelect.run() };
758
+ }
759
+ }
760
+ }
761
+
762
+ if (Object.keys(updateBody.fields).length === 0) {
763
+ console.log(chalk.yellow('No changes specified.'));
764
+ return;
765
+ }
766
+
767
+ const updateSpinner = ora('Updating issue...').start();
768
+ await api.put(`/issue/${issueKey}`, updateBody);
769
+ updateSpinner.succeed(`${chalk.cyan(issueKey)} updated successfully`);
770
+
771
+ } catch (e) {
772
+ handleCommandError(spinner, e, `Failed to edit ${issueKey}`);
773
+ }
774
+ });
775
+
776
+ // ── SEARCH ────────────────────────────────────────────────────────
777
+ issueCmd
778
+ .command('search')
779
+ .description('Quick text search across issues')
780
+ .argument('<query>', 'Search text')
781
+ .option('-p, --project <key>', 'Filter by project')
782
+ .option('-l, --limit <n>', 'Max results', '15')
783
+ .option('-o, --output <format>', 'Output format (json)')
784
+ .addHelpText('after', `
785
+ Examples:
786
+ $ jira issue search "login bug"
787
+ $ jira issue search "payment" -p PROJ
788
+ $ jira issue search "crash" --output json
789
+ `)
790
+ .action(async (query, options) => {
791
+ const spinner = ora(`Searching for "${query}"...`).start();
792
+ try {
793
+ const jqlParts = [`text ~ "${query.replace(/"/g, '\\"')}"`];
794
+ if (options.project) jqlParts.push(`project = "${options.project}"`);
795
+ const jql = jqlParts.join(' AND ') + ' ORDER BY updated DESC';
796
+
797
+ const data = await api.post('/search/jql', {
798
+ jql,
799
+ maxResults: parseInt(options.limit),
800
+ fields: ['summary', 'status', 'assignee', 'updated']
801
+ });
802
+ spinner.stop();
803
+
804
+ if (!data.issues || data.issues.length === 0) {
805
+ console.log(chalk.yellow('No issues found.'));
806
+ return;
807
+ }
808
+
809
+ if (options.output === 'json') {
810
+ console.log(JSON.stringify(data.issues.map(i => ({
811
+ key: i.key, summary: i.fields.summary,
812
+ status: i.fields.status?.name, assignee: i.fields.assignee?.displayName || null,
813
+ updated: i.fields.updated
814
+ })), null, 2));
815
+ return;
816
+ }
817
+
818
+ const tableData = [
819
+ [chalk.bold('Key'), chalk.bold('Summary'), chalk.bold('Status'), chalk.bold('Assignee')]
820
+ ];
821
+ data.issues.forEach(i => {
822
+ tableData.push([
823
+ chalk.cyan(i.key),
824
+ i.fields.summary ? (i.fields.summary.length > 55 ? i.fields.summary.substring(0, 52) + '...' : i.fields.summary) : '',
825
+ i.fields.status?.name || '',
826
+ i.fields.assignee?.displayName || 'Unassigned'
827
+ ]);
828
+ });
829
+ console.log(table(tableData));
830
+ console.log(chalk.grey(`Found ${data.issues.length} result(s)`));
831
+
832
+ } catch (e) {
833
+ handleCommandError(spinner, e, 'Search failed');
834
+ }
835
+ });
836
+
837
+ // ── LINK ──────────────────────────────────────────────────────────
838
+ issueCmd
839
+ .command('link')
840
+ .description('Link two issues together')
841
+ .argument('<sourceKey>', 'Source issue key')
842
+ .argument('<targetKey>', 'Target issue key')
843
+ .option('-t, --type <name>', 'Link type (e.g., "Blocks", "Relates")')
844
+ .addHelpText('after', `
845
+ Examples:
846
+ $ jira issue link PROJ-1 PROJ-2 # Interactive type selection
847
+ $ jira issue link PROJ-1 PROJ-2 -t "Blocks"
848
+ $ jira issue link PROJ-1 PROJ-2 -t "Relates"
849
+ `)
850
+ .action(async (sourceKey, targetKey, options) => {
851
+ const srcCheck = validateIssueKey(sourceKey);
852
+ if (!srcCheck.valid) { console.error(chalk.red(srcCheck.message)); return; }
853
+ const tgtCheck = validateIssueKey(targetKey);
854
+ if (!tgtCheck.valid) { console.error(chalk.red(tgtCheck.message)); return; }
855
+
856
+ try {
857
+ let linkType = options.type;
858
+
859
+ if (!linkType) {
860
+ const spinner = ora('Fetching link types...').start();
861
+ const linkTypes = await api.get('/issueLinkType');
862
+ spinner.stop();
863
+
864
+ const { Select } = enquirer;
865
+ const typeSelect = new Select({
866
+ name: 'linkType',
867
+ message: `Link type: ${chalk.cyan(sourceKey)} → ${chalk.cyan(targetKey)}`,
868
+ choices: linkTypes.issueLinkTypes.map(lt => ({
869
+ name: lt.name,
870
+ message: `${lt.name} (${lt.inward} / ${lt.outward})`
871
+ }))
872
+ });
873
+ linkType = await typeSelect.run();
874
+ }
875
+
876
+ const spinner = ora(`Linking ${sourceKey} → ${targetKey}...`).start();
877
+ await api.post('/issueLink', {
878
+ type: { name: linkType },
879
+ inwardIssue: { key: sourceKey },
880
+ outwardIssue: { key: targetKey }
881
+ });
882
+ spinner.succeed(`Linked ${chalk.cyan(sourceKey)} ${chalk.grey(`—[${linkType}]→`)} ${chalk.cyan(targetKey)}`);
883
+
884
+ } catch (e) {
885
+ handleCommandError(null, e, `Failed to link issues`);
886
+ }
887
+ });
888
+
889
+ // ── WATCH ─────────────────────────────────────────────────────────
890
+ issueCmd
891
+ .command('watch')
892
+ .description('Start watching an issue')
893
+ .argument('<issueKey>', 'Issue Key')
894
+ .action(async (issueKey) => {
895
+ const check = validateIssueKey(issueKey);
896
+ if (!check.valid) { console.error(chalk.red(check.message)); return; }
897
+ const spinner = ora(`Watching ${issueKey}...`).start();
898
+ try {
899
+ await api.post(`/issue/${issueKey}/watchers`, null);
900
+ spinner.succeed(`Now watching ${chalk.cyan(issueKey)}`);
901
+ } catch (e) {
902
+ handleCommandError(spinner, e, `Failed to watch ${issueKey}`);
903
+ }
904
+ });
905
+
906
+ // ── UNWATCH ───────────────────────────────────────────────────────
907
+ issueCmd
908
+ .command('unwatch')
909
+ .description('Stop watching an issue')
910
+ .argument('<issueKey>', 'Issue Key')
911
+ .action(async (issueKey) => {
912
+ const check = validateIssueKey(issueKey);
913
+ if (!check.valid) { console.error(chalk.red(check.message)); return; }
914
+ const spinner = ora(`Unwatching ${issueKey}...`).start();
915
+ try {
916
+ const me = await api.get('/myself');
917
+ await api.delete(`/issue/${issueKey}/watchers?accountId=${me.accountId}`);
918
+ spinner.succeed(`Stopped watching ${chalk.cyan(issueKey)}`);
919
+ } catch (e) {
920
+ handleCommandError(spinner, e, `Failed to unwatch ${issueKey}`);
921
+ }
922
+ });
923
+
924
+ program.addCommand(issueCmd);
925
+ }