任务
按文件逐一判断:触及安全或配置为高风险,改动规模过大或缺少测试为中风险,其余为低风险,最后按文件名排序。
契约
- 输入:JSON 对象:{ files: [{ file, added, removed, touchesTests?, touchesConfig?, touchesSecurity? }] }
- 输出:JSON 对象数组:[{ file, risk }],按文件排序
样例
- 输入
{"files":[{"file":"src/auth.ts","added":4,"removed":2,"touchesSecurity":true},{"file":"README.md","added":120,"removed":3}]}-> 期望[{"file":"README.md","risk":"medium"},{"file":"src/auth.ts","risk":"high"}]。安全相关改动即使 diff 很小也属于高风险。 - 输入
{"files":[{"file":"src/auth.test.ts","added":30,"removed":5,"touchesTests":true}]}-> 期望[{"file":"src/auth.test.ts","risk":"low"}]。只改测试、未触及安全与配置的变更是低风险。
参考实现
function solve({ files }) {
return files
.map((entry) => {
let risk = 'low';
if (entry.touchesSecurity || entry.touchesConfig) risk = 'high';
else if (!entry.touchesTests && entry.added + entry.removed > 60) risk = 'medium';
return { file: entry.file, risk };
})
.sort((a, b) => a.file.localeCompare(b.file));
}
为什么有效
评审时间是交付中最稀缺的资源。风险分级能让大而机械的改动与小而敏感的改动得到完全不同的处理方式。
安全与配置优先于改动规模;一份巨大的文档 diff 不应被算成高风险。