|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import fs from 'fs'; |
| 4 | +import path from 'path'; |
| 5 | + |
| 6 | +const args = process.argv.slice(2); |
| 7 | + |
| 8 | +function getArgValue(flag, fallback = null) { |
| 9 | + const idx = args.indexOf(flag); |
| 10 | + if (idx === -1) return fallback; |
| 11 | + const value = args[idx + 1]; |
| 12 | + return value ?? fallback; |
| 13 | +} |
| 14 | + |
| 15 | +function ensureParent(filePath) { |
| 16 | + fs.mkdirSync(path.dirname(filePath), { recursive: true }); |
| 17 | +} |
| 18 | + |
| 19 | +function shortHash(value) { |
| 20 | + if (!value) return 'missing'; |
| 21 | + const text = String(value); |
| 22 | + return text.length <= 12 ? text : text.slice(0, 12); |
| 23 | +} |
| 24 | + |
| 25 | +function parsePatchStats(patchText) { |
| 26 | + const stats = new Map(); |
| 27 | + |
| 28 | + if (!patchText.trim()) { |
| 29 | + return stats; |
| 30 | + } |
| 31 | + |
| 32 | + let currentFile = null; |
| 33 | + for (const line of patchText.split('\n')) { |
| 34 | + if (line.startsWith('diff --git ')) { |
| 35 | + const match = line.match(/^diff --git a\/(.+?) b\//); |
| 36 | + currentFile = match ? match[1] : null; |
| 37 | + if (currentFile && !stats.has(currentFile)) { |
| 38 | + stats.set(currentFile, { |
| 39 | + file: currentFile, |
| 40 | + additions: 0, |
| 41 | + deletions: 0, |
| 42 | + hunks: 0, |
| 43 | + }); |
| 44 | + } |
| 45 | + continue; |
| 46 | + } |
| 47 | + |
| 48 | + if (!currentFile) continue; |
| 49 | + |
| 50 | + const current = stats.get(currentFile); |
| 51 | + if (!current) continue; |
| 52 | + |
| 53 | + if (line.startsWith('@@')) { |
| 54 | + current.hunks += 1; |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + if (line.startsWith('+') && !line.startsWith('+++')) { |
| 59 | + current.additions += 1; |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + if (line.startsWith('-') && !line.startsWith('---')) { |
| 64 | + current.deletions += 1; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return stats; |
| 69 | +} |
| 70 | + |
| 71 | +function defaultAssessment(payload) { |
| 72 | + if (!payload.changed) { |
| 73 | + return { |
| 74 | + status: 'completed', |
| 75 | + impactLevel: 'none', |
| 76 | + requiresProjectChanges: 'false', |
| 77 | + confidence: '0.95', |
| 78 | + rationale: 'No monitored documentation changes were detected in this run.', |
| 79 | + recommendedAction: 'No action required.', |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + return { |
| 84 | + status: 'pending-codex-review', |
| 85 | + impactLevel: 'pending', |
| 86 | + requiresProjectChanges: 'unknown', |
| 87 | + confidence: 'n/a', |
| 88 | + rationale: |
| 89 | + 'Changes were detected. Review the diff report and classify whether project code or docs need updates.', |
| 90 | + recommendedAction: |
| 91 | + 'Review `.tmp/docs-watch/docs-diff.md`, classify impact, and open/update a draft PR if changes are required.', |
| 92 | + }; |
| 93 | +} |
| 94 | + |
| 95 | +function escapePipe(value) { |
| 96 | + return String(value ?? '').replace(/\|/g, '\\|'); |
| 97 | +} |
| 98 | + |
| 99 | +async function main() { |
| 100 | + const checkPath = getArgValue('--check', '.tmp/docs-watch/docs-check.json'); |
| 101 | + const diffPath = getArgValue('--diff', '.tmp/docs-watch/docs-diff.patch'); |
| 102 | + const diffMarkdownPath = getArgValue('--diff-markdown', '.tmp/docs-watch/docs-diff.md'); |
| 103 | + const outputPath = getArgValue('--output', '.tmp/docs-watch/docs-watch-report.md'); |
| 104 | + const writeSummary = args.includes('--write-summary'); |
| 105 | + |
| 106 | + const checkPayload = JSON.parse(fs.readFileSync(checkPath, 'utf8')); |
| 107 | + const changed = checkPayload.results.filter((item) => item.changed); |
| 108 | + const patchText = fs.existsSync(diffPath) ? fs.readFileSync(diffPath, 'utf8') : ''; |
| 109 | + const patchStats = parsePatchStats(patchText); |
| 110 | + |
| 111 | + const assessment = defaultAssessment(checkPayload); |
| 112 | + |
| 113 | + const lines = []; |
| 114 | + lines.push('# Docs Watch Run Report'); |
| 115 | + lines.push(''); |
| 116 | + lines.push(`- generated_at: ${new Date().toISOString()}`); |
| 117 | + lines.push(`- check_file: ${checkPath}`); |
| 118 | + lines.push(`- diff_patch_file: ${diffPath}`); |
| 119 | + lines.push(`- diff_markdown_file: ${diffMarkdownPath}`); |
| 120 | + lines.push(`- changed: ${checkPayload.changed}`); |
| 121 | + lines.push(`- changed_count: ${checkPayload.changed_count}`); |
| 122 | + lines.push(`- state_dir: ${checkPayload.state_dir ?? 'none'}`); |
| 123 | + lines.push(''); |
| 124 | + |
| 125 | + lines.push('## Monitored Files'); |
| 126 | + lines.push(''); |
| 127 | + lines.push('| File | Changed | Baseline | Expected | Actual | First Diff Line |'); |
| 128 | + lines.push('| --- | --- | --- | --- | --- | --- |'); |
| 129 | + |
| 130 | + for (const item of checkPayload.results) { |
| 131 | + const diffLine = item.diff?.line ?? '-'; |
| 132 | + lines.push( |
| 133 | + `| ${escapePipe(item.file)} | ${item.changed} | ${escapePipe(item.baseline_source ?? 'unknown')} | ${shortHash(item.expected_hash)} | ${shortHash(item.actual_hash)} | ${diffLine} |`, |
| 134 | + ); |
| 135 | + } |
| 136 | + lines.push(''); |
| 137 | + |
| 138 | + lines.push('## Diff Stats'); |
| 139 | + lines.push(''); |
| 140 | + |
| 141 | + if (changed.length === 0) { |
| 142 | + lines.push('(no changed files)'); |
| 143 | + } else { |
| 144 | + lines.push('| File | Hunks | Additions | Deletions |'); |
| 145 | + lines.push('| --- | ---: | ---: | ---: |'); |
| 146 | + |
| 147 | + for (const item of changed) { |
| 148 | + const stats = patchStats.get(item.file) ?? { |
| 149 | + hunks: 0, |
| 150 | + additions: 0, |
| 151 | + deletions: 0, |
| 152 | + }; |
| 153 | + |
| 154 | + lines.push( |
| 155 | + `| ${escapePipe(item.file)} | ${stats.hunks} | ${stats.additions} | ${stats.deletions} |`, |
| 156 | + ); |
| 157 | + } |
| 158 | + } |
| 159 | + lines.push(''); |
| 160 | + |
| 161 | + lines.push('## Codex Assessment'); |
| 162 | + lines.push(''); |
| 163 | + lines.push(`- status: ${assessment.status}`); |
| 164 | + lines.push(`- impact_level: ${assessment.impactLevel}`); |
| 165 | + lines.push(`- requires_project_changes: ${assessment.requiresProjectChanges}`); |
| 166 | + lines.push(`- confidence: ${assessment.confidence}`); |
| 167 | + lines.push(''); |
| 168 | + lines.push('### Rationale'); |
| 169 | + lines.push(''); |
| 170 | + lines.push(assessment.rationale); |
| 171 | + lines.push(''); |
| 172 | + lines.push('### Recommended Action'); |
| 173 | + lines.push(''); |
| 174 | + lines.push(assessment.recommendedAction); |
| 175 | + lines.push(''); |
| 176 | + |
| 177 | + ensureParent(outputPath); |
| 178 | + fs.writeFileSync(outputPath, lines.join('\n'), 'utf8'); |
| 179 | + |
| 180 | + if (process.env.GITHUB_OUTPUT) { |
| 181 | + fs.appendFileSync(process.env.GITHUB_OUTPUT, `report_path=${path.resolve(outputPath)}\n`); |
| 182 | + fs.appendFileSync(process.env.GITHUB_OUTPUT, `changed=${checkPayload.changed}\n`); |
| 183 | + fs.appendFileSync(process.env.GITHUB_OUTPUT, `changed_count=${checkPayload.changed_count}\n`); |
| 184 | + fs.appendFileSync(process.env.GITHUB_OUTPUT, `assessment_status=${assessment.status}\n`); |
| 185 | + } |
| 186 | + |
| 187 | + if (writeSummary) { |
| 188 | + const summaryLines = [ |
| 189 | + '# Docs watch report summary', |
| 190 | + '', |
| 191 | + `- report: ${outputPath}`, |
| 192 | + `- changed: ${checkPayload.changed}`, |
| 193 | + `- changed_count: ${checkPayload.changed_count}`, |
| 194 | + `- assessment_status: ${assessment.status}`, |
| 195 | + ]; |
| 196 | + |
| 197 | + if (process.env.GITHUB_STEP_SUMMARY) { |
| 198 | + fs.appendFileSync(process.env.GITHUB_STEP_SUMMARY, summaryLines.join('\n') + '\n'); |
| 199 | + } else { |
| 200 | + console.log(summaryLines.join('\n')); |
| 201 | + } |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +main().catch((error) => { |
| 206 | + console.error(error); |
| 207 | + process.exit(1); |
| 208 | +}); |
0 commit comments