claude-autopm 2.4.0 → 2.6.0

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.
@@ -0,0 +1,591 @@
1
+ /**
2
+ * IssueService - Issue Management Service
3
+ *
4
+ * Pure service layer for issue operations following ClaudeAutoPM patterns.
5
+ * Follows 3-layer architecture: Service (logic) -> Provider (I/O) -> CLI (presentation)
6
+ *
7
+ * Provides comprehensive issue lifecycle management:
8
+ *
9
+ * 1. Issue Metadata & Parsing (4 methods):
10
+ * - parseIssueMetadata: Parse YAML frontmatter from issue content
11
+ * - getLocalIssue: Read local issue file with metadata
12
+ * - getIssueStatus: Get current status of an issue
13
+ * - validateIssue: Validate issue structure and required fields
14
+ *
15
+ * 2. Issue Lifecycle Management (2 methods):
16
+ * - updateIssueStatus: Update status with automatic timestamps
17
+ * - listIssues: List all issues with optional filtering
18
+ *
19
+ * 3. Issue Relationships (3 methods):
20
+ * - getIssueFiles: Find all files related to an issue
21
+ * - getSubIssues: Get child issues
22
+ * - getDependencies: Get blocking issues
23
+ *
24
+ * 4. Provider Integration (2 methods):
25
+ * - syncIssueToProvider: Push local changes to GitHub/Azure
26
+ * - syncIssueFromProvider: Pull updates from provider
27
+ *
28
+ * 5. Utility Methods (4 methods):
29
+ * - categorizeStatus: Categorize status into standard buckets
30
+ * - isIssueClosed: Check if issue is closed
31
+ * - getIssuePath: Get file path for issue
32
+ * - formatIssueDuration: Format time duration
33
+ *
34
+ * Documentation Queries:
35
+ * - GitHub Issues API v3 best practices (2025)
36
+ * - Azure DevOps work items REST API patterns
37
+ * - Agile issue tracking workflow best practices
38
+ * - mcp://context7/project-management/issue-tracking - Issue lifecycle management
39
+ * - mcp://context7/markdown/frontmatter - YAML frontmatter patterns
40
+ */
41
+
42
+ class IssueService {
43
+ /**
44
+ * Create a new IssueService instance
45
+ *
46
+ * @param {Object} options - Configuration options
47
+ * @param {Object} options.provider - Provider instance for GitHub/Azure (optional)
48
+ * @param {string} [options.issuesDir] - Path to issues directory (default: .claude/issues)
49
+ * @param {string} [options.defaultStatus] - Default issue status (default: open)
50
+ */
51
+ constructor(options = {}) {
52
+ // Provider for GitHub/Azure integration (optional)
53
+ this.provider = options.provider || null;
54
+
55
+ // CLI operation options
56
+ this.options = {
57
+ issuesDir: options.issuesDir || '.claude/issues',
58
+ defaultStatus: options.defaultStatus || 'open',
59
+ ...options
60
+ };
61
+ }
62
+
63
+ // ==========================================
64
+ // 1. ISSUE METADATA & PARSING (4 METHODS)
65
+ // ==========================================
66
+
67
+ /**
68
+ * Parse YAML frontmatter from issue content
69
+ *
70
+ * Extracts key-value pairs from YAML frontmatter block.
71
+ * Returns null if frontmatter is missing or malformed.
72
+ *
73
+ * @param {string} content - Issue markdown content with frontmatter
74
+ * @returns {Object|null} Parsed frontmatter object or null
75
+ *
76
+ * @example
77
+ * parseIssueMetadata(`---
78
+ * id: 123
79
+ * title: Fix bug
80
+ * status: open
81
+ * ---
82
+ * # Issue Details`)
83
+ * // Returns: { id: '123', title: 'Fix bug', status: 'open' }
84
+ */
85
+ parseIssueMetadata(content) {
86
+ if (!content || typeof content !== 'string') {
87
+ return null;
88
+ }
89
+
90
+ // Match frontmatter block: ---\n...\n--- or ---\n---
91
+ const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---/) ||
92
+ content.match(/^---\n---/);
93
+
94
+ if (!frontmatterMatch) {
95
+ return null;
96
+ }
97
+
98
+ // Empty frontmatter (---\n---)
99
+ if (!frontmatterMatch[1] && content.startsWith('---\n---')) {
100
+ return {};
101
+ }
102
+
103
+ const metadata = {};
104
+ const lines = (frontmatterMatch[1] || '').split('\n');
105
+
106
+ for (const line of lines) {
107
+ // Skip empty lines
108
+ if (!line.trim()) {
109
+ continue;
110
+ }
111
+
112
+ // Find first colon to split key and value
113
+ const colonIndex = line.indexOf(':');
114
+ if (colonIndex === -1) {
115
+ continue; // Skip lines without colons
116
+ }
117
+
118
+ const key = line.substring(0, colonIndex).trim();
119
+ const value = line.substring(colonIndex + 1).trim();
120
+
121
+ if (key) {
122
+ metadata[key] = value;
123
+ }
124
+ }
125
+
126
+ return metadata;
127
+ }
128
+
129
+ /**
130
+ * Read local issue file with metadata
131
+ *
132
+ * @param {number|string} issueNumber - Issue number
133
+ * @returns {Promise<Object>} Issue data with metadata and content
134
+ * @throws {Error} If issue not found
135
+ *
136
+ * @example
137
+ * await getLocalIssue(123)
138
+ * // Returns: { id: '123', title: '...', status: '...', content: '...' }
139
+ */
140
+ async getLocalIssue(issueNumber) {
141
+ const fs = require('fs-extra');
142
+
143
+ const issuePath = this.getIssuePath(issueNumber);
144
+
145
+ // Check if issue exists
146
+ const exists = await fs.pathExists(issuePath);
147
+ if (!exists) {
148
+ throw new Error(`Issue not found: ${issueNumber}`);
149
+ }
150
+
151
+ // Read issue content
152
+ const content = await fs.readFile(issuePath, 'utf8');
153
+ const metadata = this.parseIssueMetadata(content);
154
+
155
+ return {
156
+ ...metadata,
157
+ content,
158
+ path: issuePath
159
+ };
160
+ }
161
+
162
+ /**
163
+ * Get current status of an issue
164
+ *
165
+ * @param {number|string} issueNumber - Issue number
166
+ * @returns {Promise<string>} Current status
167
+ * @throws {Error} If issue not found
168
+ */
169
+ async getIssueStatus(issueNumber) {
170
+ try {
171
+ const issue = await this.getLocalIssue(issueNumber);
172
+ return issue.status || this.options.defaultStatus;
173
+ } catch (error) {
174
+ if (error.message.includes('not found')) {
175
+ throw new Error(`Issue not found: ${issueNumber}`);
176
+ }
177
+ throw error;
178
+ }
179
+ }
180
+
181
+ /**
182
+ * Validate issue structure and completeness
183
+ *
184
+ * @param {number|string} issueNumber - Issue number
185
+ * @returns {Promise<Object>} Validation result: { valid: boolean, issues: string[] }
186
+ * @throws {Error} If issue not found
187
+ */
188
+ async validateIssue(issueNumber) {
189
+ const fs = require('fs-extra');
190
+
191
+ const issuePath = this.getIssuePath(issueNumber);
192
+
193
+ // Check if issue exists
194
+ const exists = await fs.pathExists(issuePath);
195
+ if (!exists) {
196
+ throw new Error(`Issue not found: ${issueNumber}`);
197
+ }
198
+
199
+ const issues = [];
200
+
201
+ // Read issue content
202
+ const content = await fs.readFile(issuePath, 'utf8');
203
+
204
+ // Check for frontmatter
205
+ const metadata = this.parseIssueMetadata(content);
206
+ if (!metadata) {
207
+ issues.push('Missing frontmatter');
208
+ } else {
209
+ // Check for required fields
210
+ if (!metadata.id) {
211
+ issues.push('Missing id in frontmatter');
212
+ }
213
+ if (!metadata.title) {
214
+ issues.push('Missing title in frontmatter');
215
+ }
216
+ }
217
+
218
+ return {
219
+ valid: issues.length === 0,
220
+ issues
221
+ };
222
+ }
223
+
224
+ // ==========================================
225
+ // 2. ISSUE LIFECYCLE MANAGEMENT (2 METHODS)
226
+ // ==========================================
227
+
228
+ /**
229
+ * Update issue status with automatic timestamps
230
+ *
231
+ * @param {number|string} issueNumber - Issue number
232
+ * @param {string} newStatus - New status to set
233
+ * @returns {Promise<void>}
234
+ * @throws {Error} If issue not found
235
+ */
236
+ async updateIssueStatus(issueNumber, newStatus) {
237
+ const fs = require('fs-extra');
238
+
239
+ const issuePath = this.getIssuePath(issueNumber);
240
+
241
+ // Check if issue exists
242
+ const exists = await fs.pathExists(issuePath);
243
+ if (!exists) {
244
+ throw new Error(`Issue not found: ${issueNumber}`);
245
+ }
246
+
247
+ // Read current content
248
+ let content = await fs.readFile(issuePath, 'utf8');
249
+
250
+ // Update status
251
+ content = content.replace(/^status:\s*.+$/m, `status: ${newStatus}`);
252
+
253
+ const now = new Date().toISOString();
254
+
255
+ // Add started timestamp when moving to in-progress
256
+ if (newStatus === 'in-progress' && !content.includes('started:')) {
257
+ // Try to add after created: field, or after status: if no created: field
258
+ if (content.includes('created:')) {
259
+ content = content.replace(/^(created:.+)$/m, `$1\nstarted: ${now}`);
260
+ } else {
261
+ content = content.replace(/^(status:.+)$/m, `$1\nstarted: ${now}`);
262
+ }
263
+ }
264
+
265
+ // Add completed timestamp when closing
266
+ if (['closed', 'completed', 'done', 'resolved'].includes(newStatus.toLowerCase()) &&
267
+ !content.includes('completed:')) {
268
+ // Try to add after created: field, or after status: if no created: field
269
+ if (content.includes('created:')) {
270
+ content = content.replace(/^(created:.+)$/m, `$1\ncompleted: ${now}`);
271
+ } else {
272
+ content = content.replace(/^(status:.+)$/m, `$1\ncompleted: ${now}`);
273
+ }
274
+ }
275
+
276
+ // Write updated content
277
+ await fs.writeFile(issuePath, content);
278
+ }
279
+
280
+ /**
281
+ * List all issues with optional filtering
282
+ *
283
+ * @param {Object} [options] - Filter options
284
+ * @param {string} [options.status] - Filter by status
285
+ * @returns {Promise<Array<Object>>} Array of issue objects with metadata
286
+ */
287
+ async listIssues(options = {}) {
288
+ const fs = require('fs-extra');
289
+ const path = require('path');
290
+
291
+ const issuesDir = path.join(process.cwd(), this.options.issuesDir);
292
+
293
+ // Check if issues directory exists
294
+ const dirExists = await fs.pathExists(issuesDir);
295
+ if (!dirExists) {
296
+ return [];
297
+ }
298
+
299
+ // Read all issue files
300
+ let files;
301
+ try {
302
+ files = await fs.readdir(issuesDir);
303
+ // Only process .md files with numeric names
304
+ files = files.filter(file => /^\d+\.md$/.test(file));
305
+ } catch (error) {
306
+ return [];
307
+ }
308
+
309
+ const issues = [];
310
+
311
+ for (const file of files) {
312
+ const filePath = path.join(issuesDir, file);
313
+
314
+ try {
315
+ const content = await fs.readFile(filePath, 'utf8');
316
+ const metadata = this.parseIssueMetadata(content);
317
+
318
+ if (metadata) {
319
+ // Apply default status if missing
320
+ metadata.status = metadata.status || this.options.defaultStatus;
321
+
322
+ // Filter by status if specified
323
+ if (options.status && metadata.status !== options.status) {
324
+ continue;
325
+ }
326
+
327
+ issues.push({
328
+ ...metadata,
329
+ path: filePath
330
+ });
331
+ }
332
+ } catch (error) {
333
+ // Skip files that can't be read
334
+ continue;
335
+ }
336
+ }
337
+
338
+ return issues;
339
+ }
340
+
341
+ // ==========================================
342
+ // 3. ISSUE RELATIONSHIPS (3 METHODS)
343
+ // ==========================================
344
+
345
+ /**
346
+ * Find all files related to an issue
347
+ *
348
+ * @param {number|string} issueNumber - Issue number
349
+ * @returns {Promise<Array<string>>} Array of related file names
350
+ */
351
+ async getIssueFiles(issueNumber) {
352
+ const fs = require('fs-extra');
353
+ const path = require('path');
354
+
355
+ const issuesDir = path.join(process.cwd(), this.options.issuesDir);
356
+
357
+ // Check if issues directory exists
358
+ const dirExists = await fs.pathExists(issuesDir);
359
+ if (!dirExists) {
360
+ return [];
361
+ }
362
+
363
+ // Read all files
364
+ try {
365
+ const files = await fs.readdir(issuesDir);
366
+
367
+ // Filter files that start with issue number
368
+ const issueStr = String(issueNumber);
369
+ const relatedFiles = files.filter(file => {
370
+ return file.startsWith(`${issueStr}.md`) ||
371
+ file.startsWith(`${issueStr}-`);
372
+ });
373
+
374
+ return relatedFiles;
375
+ } catch (error) {
376
+ return [];
377
+ }
378
+ }
379
+
380
+ /**
381
+ * Get child issues of a parent issue
382
+ *
383
+ * @param {number|string} issueNumber - Parent issue number
384
+ * @returns {Promise<Array<string>>} Array of child issue numbers
385
+ */
386
+ async getSubIssues(issueNumber) {
387
+ try {
388
+ const issue = await this.getLocalIssue(issueNumber);
389
+ const children = issue.children;
390
+
391
+ if (!children) {
392
+ return [];
393
+ }
394
+
395
+ // Parse children (can be array [101, 102] or string "101, 102")
396
+ if (typeof children === 'string') {
397
+ // Remove brackets and split by comma
398
+ const cleaned = children.replace(/[\[\]]/g, '');
399
+ return cleaned.split(',').map(c => c.trim()).filter(c => c);
400
+ }
401
+
402
+ return [];
403
+ } catch (error) {
404
+ return [];
405
+ }
406
+ }
407
+
408
+ /**
409
+ * Get blocking issues (dependencies)
410
+ *
411
+ * @param {number|string} issueNumber - Issue number
412
+ * @returns {Promise<Array<string>>} Array of blocking issue numbers
413
+ */
414
+ async getDependencies(issueNumber) {
415
+ try {
416
+ const issue = await this.getLocalIssue(issueNumber);
417
+ const dependencies = issue.dependencies || issue.blocked_by;
418
+
419
+ if (!dependencies) {
420
+ return [];
421
+ }
422
+
423
+ // Parse dependencies (can be array [120, 121] or string "120, 121")
424
+ if (typeof dependencies === 'string') {
425
+ // Remove brackets and split by comma
426
+ const cleaned = dependencies.replace(/[\[\]]/g, '');
427
+ return cleaned.split(',').map(d => d.trim()).filter(d => d);
428
+ }
429
+
430
+ return [];
431
+ } catch (error) {
432
+ return [];
433
+ }
434
+ }
435
+
436
+ // ==========================================
437
+ // 4. PROVIDER INTEGRATION (2 METHODS)
438
+ // ==========================================
439
+
440
+ /**
441
+ * Push local issue changes to GitHub/Azure
442
+ *
443
+ * @param {number|string} issueNumber - Issue number
444
+ * @returns {Promise<Object>} Sync result
445
+ * @throws {Error} If no provider configured
446
+ */
447
+ async syncIssueToProvider(issueNumber) {
448
+ if (!this.provider || !this.provider.updateIssue) {
449
+ throw new Error('No provider configured for syncing');
450
+ }
451
+
452
+ // Read local issue
453
+ const issue = await this.getLocalIssue(issueNumber);
454
+
455
+ // Push to provider
456
+ const result = await this.provider.updateIssue(issue);
457
+
458
+ return result;
459
+ }
460
+
461
+ /**
462
+ * Pull issue updates from GitHub/Azure
463
+ *
464
+ * @param {number|string} issueNumber - Issue number
465
+ * @returns {Promise<Object>} Updated issue data
466
+ * @throws {Error} If no provider configured
467
+ */
468
+ async syncIssueFromProvider(issueNumber) {
469
+ const fs = require('fs-extra');
470
+
471
+ if (!this.provider || !this.provider.getIssue) {
472
+ throw new Error('No provider configured for syncing');
473
+ }
474
+
475
+ // Fetch from provider
476
+ const issueData = await this.provider.getIssue(String(issueNumber));
477
+
478
+ // Build issue content with frontmatter
479
+ const frontmatter = `---
480
+ id: ${issueData.id}
481
+ title: ${issueData.title}
482
+ status: ${issueData.status}
483
+ ${issueData.assignees && issueData.assignees.length > 0 ? `assignee: ${issueData.assignees[0]}` : ''}
484
+ ${issueData.labels && issueData.labels.length > 0 ? `labels: ${issueData.labels.join(', ')}` : ''}
485
+ created: ${issueData.createdAt}
486
+ updated: ${issueData.updatedAt}
487
+ ${issueData.url ? `url: ${issueData.url}` : ''}
488
+ ---
489
+
490
+ # ${issueData.title}
491
+
492
+ ${issueData.description || ''}
493
+ `;
494
+
495
+ // Write to local file
496
+ const issuePath = this.getIssuePath(issueNumber);
497
+ await fs.writeFile(issuePath, frontmatter);
498
+
499
+ return issueData;
500
+ }
501
+
502
+ // ==========================================
503
+ // 5. UTILITY METHODS (4 METHODS)
504
+ // ==========================================
505
+
506
+ /**
507
+ * Categorize issue status into standard buckets
508
+ *
509
+ * Maps various status strings to standardized categories:
510
+ * - open: Not started, awaiting work
511
+ * - in_progress: Active development
512
+ * - closed: Completed/resolved
513
+ *
514
+ * @param {string} status - Raw status string
515
+ * @returns {string} Categorized status (open|in_progress|closed)
516
+ */
517
+ categorizeStatus(status) {
518
+ const lowerStatus = (status || '').toLowerCase();
519
+
520
+ // Open statuses
521
+ if (['open', 'todo', 'new', ''].includes(lowerStatus)) {
522
+ return 'open';
523
+ }
524
+
525
+ // In-progress statuses
526
+ if (['in-progress', 'in_progress', 'active', 'started'].includes(lowerStatus)) {
527
+ return 'in_progress';
528
+ }
529
+
530
+ // Closed statuses
531
+ if (['closed', 'completed', 'done', 'resolved'].includes(lowerStatus)) {
532
+ return 'closed';
533
+ }
534
+
535
+ // Default to open for unknown statuses
536
+ return 'open';
537
+ }
538
+
539
+ /**
540
+ * Check if issue is in closed/completed state
541
+ *
542
+ * @param {Object} issue - Issue object with status field
543
+ * @returns {boolean} True if issue is closed
544
+ */
545
+ isIssueClosed(issue) {
546
+ if (!issue || !issue.status) {
547
+ return false;
548
+ }
549
+
550
+ const status = (issue.status || '').toLowerCase();
551
+ return ['closed', 'completed', 'done', 'resolved'].includes(status);
552
+ }
553
+
554
+ /**
555
+ * Get file path for issue
556
+ *
557
+ * @param {number|string} issueNumber - Issue number
558
+ * @returns {string} Full path to issue file
559
+ */
560
+ getIssuePath(issueNumber) {
561
+ const path = require('path');
562
+ return path.join(process.cwd(), this.options.issuesDir, `${issueNumber}.md`);
563
+ }
564
+
565
+ /**
566
+ * Format time duration between two timestamps
567
+ *
568
+ * @param {string} startTime - ISO timestamp for start
569
+ * @param {string} [endTime] - ISO timestamp for end (defaults to now)
570
+ * @returns {string} Formatted duration (e.g., "2h 30m", "3 days 4h")
571
+ */
572
+ formatIssueDuration(startTime, endTime = null) {
573
+ const start = new Date(startTime);
574
+ const end = endTime ? new Date(endTime) : new Date();
575
+ const diff = end - start;
576
+
577
+ const days = Math.floor(diff / (1000 * 60 * 60 * 24));
578
+ const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
579
+ const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
580
+
581
+ if (days > 0) {
582
+ return `${days} day${days > 1 ? 's' : ''} ${hours}h`;
583
+ } else if (hours > 0) {
584
+ return `${hours}h ${minutes}m`;
585
+ } else {
586
+ return `${minutes} minute${minutes > 1 ? 's' : ''}`;
587
+ }
588
+ }
589
+ }
590
+
591
+ module.exports = IssueService;