|
| 1 | +/** |
| 2 | + * ralph-starter github — Interactive GitHub issues wizard |
| 3 | + * |
| 4 | + * Guides the user through selecting GitHub issues to work on: |
| 5 | + * 1. Authenticate (gh CLI or token) |
| 6 | + * 2. Browse repos + issues or paste a URL |
| 7 | + * 3. Select issues (multi-select) |
| 8 | + * 4. Delegate to run command |
| 9 | + */ |
| 10 | + |
| 11 | +import chalk from 'chalk'; |
| 12 | +import inquirer from 'inquirer'; |
| 13 | +import { askBrowseOrUrl, askForUrl, ensureCredentials } from '../integrations/wizards/shared.js'; |
| 14 | +import { type RunCommandOptions, runCommand } from './run.js'; |
| 15 | + |
| 16 | +export type GitHubWizardOptions = { |
| 17 | + commit?: boolean; |
| 18 | + push?: boolean; |
| 19 | + pr?: boolean; |
| 20 | + validate?: boolean; |
| 21 | + maxIterations?: number; |
| 22 | + agent?: string; |
| 23 | +}; |
| 24 | + |
| 25 | +type GitHubRepo = { |
| 26 | + name: string; |
| 27 | + owner: { login: string }; |
| 28 | + description: string; |
| 29 | +}; |
| 30 | + |
| 31 | +type GitHubIssue = { |
| 32 | + number: number; |
| 33 | + title: string; |
| 34 | + labels: Array<{ name: string }>; |
| 35 | +}; |
| 36 | + |
| 37 | +type GitHubLabel = { |
| 38 | + name: string; |
| 39 | +}; |
| 40 | + |
| 41 | +/** Check if gh CLI is available and authenticated */ |
| 42 | +async function isGhCliAvailable(): Promise<boolean> { |
| 43 | + try { |
| 44 | + const { execa } = await import('execa'); |
| 45 | + await execa('gh', ['auth', 'status']); |
| 46 | + return true; |
| 47 | + } catch { |
| 48 | + return false; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/** Fetch user's repos via gh CLI */ |
| 53 | +async function fetchReposViaCli(limit = 30): Promise<GitHubRepo[]> { |
| 54 | + const { execa } = await import('execa'); |
| 55 | + const result = await execa('gh', [ |
| 56 | + 'repo', |
| 57 | + 'list', |
| 58 | + '--json', |
| 59 | + 'name,owner,description', |
| 60 | + '--limit', |
| 61 | + String(limit), |
| 62 | + '--sort', |
| 63 | + 'updated', |
| 64 | + ]); |
| 65 | + return JSON.parse(result.stdout); |
| 66 | +} |
| 67 | + |
| 68 | +/** Fetch open issues for a repo via gh CLI */ |
| 69 | +async function fetchIssuesViaCli( |
| 70 | + owner: string, |
| 71 | + repo: string, |
| 72 | + label?: string, |
| 73 | + limit = 30 |
| 74 | +): Promise<GitHubIssue[]> { |
| 75 | + const { execa } = await import('execa'); |
| 76 | + const args = [ |
| 77 | + 'issue', |
| 78 | + 'list', |
| 79 | + '-R', |
| 80 | + `${owner}/${repo}`, |
| 81 | + '--json', |
| 82 | + 'number,title,labels', |
| 83 | + '--limit', |
| 84 | + String(limit), |
| 85 | + '--state', |
| 86 | + 'open', |
| 87 | + ]; |
| 88 | + if (label) { |
| 89 | + args.push('--label', label); |
| 90 | + } |
| 91 | + const result = await execa('gh', args); |
| 92 | + return JSON.parse(result.stdout); |
| 93 | +} |
| 94 | + |
| 95 | +/** Fetch labels for a repo via gh CLI */ |
| 96 | +async function fetchLabelsViaCli(owner: string, repo: string): Promise<GitHubLabel[]> { |
| 97 | + const { execa } = await import('execa'); |
| 98 | + const result = await execa('gh', [ |
| 99 | + 'label', |
| 100 | + 'list', |
| 101 | + '-R', |
| 102 | + `${owner}/${repo}`, |
| 103 | + '--json', |
| 104 | + 'name', |
| 105 | + '--limit', |
| 106 | + '50', |
| 107 | + ]); |
| 108 | + return JSON.parse(result.stdout); |
| 109 | +} |
| 110 | + |
| 111 | +/** Parse a GitHub URL into owner/repo and optional issue number */ |
| 112 | +function parseGitHubUrl(url: string): { owner: string; repo: string; issue?: number } | null { |
| 113 | + // Match: https://github.com/owner/repo/issues/123 |
| 114 | + const issueMatch = url.match(/^https?:\/\/github\.com\/([^/]+)\/([^/]+)\/issues\/(\d+)/); |
| 115 | + if (issueMatch) { |
| 116 | + return { |
| 117 | + owner: issueMatch[1], |
| 118 | + repo: issueMatch[2].replace(/\.git$/, ''), |
| 119 | + issue: parseInt(issueMatch[3], 10), |
| 120 | + }; |
| 121 | + } |
| 122 | + |
| 123 | + // Match: https://github.com/owner/repo |
| 124 | + const repoMatch = url.match(/^https?:\/\/github\.com\/([^/]+)\/([^/]+)/); |
| 125 | + if (repoMatch) { |
| 126 | + return { |
| 127 | + owner: repoMatch[1], |
| 128 | + repo: repoMatch[2].replace(/\.git$/, '').replace(/\/$/, ''), |
| 129 | + }; |
| 130 | + } |
| 131 | + |
| 132 | + return null; |
| 133 | +} |
| 134 | + |
| 135 | +export async function githubCommand(options: GitHubWizardOptions): Promise<void> { |
| 136 | + console.log(); |
| 137 | + console.log(chalk.cyan.bold(' GitHub Issues')); |
| 138 | + console.log(chalk.dim(' Build from GitHub issues interactively')); |
| 139 | + console.log(); |
| 140 | + |
| 141 | + // Step 1: Ensure credentials |
| 142 | + await ensureCredentials('github', 'GitHub', { |
| 143 | + credKey: 'token', |
| 144 | + consoleUrl: 'https://github.com/settings/tokens', |
| 145 | + envVar: 'GITHUB_TOKEN', |
| 146 | + checkCliAuth: isGhCliAvailable, |
| 147 | + }); |
| 148 | + |
| 149 | + // Step 2: Browse or URL? |
| 150 | + const mode = await askBrowseOrUrl('GitHub'); |
| 151 | + |
| 152 | + if (mode === 'url') { |
| 153 | + const url = await askForUrl('GitHub', /^https?:\/\/github\.com\//); |
| 154 | + const parsed = parseGitHubUrl(url); |
| 155 | + if (!parsed) { |
| 156 | + console.log( |
| 157 | + chalk.red(' Could not parse GitHub URL. Expected format: github.com/owner/repo') |
| 158 | + ); |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + const runOpts: RunCommandOptions = { |
| 163 | + from: 'github', |
| 164 | + project: `${parsed.owner}/${parsed.repo}`, |
| 165 | + issue: parsed.issue, |
| 166 | + auto: true, |
| 167 | + commit: options.commit ?? false, |
| 168 | + push: options.push, |
| 169 | + pr: options.pr, |
| 170 | + validate: options.validate ?? true, |
| 171 | + maxIterations: options.maxIterations, |
| 172 | + agent: options.agent, |
| 173 | + }; |
| 174 | + |
| 175 | + await runCommand(undefined, runOpts); |
| 176 | + return; |
| 177 | + } |
| 178 | + |
| 179 | + // Browse mode |
| 180 | + // Step 3: Fetch and select repository |
| 181 | + console.log(chalk.dim(' Fetching your repositories...')); |
| 182 | + let repos: GitHubRepo[]; |
| 183 | + try { |
| 184 | + repos = await fetchReposViaCli(); |
| 185 | + } catch (err) { |
| 186 | + console.log(chalk.red(' Failed to fetch repositories. Check your authentication.')); |
| 187 | + console.log(chalk.dim(` Error: ${err instanceof Error ? err.message : String(err)}`)); |
| 188 | + return; |
| 189 | + } |
| 190 | + |
| 191 | + if (repos.length === 0) { |
| 192 | + console.log(chalk.yellow(' No repositories found.')); |
| 193 | + return; |
| 194 | + } |
| 195 | + |
| 196 | + const { selectedRepo } = await inquirer.prompt([ |
| 197 | + { |
| 198 | + type: 'select', |
| 199 | + name: 'selectedRepo', |
| 200 | + message: 'Select a repository:', |
| 201 | + choices: repos.map((r) => ({ |
| 202 | + name: `${r.owner.login}/${r.name}${r.description ? chalk.dim(` — ${r.description.slice(0, 60)}`) : ''}`, |
| 203 | + value: `${r.owner.login}/${r.name}`, |
| 204 | + })), |
| 205 | + }, |
| 206 | + ]); |
| 207 | + |
| 208 | + const [owner, repo] = selectedRepo.split('/'); |
| 209 | + |
| 210 | + // Step 4: Optional label filter |
| 211 | + let selectedLabel: string | undefined; |
| 212 | + try { |
| 213 | + const labels = await fetchLabelsViaCli(owner, repo); |
| 214 | + if (labels.length > 0) { |
| 215 | + const { labelChoice } = await inquirer.prompt([ |
| 216 | + { |
| 217 | + type: 'select', |
| 218 | + name: 'labelChoice', |
| 219 | + message: 'Filter by label?', |
| 220 | + choices: [ |
| 221 | + { name: 'All issues (no filter)', value: '__none__' }, |
| 222 | + ...labels.map((l) => ({ name: l.name, value: l.name })), |
| 223 | + ], |
| 224 | + }, |
| 225 | + ]); |
| 226 | + if (labelChoice !== '__none__') { |
| 227 | + selectedLabel = labelChoice; |
| 228 | + } |
| 229 | + } |
| 230 | + } catch { |
| 231 | + // Labels fetch failed, skip filter |
| 232 | + } |
| 233 | + |
| 234 | + // Step 5: Fetch and select issues |
| 235 | + console.log(chalk.dim(` Fetching open issues for ${owner}/${repo}...`)); |
| 236 | + let issues: GitHubIssue[]; |
| 237 | + try { |
| 238 | + issues = await fetchIssuesViaCli(owner, repo, selectedLabel); |
| 239 | + } catch (err) { |
| 240 | + console.log(chalk.red(' Failed to fetch issues.')); |
| 241 | + console.log(chalk.dim(` Error: ${err instanceof Error ? err.message : String(err)}`)); |
| 242 | + return; |
| 243 | + } |
| 244 | + |
| 245 | + if (issues.length === 0) { |
| 246 | + console.log( |
| 247 | + chalk.yellow( |
| 248 | + ` No open issues found${selectedLabel ? ` with label "${selectedLabel}"` : ''}.` |
| 249 | + ) |
| 250 | + ); |
| 251 | + return; |
| 252 | + } |
| 253 | + |
| 254 | + const { selectedIssues } = await inquirer.prompt([ |
| 255 | + { |
| 256 | + type: 'checkbox', |
| 257 | + name: 'selectedIssues', |
| 258 | + message: 'Select issues to work on:', |
| 259 | + choices: issues.map((issue) => { |
| 260 | + const labelTags = |
| 261 | + issue.labels.length > 0 |
| 262 | + ? ` ${chalk.dim(`[${issue.labels.map((l) => l.name).join(', ')}]`)}` |
| 263 | + : ''; |
| 264 | + return { |
| 265 | + name: `#${issue.number} — ${issue.title}${labelTags}`, |
| 266 | + value: issue.number, |
| 267 | + }; |
| 268 | + }), |
| 269 | + validate: (input: number[]) => (input.length > 0 ? true : 'Please select at least one issue'), |
| 270 | + }, |
| 271 | + ]); |
| 272 | + |
| 273 | + // Step 6: Run for each selected issue |
| 274 | + console.log(); |
| 275 | + console.log( |
| 276 | + chalk.green( |
| 277 | + ` Starting build for ${selectedIssues.length} issue${selectedIssues.length > 1 ? 's' : ''}...` |
| 278 | + ) |
| 279 | + ); |
| 280 | + console.log(); |
| 281 | + |
| 282 | + for (const issueNumber of selectedIssues) { |
| 283 | + const runOpts: RunCommandOptions = { |
| 284 | + from: 'github', |
| 285 | + project: `${owner}/${repo}`, |
| 286 | + issue: issueNumber, |
| 287 | + label: selectedLabel, |
| 288 | + auto: true, |
| 289 | + commit: options.commit ?? false, |
| 290 | + push: options.push, |
| 291 | + pr: options.pr, |
| 292 | + validate: options.validate ?? true, |
| 293 | + maxIterations: options.maxIterations, |
| 294 | + agent: options.agent, |
| 295 | + }; |
| 296 | + |
| 297 | + await runCommand(undefined, runOpts); |
| 298 | + } |
| 299 | +} |
0 commit comments