1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| const simpleGit = require('simple-git'); const { ESLint } = require('eslint');
const git = simpleGit();
const eslint = new ESLint();
async function analyzeCommitChanges(commitHash, fileChanges) { try { for (const fileChange of fileChanges) { const { file } = fileChange; if (file.endsWith('.js')) { const diff = await getFileDiff(commitHash, file); if (diff) { console.log(`文件名: ${file}`); console.log('变更内容:', diff); const results = await analyzeJavaScriptCode(diff); if (results) { results.forEach(result => { console.log(`- ${result.message} (${result.ruleId}) at line ${result.line}`); }); } } } } } catch (error) { console.error('静态分析变更内容时出错:', error); } }
async function getFileDiff(commitHash, file) { return new Promise((resolve, reject) => { git.diff([commitHash, file], (err, diff) => { if (err) { console.error('获取文件变更内容时出错:', err); resolve(null); } else { resolve(diff); } }); }); }
async function analyzeJavaScriptCode(code) { try { const results = await eslint.lintText(code); return results[0].messages; } catch (error) { console.error('静态分析 JavaScript 代码时出错:', error); return null; } }
git.log((err, log) => { if (err) { console.error('获取提交记录时出错:', err); return; } console.log('提交记录:', log); log.all.forEach(commit => { getAndAnalyzeFileChanges(commit); }); });
function getAndAnalyzeFileChanges(commit) { git.diffSummary(commit.hash, (err, summary) => { if (err) { console.error('获取文件变更摘要信息时出错:', err); return; } console.log('文件变更摘要信息:', summary); analyzeCommitChanges(commit.hash, summary.files); }); }
|