diff --git a/app/comments.js b/app/comments.js index a8f2198..286a845 100644 --- a/app/comments.js +++ b/app/comments.js @@ -16,13 +16,19 @@ function buildTable(findings) { } /** - * 寫入 findings.json 到 workspace + * 寫入 findings.json。 + * 預設寫到 workspace;若提供 mirrorDir,則同步寫入另一份供 repo commit 使用。 */ -export function saveFindings(workspace, findings) { - const fullPath = path.join(workspace, FINDINGS_PATH); - fs.mkdirSync(path.dirname(fullPath), { recursive: true }); - fs.writeFileSync(fullPath, JSON.stringify(findings, null, 2) + '\n', 'utf8'); - console.log(` ✅ findings 寫入: ${fullPath} (${findings.length} 筆)`); +export function saveFindings(workspace, findings, mirrorDir = null) { + const targets = [workspace]; + if (mirrorDir && mirrorDir !== workspace) targets.push(mirrorDir); + + for (const targetDir of targets) { + const fullPath = path.join(targetDir, FINDINGS_PATH); + fs.mkdirSync(path.dirname(fullPath), { recursive: true }); + fs.writeFileSync(fullPath, JSON.stringify(findings, null, 2) + '\n', 'utf8'); + console.log(` ✅ findings 寫入: ${fullPath} (${findings.length} 筆)`); + } } /** diff --git a/app/comments.test.js b/app/comments.test.js new file mode 100644 index 0000000..27927bd --- /dev/null +++ b/app/comments.test.js @@ -0,0 +1,22 @@ +import { describe, it } from 'node:test'; +import assert from 'node:assert/strict'; +import fs from 'fs'; +import os from 'os'; +import path from 'path'; +import { saveFindings } from './comments.js'; +import { FINDINGS_PATH } from './config.js'; + +describe('saveFindings', () => { + it('writes findings to workspace and mirror dirs when provided', () => { + const workspace = fs.mkdtempSync(path.join(os.tmpdir(), 'findings-ws-')); + const mirrorDir = fs.mkdtempSync(path.join(os.tmpdir(), 'findings-mirror-')); + const findings = [{ level: 'warning', role: 'Leo', location: 'file.js:1', suggestion: 'test' }]; + + saveFindings(workspace, findings, mirrorDir); + + const workspaceText = fs.readFileSync(path.join(workspace, FINDINGS_PATH), 'utf8'); + const mirrorText = fs.readFileSync(path.join(mirrorDir, FINDINGS_PATH), 'utf8'); + assert.equal(workspaceText, JSON.stringify(findings, null, 2) + '\n'); + assert.equal(mirrorText, JSON.stringify(findings, null, 2) + '\n'); + }); +}); diff --git a/app/main.js b/app/main.js index ae25281..1850642 100644 --- a/app/main.js +++ b/app/main.js @@ -88,7 +88,8 @@ async function main() { // Step6: 寫入 findings.json,依序發布 comment console.log('\n📝 Step5: Findings 寫入與 Comment 發布'); - saveFindings(WORKSPACE, filtered); + const reviewDir = repoDir || WORKSPACE; + saveFindings(WORKSPACE, filtered, reviewDir); try { await postOldFindingsComment(filtered); await postNewNonCriticalComment(filtered); @@ -102,7 +103,7 @@ async function main() { console.log('\n🔎 Step6: JSON 格式驗證'); const missingPaths = []; for (const relPath of [FINDINGS_PATH, EXCLUSIONS_PATH]) { - const fullPath = path.join(repoDir || WORKSPACE, relPath); + const fullPath = path.join(reviewDir, relPath); try { const result = await validateJSONArrayFile(fullPath, relPath); if (!result.exists) missingPaths.push({ fullPath, relPath }); @@ -117,7 +118,7 @@ async function main() { // Step7: commit/push findings.json 到來源分支 console.log('\n💾 Step7: 記憶區 Commit/Push'); - await commitAndPush(WORKSPACE, repoDir); + await commitAndPush(WORKSPACE, repoDir || WORKSPACE); // Step9: 有 critical 問題則 exit 1 console.log('\n🚦 Step8: 嚴重問題檢查');