|
| 1 | +/** |
| 2 | + * POST /list-prs endpoint - List GitHub pull requests for a project |
| 3 | + */ |
| 4 | + |
| 5 | +import type { Request, Response } from 'express'; |
| 6 | +import { execAsync, execEnv, getErrorMessage, logError } from './common.js'; |
| 7 | +import { checkGitHubRemote } from './check-github-remote.js'; |
| 8 | + |
| 9 | +export interface GitHubLabel { |
| 10 | + name: string; |
| 11 | + color: string; |
| 12 | +} |
| 13 | + |
| 14 | +export interface GitHubAuthor { |
| 15 | + login: string; |
| 16 | +} |
| 17 | + |
| 18 | +export interface GitHubPR { |
| 19 | + number: number; |
| 20 | + title: string; |
| 21 | + state: string; |
| 22 | + author: GitHubAuthor; |
| 23 | + createdAt: string; |
| 24 | + labels: GitHubLabel[]; |
| 25 | + url: string; |
| 26 | + isDraft: boolean; |
| 27 | + headRefName: string; |
| 28 | + reviewDecision: string | null; |
| 29 | + mergeable: string; |
| 30 | + body: string; |
| 31 | +} |
| 32 | + |
| 33 | +export interface ListPRsResult { |
| 34 | + success: boolean; |
| 35 | + prs?: GitHubPR[]; |
| 36 | + openPRs?: GitHubPR[]; |
| 37 | + mergedPRs?: GitHubPR[]; |
| 38 | + error?: string; |
| 39 | +} |
| 40 | + |
| 41 | +export function createListPRsHandler() { |
| 42 | + return async (req: Request, res: Response): Promise<void> => { |
| 43 | + try { |
| 44 | + const { projectPath } = req.body; |
| 45 | + |
| 46 | + if (!projectPath) { |
| 47 | + res.status(400).json({ success: false, error: 'projectPath is required' }); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + // First check if this is a GitHub repo |
| 52 | + const remoteStatus = await checkGitHubRemote(projectPath); |
| 53 | + if (!remoteStatus.hasGitHubRemote) { |
| 54 | + res.status(400).json({ |
| 55 | + success: false, |
| 56 | + error: 'Project does not have a GitHub remote', |
| 57 | + }); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + // Fetch open PRs |
| 62 | + const { stdout: openStdout } = await execAsync( |
| 63 | + 'gh pr list --state open --json number,title,state,author,createdAt,labels,url,isDraft,headRefName,reviewDecision,mergeable,body --limit 100', |
| 64 | + { |
| 65 | + cwd: projectPath, |
| 66 | + env: execEnv, |
| 67 | + } |
| 68 | + ); |
| 69 | + |
| 70 | + // Fetch merged PRs |
| 71 | + const { stdout: mergedStdout } = await execAsync( |
| 72 | + 'gh pr list --state merged --json number,title,state,author,createdAt,labels,url,isDraft,headRefName,reviewDecision,mergeable,body --limit 50', |
| 73 | + { |
| 74 | + cwd: projectPath, |
| 75 | + env: execEnv, |
| 76 | + } |
| 77 | + ); |
| 78 | + |
| 79 | + const openPRs: GitHubPR[] = JSON.parse(openStdout || '[]'); |
| 80 | + const mergedPRs: GitHubPR[] = JSON.parse(mergedStdout || '[]'); |
| 81 | + |
| 82 | + res.json({ |
| 83 | + success: true, |
| 84 | + openPRs, |
| 85 | + mergedPRs, |
| 86 | + prs: [...openPRs, ...mergedPRs], |
| 87 | + }); |
| 88 | + } catch (error) { |
| 89 | + logError(error, 'List GitHub PRs failed'); |
| 90 | + res.status(500).json({ success: false, error: getErrorMessage(error) }); |
| 91 | + } |
| 92 | + }; |
| 93 | +} |
0 commit comments