Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/pull-request-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,21 @@
delete-merged: true
name: test-snapshot-diff
pattern: test-snapshot-diff-*

print-commit-stats:
if: always()
name: Commit statistics
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Use Node.js ${{ env.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ env.node-version }}
cache: npm

- name: Print commit stats
run: |
node ./scripts/printCommitStats.mjs | tee --append $GITHUB_STEP_SUMMARY
Comment thread Fixed
132 changes: 132 additions & 0 deletions scripts/printCommitStat.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/* eslint-disable no-console */
/* eslint-disable no-magic-numbers */
/* eslint-env node */

import chalk from 'chalk';
import { exec } from 'child_process';
import { parsePatch } from 'diff';
import { promisify } from 'util';

function getCategory(
/** @type { string } */
path
) {
return path.includes('/.github/')
? 'others'
: path.endsWith('.md')
? 'doc'
: path.includes('/__tests__/')
? 'test'
: path.includes('package-lock.json')
? 'generated'
: 'production';
}

function toIntegerOrFixed(
/** @type { number } */
value,
/** @type { number } */
fractionDigits = 2
) {
return value % 1 === 0 ? value : value.toFixed(fractionDigits);
}

function toRatio(
/** @type { number } */
x,
/** @type { number } */
y
) {
if (x > y) {
if (y === 0) {
return [x, 0];
}

return [x / y, 1];
}

if (x === 0) {
return [0, y];
}

return [1, y / x];
}

function toRatioString(
/** @type { number } */
x,
/** @type { number } */
y,
positiveIsGood = false
) {
const chalks = x === y ? [chalk.yellow, chalk.yellow] : [chalk.green, chalk.red];
const [ratio1, ratio2] = toRatio(x, y);

positiveIsGood || chalks.reverse();

return chalks[x > y ? 0 : 1](`${toIntegerOrFixed(ratio1)}:${toIntegerOrFixed(ratio2)}`);
}

(async () => {
const { stdout: diffContent } = await promisify(exec)('git diff main..HEAD', { encoding: 'utf-8' });

const patches = parsePatch(diffContent);

/** @type { Map<'doc' | 'generated' | 'others' | 'production' | 'test', { numFile: number; numLineAdded: number; numLineRemoved: number; }> } */
const stats = new Map();

stats.set('doc', { numFile: 0, numLineAdded: 0, numLineRemoved: 0 });
stats.set('generated', { numFile: 0, numLineAdded: 0, numLineRemoved: 0 });
stats.set('others', { numFile: 0, numLineAdded: 0, numLineRemoved: 0 });
stats.set('production', { numFile: 0, numLineAdded: 0, numLineRemoved: 0 });
stats.set('test', { numFile: 0, numLineAdded: 0, numLineRemoved: 0 });

patches.forEach(patch => {
if (patch.newFileName || patch.oldFileName) {
const stat = stats.get(getCategory(patch.newFileName || patch.oldFileName));

stat.numFile++;

for (const hunk of patch.hunks) {
for (const line of hunk.lines) {
if (line.startsWith('+') && !line.startsWith('+++') && line.substring(1).trim()) {
stat.numLineAdded++;
}

if (line.startsWith('-') && !line.startsWith('---') && line.substring(1).trim()) {
stat.numLineRemoved++;
}
}
}
}
});

console.log('| | Files | Lines added | Lines added and removed | Lines ratio |');
console.log('| - | - | - | - | - |');

for (const [name, type] of [
['Production code', 'production'],
['Test code', 'test'],
['Documentation', 'doc'],
['Generated code', 'generated'],
['Others', 'others']
]) {
console.log(
`| ${[
name,
`${stats.get(type).numFile} files`,
`${stats.get(type).numLineAdded} lines added`,
`${stats.get(type).numLineAdded + stats.get(type).numLineRemoved} lines total`,
`${toRatioString(stats.get(type).numLineAdded, stats.get(type).numLineRemoved)} lines added vs. removed`
].join(' | ')} |`
);
}

console.log();

const [prodRatio, testRatio] = toRatio(stats.get('production').numLineAdded, stats.get('test').numLineAdded);

console.log(
`${testRatio > prodRatio ? '😇' : '🚨'} There are ${chalk.magenta(toIntegerOrFixed(testRatio))} lines of test code added for every ${chalk.magenta(toIntegerOrFixed(prodRatio))} lines of production code added.`
);
})();
Loading