Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 3x 3x | import { BaseAdapter } from './BaseAdapter.js'; import { IProviderAdapter, ProviderConfig } from './IProviderAdapter.js'; import { WorkItem, CreateWorkItemData, UpdateWorkItemData, WorkItemFilter, ProviderCapabilities, WorkItemType, Priority, User, Milestone, GitHubSpecificFields, } from '../types/index.js'; interface GitHubIssue { id: number; number: number; title: string; body?: string; state: 'open' | 'closed'; state_reason?: 'completed' | 'not_planned' | 'reopened'; user: { id: number; login: string; avatar_url: string; type: string; }; assignees: Array<{ id: number; login: string; avatar_url: string; }>; labels: Array<{ id: number; name: string; color: string; description?: string; }>; milestone?: { id: number; number: number; title: string; description?: string; state: 'open' | 'closed'; due_on?: string; created_at: string; }; created_at: string; updated_at: string; closed_at?: string; draft?: boolean; pull_request?: { url: string; html_url: string; diff_url: string; patch_url: string; }; reactions: { total_count: number; '+1': number; '-1': number; laugh: number; hooray: number; confused: number; heart: number; rocket: number; eyes: number; }; html_url: string; repository_url: string; } // GitHubUser interface removed - using inline type instead export class GitHubAdapter extends BaseAdapter implements IProviderAdapter { private baseUrl = 'https://api.github.com'; private owner!: string; private repo!: string; async initialize(config: ProviderConfig): Promise<void> { this.config = config; // Parse organization/project from config if (config.organization && config.project) { this.owner = config.organization; this.repo = config.project; } else if (config.project?.includes('/')) { [this.owner, this.repo] = config.project.split('/'); } else { throw new Error( 'GitHub adapter requires owner/repo format in organization+project or project fields', ); } await this.validateConnection(); this.initialized = true; } async validateConnection(): Promise<boolean> { try { const response = await fetch(`${this.baseUrl}/user`, { headers: this.createAuthHeaders(), }); if (!response.ok) { this.handleHttpError(response, 'user validation'); } return true; } catch (error) { throw new Error( `GitHub connection validation failed: ${error instanceof Error ? error.message : String(error)}`, ); } } async getWorkItem(id: string): Promise<WorkItem> { this.ensureInitialized(); const issueNumber = this.parseWorkItemId(id); const response = await fetch( `${this.baseUrl}/repos/${this.owner}/${this.repo}/issues/${issueNumber}`, { headers: this.createAuthHeaders(), }, ); if (!response.ok) { this.handleHttpError(response, `getting issue ${issueNumber}`); } const issue = (await response.json()) as GitHubIssue; return this.convertIssueToWorkItem(issue); } async listWorkItems(filter: WorkItemFilter): Promise<WorkItem[]> { this.ensureInitialized(); const params = new URLSearchParams(); if (filter.state) { params.append('state', filter.state); } if (filter.assignee) { params.append('assignee', filter.assignee); } if (filter.labels && filter.labels.length > 0) { params.append('labels', filter.labels.join(',')); } if (filter.milestone) { params.append('milestone', filter.milestone); } if (filter.since) { params.append('since', filter.since.toISOString()); } // Determine if we should include pull requests // GitHub treats pull requests as issues, but we can filter them out const includePullRequests = filter.type === 'feature' || filter.type === undefined; const response = await fetch( `${this.baseUrl}/repos/${this.owner}/${this.repo}/issues?${params.toString()}`, { headers: this.createAuthHeaders(), }, ); if (!response.ok) { this.handleHttpError(response, 'listing issues'); } const issues = (await response.json()) as GitHubIssue[]; // Filter out pull requests if not requested const filteredIssues = includePullRequests ? issues : issues.filter((issue) => !issue.pull_request); return filteredIssues.map((issue) => this.convertIssueToWorkItem(issue)); } async createWorkItem(data: CreateWorkItemData): Promise<WorkItem> { this.ensureInitialized(); this.validateCreateData(data); // GitHub doesn't have native work item types, so we use labels to indicate type const labels = [...(data.labels ?? [])]; const workItemTypeLabel = this.getTypeLabel(data.type); if (workItemTypeLabel && !labels.includes(workItemTypeLabel)) { labels.push(workItemTypeLabel); } // Add priority label if specified if (data.priority && data.priority !== 'medium') { const priorityLabel = `priority: ${data.priority}`; if (!labels.includes(priorityLabel)) { labels.push(priorityLabel); } } const payload = { title: data.title, body: data.description, assignees: data.assignees?.map((a) => a.username) ?? [], labels: labels, milestone: data.milestone?.id ? parseInt(data.milestone.id) : undefined, }; const response = await fetch(`${this.baseUrl}/repos/${this.owner}/${this.repo}/issues`, { method: 'POST', headers: this.createAuthHeaders(), body: JSON.stringify(payload), }); if (!response.ok) { this.handleHttpError(response, 'creating issue'); } const issue = (await response.json()) as GitHubIssue; return this.convertIssueToWorkItem(issue); } async updateWorkItem(id: string, updates: UpdateWorkItemData): Promise<WorkItem> { this.ensureInitialized(); const issueNumber = this.parseWorkItemId(id); interface GitHubUpdatePayload { title?: string; body?: string; state?: string; assignees?: string[]; labels?: string[]; milestone?: number | null; } const payload: GitHubUpdatePayload = {}; if (updates.title) payload.title = updates.title; if (updates.description) payload.body = updates.description; if (updates.state) payload.state = updates.state; if (updates.assignees) payload.assignees = updates.assignees.map((a) => a.username); if (updates.labels) payload.labels = updates.labels; if (updates.milestone) payload.milestone = parseInt(updates.milestone.id); const response = await fetch( `${this.baseUrl}/repos/${this.owner}/${this.repo}/issues/${issueNumber}`, { method: 'PATCH', headers: this.createAuthHeaders(), body: JSON.stringify(payload), }, ); if (!response.ok) { this.handleHttpError(response, `updating issue ${issueNumber}`); } const issue = (await response.json()) as GitHubIssue; return this.convertIssueToWorkItem(issue); } async deleteWorkItem(id: string): Promise<void> { // GitHub doesn't support deleting issues via API // Instead, we close the issue and add a "deleted" label await this.updateWorkItem(id, { state: 'closed', labels: ['deleted', 'archived'], }); } async search(query: string): Promise<WorkItem[]> { this.ensureInitialized(); // Use GitHub's search API with repository scope const searchQuery = `repo:${this.owner}/${this.repo} ${query}`; const response = await fetch( `${this.baseUrl}/search/issues?q=${encodeURIComponent(searchQuery)}`, { headers: this.createAuthHeaders(), }, ); if (!response.ok) { this.handleHttpError(response, 'searching issues'); } const searchResult = (await response.json()) as { items?: GitHubIssue[] }; const issues: GitHubIssue[] = searchResult.items ?? []; return issues.map((issue) => this.convertIssueToWorkItem(issue)); } async executeQuery(query: string): Promise<WorkItem[]> { // GitHub search supports advanced query syntax return this.search(query); } async linkWorkItems(parent: string, child: string, linkType: string): Promise<void> { // GitHub doesn't have native linking, but we can simulate with comments and labels const parentNumber = this.parseWorkItemId(parent); const childNumber = this.parseWorkItemId(child); // Add comment to parent referencing child const commentBody = `Related ${linkType}: #${childNumber}`; await fetch( `${this.baseUrl}/repos/${this.owner}/${this.repo}/issues/${parentNumber}/comments`, { method: 'POST', headers: this.createAuthHeaders(), body: JSON.stringify({ body: commentBody }), }, ); // Add comment to child referencing parent const childCommentBody = `${linkType === 'blocks' ? 'Blocked by' : 'Related to'}: #${parentNumber}`; await fetch(`${this.baseUrl}/repos/${this.owner}/${this.repo}/issues/${childNumber}/comments`, { method: 'POST', headers: this.createAuthHeaders(), body: JSON.stringify({ body: childCommentBody }), }); } async unlinkWorkItems(parent: string, child: string): Promise<void> { // In a real implementation, we would need to find and remove the linking comments // For now, we'll add a comment indicating the unlink const parentNumber = this.parseWorkItemId(parent); const childNumber = this.parseWorkItemId(child); const commentBody = `Unlinked from #${childNumber}`; await fetch( `${this.baseUrl}/repos/${this.owner}/${this.repo}/issues/${parentNumber}/comments`, { method: 'POST', headers: this.createAuthHeaders(), body: JSON.stringify({ body: commentBody }), }, ); } getCapabilities(): ProviderCapabilities { return { supportsEpics: false, // Can simulate with parent issues supportsIterations: false, // Can use milestones supportsMilestones: true, supportsMultipleAssignees: true, supportsConfidential: false, supportsWeight: false, supportsTimeTracking: false, supportsCustomFields: true, // Through Projects Beta maxAssignees: 10, hierarchyLevels: 2, customWorkItemTypes: ['issue'], }; } // Helper methods private parseWorkItemId(id: string): string { // Expected format: github:owner/repo#number or github:number const parts = id.split(':'); if (parts.length >= 2) { const idPart = parts[parts.length - 1]; return idPart.includes('#') ? idPart.split('#')[1] : idPart; } throw new Error(`Invalid GitHub work item ID format: ${id}`); } private convertIssueToWorkItem(issue: GitHubIssue): WorkItem { const githubFields: GitHubSpecificFields = { number: issue.number, repository: `${this.owner}/${this.repo}`, stateReason: issue.state_reason, reactions: issue.reactions, isDraft: issue.draft, }; return { id: `github:${this.owner}/${this.repo}#${issue.number}`, provider: 'github', type: this.detectWorkItemType(issue), title: issue.title, description: issue.body ?? '', state: issue.state, author: this.convertGitHubUser(issue.user), assignees: issue.assignees.map((a) => this.convertGitHubUser(a)), labels: issue.labels.map((label) => label.name), milestone: issue.milestone ? this.convertGitHubMilestone(issue.milestone) : undefined, priority: this.extractPriorityFromLabels(issue.labels.map((l) => l.name)), createdAt: new Date(issue.created_at), updatedAt: new Date(issue.updated_at), closedAt: issue.closed_at ? new Date(issue.closed_at) : undefined, providerFields: githubFields, }; } private convertGitHubUser(githubUser: { id: number; login: string; name?: string; email?: string; }): User { return { id: githubUser.id.toString(), username: githubUser.login, displayName: githubUser.name ?? githubUser.login, email: githubUser.email, provider: 'github', }; } private convertGitHubMilestone(milestone: { id: number; number: number; title: string; description?: string; state: string; due_on?: string; created_at: string; }): Milestone { return { id: milestone.id.toString(), title: milestone.title, description: milestone.description, dueDate: milestone.due_on ? new Date(milestone.due_on) : undefined, state: milestone.state as 'open' | 'closed', provider: 'github', }; } private detectWorkItemType(issue: GitHubIssue): WorkItemType { const labels = issue.labels.map((l) => l.name.toLowerCase()); // Check for type labels if (labels.includes('epic')) return 'epic'; if (labels.includes('bug')) return 'bug'; if (labels.includes('task')) return 'task'; if (labels.includes('enhancement')) return 'story'; if (labels.includes('feature')) return 'feature'; if (labels.includes('test')) return 'test'; // Check if it's a pull request if (issue.pull_request) return 'feature'; // Check for parent-child pattern (epic simulation) if (issue.body && issue.body.includes('- [ ]') && issue.body.split('- [ ]').length > 3) { return 'epic'; } return 'issue'; // Default } private getTypeLabel(type: WorkItemType): string | null { const typeLabelMap: Record<WorkItemType, string> = { epic: 'epic', feature: 'enhancement', story: 'enhancement', bug: 'bug', task: 'task', test: 'test', issue: '', // No specific label for generic issues }; return typeLabelMap[type] || null; } private extractPriorityFromLabels(labels: string[]): Priority { const priorityPatterns: Array<{ pattern: RegExp; priority: Priority }> = [ { pattern: /priority:\s*critical|critical/i, priority: 'critical' }, { pattern: /priority:\s*high|high/i, priority: 'high' }, { pattern: /priority:\s*low|low/i, priority: 'low' }, { pattern: /priority:\s*medium|medium/i, priority: 'medium' }, ]; for (const label of labels) { for (const { pattern, priority } of priorityPatterns) { if (pattern.test(label)) { return priority; } } } return 'medium'; // Default priority } protected createAuthHeaders(): Record<string, string> { return { Authorization: `Bearer ${this.config.token}`, Accept: 'application/vnd.github.v3+json', 'Content-Type': 'application/json', 'User-Agent': 'Project-Nexus-MCP/1.0', }; } } |