Skip to content

Commit 73e41fb

Browse files
committed
fix: compute regression based on total percentages
1 parent 6688271 commit 73e41fb

File tree

3 files changed

+109
-101
lines changed

3 files changed

+109
-101
lines changed

dist/index.js

Lines changed: 72 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -13893,43 +13893,10 @@ module.exports = { addComment, deleteExistingComments };
1389313893

1389413894
/***/ }),
1389513895

13896-
/***/ 109:
13896+
/***/ 1752:
1389713897
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
1389813898

13899-
const simpleGit = __nccwpck_require__(4959);
13900-
13901-
async function gitClone(url, wikiPath) {
13902-
return await simpleGit().clone(url, wikiPath);
13903-
}
13904-
13905-
async function gitUpdate(wikiPath) {
13906-
return await simpleGit(wikiPath)
13907-
.addConfig("user.name", "Coverage Diff Action")
13908-
.addConfig("user.email", "coverage-diff-action")
13909-
.add("*")
13910-
.commit("Update coverage badge")
13911-
.push();
13912-
}
13913-
13914-
module.exports = { gitClone, gitUpdate };
13915-
13916-
13917-
/***/ }),
13918-
13919-
/***/ 8978:
13920-
/***/ ((module) => {
13921-
13922-
function average(arr, fixed = 2) {
13923-
return Math.round(arr.reduce((a, b) => a + b, 0) / arr.length).toFixed(fixed);
13924-
}
13925-
13926-
module.exports = { average };
13927-
13928-
13929-
/***/ }),
13930-
13931-
/***/ 1397:
13932-
/***/ ((module) => {
13899+
const coverageDiff = __nccwpck_require__(6387);
1393313900

1393413901
const ICONS = {
1393513902
OK: "✅",
@@ -13946,36 +13913,34 @@ function _renderPct(pct, addSign = true) {
1394613913
return `${pct.toFixed(2)}%`;
1394713914
}
1394813915

13949-
function renderDiff(base, head, diff, options = {}) {
13916+
function computeDiff(base, head, options = {}) {
13917+
const diff = coverageDiff.diff(base, head);
13918+
1395013919
let totalTitle = "Total coverage";
13951-
if (diff.regression) {
13952-
totalTitle = `${
13953-
options.allowedToFail ? ICONS.WARN : ICONS.KO
13954-
} Total coverage is lower than the default branch`;
13955-
}
13920+
let summaryTitle = "click to open the diff coverage report";
1395613921

1395713922
let countRegression = 0;
13958-
let summaryTitle = "click to open the diff coverage report";
1395913923
let table = [];
13960-
1396113924
Object.keys(diff.diff).forEach((file) => {
1396213925
if (file === "total") {
1396313926
return;
1396413927
}
1396513928

13966-
let element = diff.diff[file];
13929+
const element = diff.diff[file];
1396713930

1396813931
if (CRITERIAS.every((criteria) => element[criteria].pct === 0)) {
1396913932
return;
1397013933
}
1397113934

13972-
let regression = CRITERIAS.some((criteria) => element[criteria].pct < 0);
13973-
if (regression) {
13935+
const fileRegression = CRITERIAS.some(
13936+
(criteria) => element[criteria].pct < 0
13937+
);
13938+
if (fileRegression) {
1397413939
countRegression++;
1397513940
}
1397613941

1397713942
table.push({
13978-
icon: regression ? ICONS.KO : ICONS.OK,
13943+
icon: fileRegression ? ICONS.KO : ICONS.OK,
1397913944
filename: file,
1398013945
lines: {
1398113946
pct: _renderPct(head[file].lines.pct, false),
@@ -14001,21 +13966,34 @@ function renderDiff(base, head, diff, options = {}) {
1400113966
}
1400213967

1400313968
let totals = {};
13969+
let globalRegression = false;
1400413970
CRITERIAS.forEach((criteria) => {
13971+
let diffPct = head.total[criteria].pct - base.total[criteria].pct;
13972+
if (diffPct < 0) {
13973+
globalRegression = true;
13974+
}
1400513975
totals[criteria] = `${_renderPct(
1400613976
head.total[criteria].pct,
1400713977
false
14008-
)} (${_renderPct(head.total[criteria].pct - base.total[criteria].pct)})`;
13978+
)} (${_renderPct(diffPct)})`;
1400913979
});
1401013980

14011-
return `
13981+
if (globalRegression) {
13982+
totalTitle = `${
13983+
options.allowedToFail ? ICONS.WARN : ICONS.KO
13984+
} Total coverage is lower than the default branch`;
13985+
}
13986+
13987+
return {
13988+
regression: globalRegression,
13989+
markdown: `
1401213990
### ${totalTitle}
1401313991

1401413992
| Lines | Branches | Functions | Statements |
1401513993
| --------------- | ------------------ | ------------------- | -------------------- |
1401613994
| ${totals.lines} | ${totals.branches} | ${totals.functions} | ${
14017-
totals.statements
14018-
} |
13995+
totals.statements
13996+
} |
1401913997
${
1402013998
table.length > 0
1402113999
? `
@@ -14040,10 +14018,46 @@ ${
1404014018
</details>`
1404114019
: ""
1404214020
}
14043-
`;
14021+
`,
14022+
};
1404414023
}
1404514024

14046-
module.exports = { renderDiff };
14025+
module.exports = { computeDiff };
14026+
14027+
14028+
/***/ }),
14029+
14030+
/***/ 109:
14031+
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
14032+
14033+
const simpleGit = __nccwpck_require__(4959);
14034+
14035+
async function gitClone(url, wikiPath) {
14036+
return await simpleGit().clone(url, wikiPath);
14037+
}
14038+
14039+
async function gitUpdate(wikiPath) {
14040+
return await simpleGit(wikiPath)
14041+
.addConfig("user.name", "Coverage Diff Action")
14042+
.addConfig("user.email", "coverage-diff-action")
14043+
.add("*")
14044+
.commit("Update coverage badge")
14045+
.push();
14046+
}
14047+
14048+
module.exports = { gitClone, gitUpdate };
14049+
14050+
14051+
/***/ }),
14052+
14053+
/***/ 8978:
14054+
/***/ ((module) => {
14055+
14056+
function average(arr, fixed = 2) {
14057+
return Math.round(arr.reduce((a, b) => a + b, 0) / arr.length).toFixed(fixed);
14058+
}
14059+
14060+
module.exports = { average };
1404714061

1404814062

1404914063
/***/ }),
@@ -14254,13 +14268,12 @@ const { existsSync } = __nccwpck_require__(7147);
1425414268
const path = __nccwpck_require__(1017);
1425514269
const core = __nccwpck_require__(2186);
1425614270
const github = __nccwpck_require__(5438);
14257-
const coverageDiff = __nccwpck_require__(6387);
1425814271

1425914272
const { gitClone, gitUpdate } = __nccwpck_require__(109);
1426014273
const { isBranch, isMainBranch } = __nccwpck_require__(6381);
1426114274
const { getShieldURL, getJSONBadge } = __nccwpck_require__(3673);
1426214275
const { average } = __nccwpck_require__(8978);
14263-
const { renderDiff } = __nccwpck_require__(1397);
14276+
const { computeDiff } = __nccwpck_require__(1752);
1426414277
const { addComment, deleteExistingComments } = __nccwpck_require__(427);
1426514278

1426614279
const { context } = github;
@@ -14333,28 +14346,19 @@ async function run() {
1433314346
await readFile(path.join(WIKI_PATH, baseSummaryFilename), "utf8")
1433414347
);
1433514348

14336-
const diff = coverageDiff.diff(base, head);
14349+
const diff = computeDiff(base, head, { allowedToFail });
1433714350

1433814351
if (issue_number) {
14339-
await deleteExistingComments(
14340-
octokit,
14341-
context.repo,
14342-
issue_number
14343-
);
14352+
await deleteExistingComments(octokit, context.repo, issue_number);
1434414353

1434514354
core.info("Add a comment with the diff coverage report");
14346-
await addComment(
14347-
octokit,
14348-
context.repo,
14349-
issue_number,
14350-
renderDiff(base, head, diff, { allowedToFail })
14351-
);
14355+
await addComment(octokit, context.repo, issue_number, diff.markdown);
1435214356
} else {
1435314357
core.info(diff.results);
1435414358
}
1435514359

1435614360
if (!allowedToFail && diff.regression) {
14357-
throw new Error("The coverage is below the minimum threshold");
14361+
throw new Error("Total coverage is lower than the default branch");
1435814362
}
1435914363
}
1436014364
}

src/render.js renamed to src/diff.js

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const coverageDiff = require("coverage-diff");
2+
13
const ICONS = {
24
OK: "✅",
35
WARN: "⚠️",
@@ -13,36 +15,34 @@ function _renderPct(pct, addSign = true) {
1315
return `${pct.toFixed(2)}%`;
1416
}
1517

16-
function renderDiff(base, head, diff, options = {}) {
18+
function computeDiff(base, head, options = {}) {
19+
const diff = coverageDiff.diff(base, head);
20+
1721
let totalTitle = "Total coverage";
18-
if (diff.regression) {
19-
totalTitle = `${
20-
options.allowedToFail ? ICONS.WARN : ICONS.KO
21-
} Total coverage is lower than the default branch`;
22-
}
22+
let summaryTitle = "click to open the diff coverage report";
2323

2424
let countRegression = 0;
25-
let summaryTitle = "click to open the diff coverage report";
2625
let table = [];
27-
2826
Object.keys(diff.diff).forEach((file) => {
2927
if (file === "total") {
3028
return;
3129
}
3230

33-
let element = diff.diff[file];
31+
const element = diff.diff[file];
3432

3533
if (CRITERIAS.every((criteria) => element[criteria].pct === 0)) {
3634
return;
3735
}
3836

39-
let regression = CRITERIAS.some((criteria) => element[criteria].pct < 0);
40-
if (regression) {
37+
const fileRegression = CRITERIAS.some(
38+
(criteria) => element[criteria].pct < 0
39+
);
40+
if (fileRegression) {
4141
countRegression++;
4242
}
4343

4444
table.push({
45-
icon: regression ? ICONS.KO : ICONS.OK,
45+
icon: fileRegression ? ICONS.KO : ICONS.OK,
4646
filename: file,
4747
lines: {
4848
pct: _renderPct(head[file].lines.pct, false),
@@ -68,21 +68,34 @@ function renderDiff(base, head, diff, options = {}) {
6868
}
6969

7070
let totals = {};
71+
let globalRegression = false;
7172
CRITERIAS.forEach((criteria) => {
73+
let diffPct = head.total[criteria].pct - base.total[criteria].pct;
74+
if (diffPct < 0) {
75+
globalRegression = true;
76+
}
7277
totals[criteria] = `${_renderPct(
7378
head.total[criteria].pct,
7479
false
75-
)} (${_renderPct(head.total[criteria].pct - base.total[criteria].pct)})`;
80+
)} (${_renderPct(diffPct)})`;
7681
});
7782

78-
return `
83+
if (globalRegression) {
84+
totalTitle = `${
85+
options.allowedToFail ? ICONS.WARN : ICONS.KO
86+
} Total coverage is lower than the default branch`;
87+
}
88+
89+
return {
90+
regression: globalRegression,
91+
markdown: `
7992
### ${totalTitle}
8093
8194
| Lines | Branches | Functions | Statements |
8295
| --------------- | ------------------ | ------------------- | -------------------- |
8396
| ${totals.lines} | ${totals.branches} | ${totals.functions} | ${
84-
totals.statements
85-
} |
97+
totals.statements
98+
} |
8699
${
87100
table.length > 0
88101
? `
@@ -107,7 +120,8 @@ ${
107120
</details>`
108121
: ""
109122
}
110-
`;
123+
`,
124+
};
111125
}
112126

113-
module.exports = { renderDiff };
127+
module.exports = { computeDiff };

src/index.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ const { existsSync } = require("fs");
33
const path = require("path");
44
const core = require("@actions/core");
55
const github = require("@actions/github");
6-
const coverageDiff = require("coverage-diff");
76

87
const { gitClone, gitUpdate } = require("./git");
98
const { isBranch, isMainBranch } = require("./branch");
109
const { getShieldURL, getJSONBadge } = require("./badge");
1110
const { average } = require("./math");
12-
const { renderDiff } = require("./render");
11+
const { computeDiff } = require("./diff");
1312
const { addComment, deleteExistingComments } = require("./comment");
1413

1514
const { context } = github;
@@ -82,28 +81,19 @@ async function run() {
8281
await readFile(path.join(WIKI_PATH, baseSummaryFilename), "utf8")
8382
);
8483

85-
const diff = coverageDiff.diff(base, head);
84+
const diff = computeDiff(base, head, { allowedToFail });
8685

8786
if (issue_number) {
88-
await deleteExistingComments(
89-
octokit,
90-
context.repo,
91-
issue_number
92-
);
87+
await deleteExistingComments(octokit, context.repo, issue_number);
9388

9489
core.info("Add a comment with the diff coverage report");
95-
await addComment(
96-
octokit,
97-
context.repo,
98-
issue_number,
99-
renderDiff(base, head, diff, { allowedToFail })
100-
);
90+
await addComment(octokit, context.repo, issue_number, diff.markdown);
10191
} else {
10292
core.info(diff.results);
10393
}
10494

10595
if (!allowedToFail && diff.regression) {
106-
throw new Error("The coverage is below the minimum threshold");
96+
throw new Error("Total coverage is lower than the default branch");
10797
}
10898
}
10999
}

0 commit comments

Comments
 (0)