Skip to content

Commit 521c706

Browse files
committed
ci(danger): correct file delta calculation in reports
1 parent c3dbded commit 521c706

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

util/danger/format.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,20 @@ describe("renderDangerReport", () => {
4848
expect(output).toContain("## ✨ Highlights");
4949
expect(output.trim().startsWith("> 🚧 Danger.js checks for MrDocs")).toBe(true);
5050
});
51+
52+
it("treats removed files as positive file deltas", () => {
53+
const summary = summarizeScopes([
54+
{ filename: "src/lib/old.cpp", additions: 0, deletions: 5, status: "removed" },
55+
]);
56+
const result: DangerResult = { warnings: [], summary };
57+
58+
const output = renderDangerReport(result);
59+
const sourceRow = output.split("\n").find((line) => line.startsWith("| Source"));
60+
61+
expect(sourceRow).toBeDefined();
62+
expect(sourceRow).not.toMatch(/-1/);
63+
expect(sourceRow).toMatch(
64+
/\|\s*Source\s*\|\s*\*\*5\*\*\s*\|\s*-\s*\|\s*5\s*\|\s*\*\*1\*\*\s*\|\s*-\s*\|\s*-\s*\|\s*-\s*\|\s*1\s*\|/,
65+
);
66+
});
5167
});

util/danger/format.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ function renderWarnings(warnings: string[]): string {
8080
return ["## ⚠️ Warnings", blocks.join("\n\n")].join("\n");
8181
}
8282

83+
function countFileChanges(status: ScopeTotals["status"]): number {
84+
return status.added + status.modified + status.renamed + status.removed + status.other;
85+
}
86+
8387
/**
8488
* Render a single table combining change summary and per-scope breakdown.
8589
*/
@@ -100,14 +104,14 @@ function renderChangeTable(summary: ScopeReport): string {
100104

101105
const scopeHasChange = (totals: ScopeTotals): boolean => {
102106
const churn = totals.additions + totals.deletions;
103-
const fileDelta = totals.status.added + totals.status.modified + totals.status.renamed - totals.status.removed;
107+
const fileDelta = countFileChanges(totals.status);
104108
return churn !== 0 || fileDelta !== 0;
105109
};
106110

107111
const scopeRows = sortedScopes.filter((scope) => scopeHasChange(summary.totals[scope])).map((scope) => {
108112
const scoped: ScopeTotals = summary.totals[scope];
109113
const s = scoped.status;
110-
const fileDelta = s.added + s.modified + s.renamed - s.removed;
114+
const fileDelta = countFileChanges(s);
111115
const churn = scoped.additions + scoped.deletions;
112116
const fileDeltaBold = formatCount(fileDelta); // bold delta
113117
const label = labelForScope(scope);
@@ -127,7 +131,7 @@ function renderChangeTable(summary: ScopeReport): string {
127131
const total = summary.overall;
128132
const totalStatus = total.status;
129133
const totalChurn = total.additions + total.deletions;
130-
const totalFileDelta = totalStatus.added + totalStatus.modified + totalStatus.renamed - totalStatus.removed;
134+
const totalFileDelta = countFileChanges(totalStatus);
131135
const totalRow = [
132136
"**Total**",
133137
formatCount(totalChurn),

0 commit comments

Comments
 (0)