|
| 1 | +import { spawnSync } from 'node:child_process'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { Command, Flags } from '@oclif/core'; |
| 5 | +import { filenamePrefix } from '../../config/constants.ts'; |
| 6 | +import { |
| 7 | + type CommitEntry, |
| 8 | + calculateOverallStats, |
| 9 | + formatAsCsv, |
| 10 | + formatAsText, |
| 11 | + groupCommitsByMonth, |
| 12 | + parseGitLogOutput, |
| 13 | + type ReportData, |
| 14 | +} from '../../service/committers.svc.ts'; |
| 15 | +import { getErrorMessage, isErrnoException } from '../../service/error.svc.ts'; |
| 16 | + |
| 17 | +export default class Committers extends Command { |
| 18 | + static override description = 'Generate report of committers to a git repository'; |
| 19 | + static enableJsonFlag = true; |
| 20 | + static override examples = [ |
| 21 | + '<%= config.bin %> <%= command.id %>', |
| 22 | + '<%= config.bin %> <%= command.id %> --csv -s', |
| 23 | + '<%= config.bin %> <%= command.id %> --json', |
| 24 | + '<%= config.bin %> <%= command.id %> --csv', |
| 25 | + ]; |
| 26 | + |
| 27 | + static override flags = { |
| 28 | + months: Flags.integer({ |
| 29 | + char: 'm', |
| 30 | + description: 'The number of months of git history to review', |
| 31 | + default: 12, |
| 32 | + }), |
| 33 | + csv: Flags.boolean({ |
| 34 | + char: 'c', |
| 35 | + description: 'Output in CSV format', |
| 36 | + default: false, |
| 37 | + }), |
| 38 | + save: Flags.boolean({ |
| 39 | + char: 's', |
| 40 | + description: `Save the committers report as ${filenamePrefix}.committers.<output>`, |
| 41 | + default: false, |
| 42 | + }), |
| 43 | + }; |
| 44 | + |
| 45 | + public async run(): Promise<ReportData | string> { |
| 46 | + const { flags } = await this.parse(Committers); |
| 47 | + const { months, csv, save } = flags; |
| 48 | + const isJson = this.jsonEnabled(); |
| 49 | + |
| 50 | + const sinceDate = `${months} months ago`; |
| 51 | + this.log('Starting committers report with flags: %O', flags); |
| 52 | + |
| 53 | + try { |
| 54 | + // Generate structured report data |
| 55 | + const entries = this.fetchGitCommitData(sinceDate); |
| 56 | + this.log('Fetched %d commit entries', entries.length); |
| 57 | + const reportData = this.generateReportData(entries); |
| 58 | + |
| 59 | + // Handle different output scenarios |
| 60 | + if (isJson) { |
| 61 | + // JSON mode |
| 62 | + if (save) { |
| 63 | + try { |
| 64 | + fs.writeFileSync(path.resolve(`${filenamePrefix}.committers.json`), JSON.stringify(reportData, null, 2)); |
| 65 | + this.log('Report written to json'); |
| 66 | + } catch (error) { |
| 67 | + this.error(`Failed to save JSON report: ${getErrorMessage(error)}`); |
| 68 | + } |
| 69 | + } |
| 70 | + return reportData; |
| 71 | + } |
| 72 | + |
| 73 | + const textOutput = formatAsText(reportData); |
| 74 | + |
| 75 | + if (csv) { |
| 76 | + // CSV mode |
| 77 | + const csvOutput = formatAsCsv(reportData); |
| 78 | + if (save) { |
| 79 | + try { |
| 80 | + fs.writeFileSync(path.resolve(`${filenamePrefix}.committers.csv`), csvOutput); |
| 81 | + this.log('Report written to csv'); |
| 82 | + } catch (error) { |
| 83 | + this.error(`Failed to save CSV report: ${getErrorMessage(error)}`); |
| 84 | + } |
| 85 | + } else { |
| 86 | + this.log(textOutput); |
| 87 | + } |
| 88 | + return csvOutput; |
| 89 | + } |
| 90 | + |
| 91 | + if (save) { |
| 92 | + try { |
| 93 | + fs.writeFileSync(path.resolve(`${filenamePrefix}.committers.txt`), textOutput); |
| 94 | + this.log('Report written to txt'); |
| 95 | + } catch (error) { |
| 96 | + this.error(`Failed to save txt report: ${getErrorMessage(error)}`); |
| 97 | + } |
| 98 | + } else { |
| 99 | + this.log(textOutput); |
| 100 | + } |
| 101 | + return textOutput; |
| 102 | + } catch (error) { |
| 103 | + this.error(`Failed to generate report: ${getErrorMessage(error)}`); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Generates structured report data |
| 109 | + * @param entries - parsed git log output for commits |
| 110 | + */ |
| 111 | + private generateReportData(entries: CommitEntry[]): ReportData { |
| 112 | + if (entries.length === 0) { |
| 113 | + return { monthly: {}, overall: { total: 0 } }; |
| 114 | + } |
| 115 | + |
| 116 | + const monthlyData = groupCommitsByMonth(entries); |
| 117 | + const overallStats = calculateOverallStats(entries); |
| 118 | + const grandTotal = entries.length; |
| 119 | + |
| 120 | + // Format into a structured report data object |
| 121 | + const report: ReportData = { |
| 122 | + monthly: {}, |
| 123 | + overall: { ...overallStats, total: grandTotal }, |
| 124 | + }; |
| 125 | + |
| 126 | + // Add monthly totals |
| 127 | + for (const [month, authors] of Object.entries(monthlyData)) { |
| 128 | + const monthTotal = Object.values(authors).reduce((sum, count) => sum + count, 0); |
| 129 | + report.monthly[month] = { ...authors, total: monthTotal }; |
| 130 | + } |
| 131 | + |
| 132 | + return report; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Fetches git commit data with month and author information |
| 137 | + * @param sinceDate - Date range for git log |
| 138 | + */ |
| 139 | + private fetchGitCommitData(sinceDate: string): CommitEntry[] { |
| 140 | + const logProcess = spawnSync( |
| 141 | + 'git', |
| 142 | + [ |
| 143 | + 'log', |
| 144 | + '--all', // Include committers on all branches in the repo |
| 145 | + '--format="%ad|%an"', // Format: date|author |
| 146 | + '--date=format:%Y-%m', // Format date as YYYY-MM |
| 147 | + `--since="${sinceDate}"`, |
| 148 | + ], |
| 149 | + { encoding: 'utf-8' }, |
| 150 | + ); |
| 151 | + |
| 152 | + if (logProcess.error) { |
| 153 | + if (isErrnoException(logProcess.error)) { |
| 154 | + if (logProcess.error.code === 'ENOENT') { |
| 155 | + this.error('Git command not found. Please ensure git is installed and available in your PATH.'); |
| 156 | + } |
| 157 | + this.error(`Git command failed: ${getErrorMessage(logProcess.error)}`); |
| 158 | + } |
| 159 | + this.error(`Git command failed: ${getErrorMessage(logProcess.error)}`); |
| 160 | + } |
| 161 | + |
| 162 | + if (logProcess.status !== 0) { |
| 163 | + this.error(`Git command failed with status ${logProcess.status}: ${logProcess.stderr}`); |
| 164 | + } |
| 165 | + |
| 166 | + if (!logProcess.stdout) { |
| 167 | + return []; |
| 168 | + } |
| 169 | + |
| 170 | + return parseGitLogOutput(logProcess.stdout); |
| 171 | + } |
| 172 | +} |
0 commit comments