// Posts a cognitive complexity comparison comment on PRs.
// Called from ci.yml via actions/github-script.
module.exports = async ({ github, context, core }) => {
const fs = require('fs');
const current = JSON.parse(
fs.readFileSync('complexity/cognitive-complexity-summary.json', 'utf8'),
);
const hasBase = fs.existsSync('base-cognitive-complexity-summary.json');
const base = hasBase
? JSON.parse(
fs.readFileSync('base-cognitive-complexity-summary.json', 'utf8'),
)
: null;
const changedFiles = fs
.readFileSync('changed-files.txt', 'utf8')
.trim()
.split('\n')
.filter(Boolean);
const baseByFile = new Map((base?.files ?? []).map(file => [file.file, file]));
const currentByFile = new Map(current.files.map(file => [file.file, file]));
const formatDiff = (currentValue, baseValue, { higherIsBad }) => {
if (baseValue == null) {
return '๐ new';
}
const diff = currentValue - baseValue;
if (diff > 0) {
return `${higherIsBad ? '๐ด' : 'โช'} +${diff}`;
}
if (diff < 0) {
return `${higherIsBad ? '๐ข' : 'โช'} ${diff}`;
}
return 'โช 0';
};
const complexityDiff = (currentValue, baseValue) =>
formatDiff(currentValue, baseValue, { higherIsBad: true });
const neutralDiff = (currentValue, baseValue) =>
formatDiff(currentValue, baseValue, { higherIsBad: false });
const status = value => {
if (value >= 25) {
return '๐ด High';
}
if (value >= 15) {
return '๐ก Watch';
}
return '๐ข OK';
};
const topEntries = current.entries.slice(0, 10).map(entry => {
return `| \`${entry.symbol ?? '(unknown function)'}\` | \`${entry.file}:${entry.line}\` | ${entry.complexity} | ${status(entry.complexity)} |`;
});
const changedRows = [];
for (const file of changedFiles) {
const currentFile = currentByFile.get(file);
const baseFile = baseByFile.get(file);
if (!currentFile && !baseFile) {
continue;
}
changedRows.push(
`| \`${file.replace(/^packages\//, '')}\` | ${currentFile?.totalComplexity ?? 0} | ${currentFile?.maxComplexity ?? 0} | ${currentFile?.functionCount ?? 0} | ${complexityDiff(currentFile?.totalComplexity ?? 0, baseFile?.totalComplexity)} |`,
);
}
const baseTotal = base?.totals.totalComplexity;
const baseMax = base?.totals.maxComplexity;
const sections = [
'',
'## ๐ง Cognitive Complexity Report',
'',
'| Metric | PR | Base | Diff |',
'| --- | ---: | ---: | ---: |',
`| Total complexity | ${current.totals.totalComplexity} | ${baseTotal ?? '-'} | ${complexityDiff(current.totals.totalComplexity, baseTotal)} |`,
`| Max function complexity | ${current.totals.maxComplexity} | ${baseMax ?? '-'} | ${complexityDiff(current.totals.maxComplexity, baseMax)} |`,
`| Functions measured | ${current.totals.functions} | ${base?.totals.functions ?? '-'} | ${neutralDiff(current.totals.functions, base?.totals.functions)} |`,
'',
'',
'๐งฉ Most complex functions
',
'',
'| Function | Location | Complexity | Status |',
'| --- | --- | ---: | --- |',
...(topEntries.length > 0 ? topEntries : ['| - | - | 0 | ๐ข OK |']),
'',
' ',
];
if (changedRows.length > 0) {
sections.push(
'',
'',
'๐งพ Changed files
',
'',
'| File | Total | Max | Functions | Total diff |',
'| --- | ---: | ---: | ---: | ---: |',
...changedRows,
'',
' ',
);
}
sections.push(
'',
'> ๐งญ Cognitive complexity is reported as a review signal, not a merge gate. Prefer small, intention-revealing refactors when complexity rises.',
'',
'---',
`Updated for [\`${context.sha.slice(0, 7)}\`](${context.payload.repository.html_url}/commit/${context.sha}) | ${hasBase ? 'Compared against base branch' : 'No base complexity cached yet - will compare after first merge to base'}`,
);
const body = sections.join('\n');
const { data: comments } = await github.rest.issues.listComments({
...context.repo,
issue_number: context.issue.number,
});
const existing = comments.find(comment =>
comment.body?.includes(''),
);
if (existing) {
await github.rest.issues.updateComment({
...context.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
...context.repo,
issue_number: context.issue.number,
body,
});
}
};