|
| 1 | +import chalk from 'chalk'; |
| 2 | +import { getLatestAppRuns } from '../utils/github.js'; |
| 3 | + |
| 4 | +interface GetAppRunsOptions { |
| 5 | + latest?: number; |
| 6 | + platform?: string; |
| 7 | + output?: string; |
| 8 | +} |
| 9 | + |
| 10 | +const VALID_PLATFORMS = ['all', 'windows', 'mac', 'linux']; |
| 11 | +const VALID_OUTPUT_FORMATS = ['simple', 'json']; |
| 12 | + |
| 13 | +export async function getAppRuns(options: GetAppRunsOptions): Promise<void> { |
| 14 | + const limit = options.latest || 1; |
| 15 | + const platform = options.platform || 'all'; |
| 16 | + const output = options.output; |
| 17 | + |
| 18 | + // Validate platform |
| 19 | + if (!VALID_PLATFORMS.includes(platform)) { |
| 20 | + console.error(chalk.red(`Error: Invalid platform "${platform}". Valid options are: ${VALID_PLATFORMS.join(', ')}`)); |
| 21 | + process.exit(1); |
| 22 | + } |
| 23 | + |
| 24 | + // Validate output format |
| 25 | + if (output && !VALID_OUTPUT_FORMATS.includes(output)) { |
| 26 | + console.error(chalk.red(`Error: Invalid output format "${output}". Valid options are: ${VALID_OUTPUT_FORMATS.join(', ')}`)); |
| 27 | + process.exit(1); |
| 28 | + } |
| 29 | + |
| 30 | + if (output !== 'json') { |
| 31 | + const platformDesc = platform === 'all' ? 'each platform' : platform; |
| 32 | + console.log(chalk.blue(`Fetching latest ${limit} app build run${limit > 1 ? 's' : ''} for ${platformDesc}...\n`)); |
| 33 | + } |
| 34 | + |
| 35 | + try { |
| 36 | + const runs = await getLatestAppRuns(limit, platform); |
| 37 | + |
| 38 | + if (runs.length === 0) { |
| 39 | + if (output === 'json') { |
| 40 | + console.log(JSON.stringify([], null, 2)); |
| 41 | + } else { |
| 42 | + console.log(chalk.yellow('No workflow runs found')); |
| 43 | + } |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + // JSON output |
| 48 | + if (output === 'json') { |
| 49 | + console.log(JSON.stringify(runs, null, 2)); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + // Simple output - just platform name and run URL |
| 54 | + if (output === 'simple') { |
| 55 | + runs.forEach((workflowRuns) => { |
| 56 | + workflowRuns.runs.forEach((run) => { |
| 57 | + console.log(`${workflowRuns.workflowName}: ${run.url}`); |
| 58 | + }); |
| 59 | + }); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + // Default detailed output |
| 64 | + runs.forEach((workflowRuns, index) => { |
| 65 | + if (index > 0) { |
| 66 | + console.log(''); // Add spacing between workflows |
| 67 | + } |
| 68 | + |
| 69 | + console.log(chalk.bold.cyan(`${workflowRuns.workflowName}:`)); |
| 70 | + console.log(chalk.dim('─'.repeat(60))); |
| 71 | + |
| 72 | + workflowRuns.runs.forEach((run, runIndex) => { |
| 73 | + const statusIcon = run.status === 'completed' |
| 74 | + ? (run.conclusion === 'success' ? '✅' : run.conclusion === 'failure' ? '❌' : '⚠️') |
| 75 | + : '🔄'; |
| 76 | + |
| 77 | + const statusColor = run.status === 'completed' |
| 78 | + ? (run.conclusion === 'success' ? chalk.green : run.conclusion === 'failure' ? chalk.red : chalk.yellow) |
| 79 | + : chalk.blue; |
| 80 | + |
| 81 | + console.log(`\n${runIndex + 1}. ${statusIcon} ${statusColor(run.status.toUpperCase())}${run.conclusion ? ` (${run.conclusion})` : ''}`); |
| 82 | + console.log(chalk.dim(` Run ID: ${run.id}`)); |
| 83 | + console.log(chalk.dim(` Branch: ${run.headBranch}`)); |
| 84 | + console.log(chalk.dim(` Commit: ${run.headSha.substring(0, 7)}`)); |
| 85 | + console.log(chalk.dim(` Created: ${new Date(run.createdAt).toLocaleString()}`)); |
| 86 | + console.log(chalk.cyan(` URL: ${run.url}`)); |
| 87 | + |
| 88 | + if (run.artifacts.length > 0) { |
| 89 | + console.log(chalk.green(` Artifacts (${run.artifacts.length}):`)); |
| 90 | + run.artifacts.forEach(artifact => { |
| 91 | + console.log(chalk.dim(` • ${artifact.name} (${formatBytes(artifact.size)})`)); |
| 92 | + console.log(chalk.dim(` Download: ${artifact.downloadUrl}`)); |
| 93 | + }); |
| 94 | + } else if (run.status === 'completed' && run.conclusion === 'success') { |
| 95 | + console.log(chalk.yellow(` No artifacts available`)); |
| 96 | + } |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + console.log('\n' + chalk.dim('─'.repeat(60))); |
| 101 | + console.log(chalk.blue('\nView all runs at:')); |
| 102 | + console.log(chalk.cyan('https://github.com/kubernetes-sigs/headlamp/actions')); |
| 103 | + } catch (error) { |
| 104 | + if (output === 'json') { |
| 105 | + console.error(JSON.stringify({ error: String(error) }, null, 2)); |
| 106 | + } else { |
| 107 | + console.error(chalk.red('Error fetching app runs:')); |
| 108 | + console.error(error); |
| 109 | + } |
| 110 | + process.exit(1); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +function formatBytes(bytes: number): string { |
| 115 | + if (bytes === 0) return '0 Bytes'; |
| 116 | + const k = 1024; |
| 117 | + const sizes = ['Bytes', 'KB', 'MB', 'GB']; |
| 118 | + const i = Math.floor(Math.log(bytes) / Math.log(k)); |
| 119 | + return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i]; |
| 120 | +} |
0 commit comments