Skip to content

Commit

Permalink
fix: compute regression based on total percentages
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatWizard committed Feb 9, 2022
1 parent 6688271 commit 73e41fb
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 101 deletions.
140 changes: 72 additions & 68 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13893,43 +13893,10 @@ module.exports = { addComment, deleteExistingComments };

/***/ }),

/***/ 109:
/***/ 1752:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

const simpleGit = __nccwpck_require__(4959);

async function gitClone(url, wikiPath) {
return await simpleGit().clone(url, wikiPath);
}

async function gitUpdate(wikiPath) {
return await simpleGit(wikiPath)
.addConfig("user.name", "Coverage Diff Action")
.addConfig("user.email", "coverage-diff-action")
.add("*")
.commit("Update coverage badge")
.push();
}

module.exports = { gitClone, gitUpdate };


/***/ }),

/***/ 8978:
/***/ ((module) => {

function average(arr, fixed = 2) {
return Math.round(arr.reduce((a, b) => a + b, 0) / arr.length).toFixed(fixed);
}

module.exports = { average };


/***/ }),

/***/ 1397:
/***/ ((module) => {
const coverageDiff = __nccwpck_require__(6387);

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

function renderDiff(base, head, diff, options = {}) {
function computeDiff(base, head, options = {}) {
const diff = coverageDiff.diff(base, head);

let totalTitle = "Total coverage";
if (diff.regression) {
totalTitle = `${
options.allowedToFail ? ICONS.WARN : ICONS.KO
} Total coverage is lower than the default branch`;
}
let summaryTitle = "click to open the diff coverage report";

let countRegression = 0;
let summaryTitle = "click to open the diff coverage report";
let table = [];

Object.keys(diff.diff).forEach((file) => {
if (file === "total") {
return;
}

let element = diff.diff[file];
const element = diff.diff[file];

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

let regression = CRITERIAS.some((criteria) => element[criteria].pct < 0);
if (regression) {
const fileRegression = CRITERIAS.some(
(criteria) => element[criteria].pct < 0
);
if (fileRegression) {
countRegression++;
}

table.push({
icon: regression ? ICONS.KO : ICONS.OK,
icon: fileRegression ? ICONS.KO : ICONS.OK,
filename: file,
lines: {
pct: _renderPct(head[file].lines.pct, false),
Expand All @@ -14001,21 +13966,34 @@ function renderDiff(base, head, diff, options = {}) {
}

let totals = {};
let globalRegression = false;
CRITERIAS.forEach((criteria) => {
let diffPct = head.total[criteria].pct - base.total[criteria].pct;
if (diffPct < 0) {
globalRegression = true;
}
totals[criteria] = `${_renderPct(
head.total[criteria].pct,
false
)} (${_renderPct(head.total[criteria].pct - base.total[criteria].pct)})`;
)} (${_renderPct(diffPct)})`;
});

return `
if (globalRegression) {
totalTitle = `${
options.allowedToFail ? ICONS.WARN : ICONS.KO
} Total coverage is lower than the default branch`;
}

return {
regression: globalRegression,
markdown: `
### ${totalTitle}

| Lines | Branches | Functions | Statements |
| --------------- | ------------------ | ------------------- | -------------------- |
| ${totals.lines} | ${totals.branches} | ${totals.functions} | ${
totals.statements
} |
totals.statements
} |
${
table.length > 0
? `
Expand All @@ -14040,10 +14018,46 @@ ${
</details>`
: ""
}
`;
`,
};
}

module.exports = { renderDiff };
module.exports = { computeDiff };


/***/ }),

/***/ 109:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

const simpleGit = __nccwpck_require__(4959);

async function gitClone(url, wikiPath) {
return await simpleGit().clone(url, wikiPath);
}

async function gitUpdate(wikiPath) {
return await simpleGit(wikiPath)
.addConfig("user.name", "Coverage Diff Action")
.addConfig("user.email", "coverage-diff-action")
.add("*")
.commit("Update coverage badge")
.push();
}

module.exports = { gitClone, gitUpdate };


/***/ }),

/***/ 8978:
/***/ ((module) => {

function average(arr, fixed = 2) {
return Math.round(arr.reduce((a, b) => a + b, 0) / arr.length).toFixed(fixed);
}

module.exports = { average };


/***/ }),
Expand Down Expand Up @@ -14254,13 +14268,12 @@ const { existsSync } = __nccwpck_require__(7147);
const path = __nccwpck_require__(1017);
const core = __nccwpck_require__(2186);
const github = __nccwpck_require__(5438);
const coverageDiff = __nccwpck_require__(6387);

const { gitClone, gitUpdate } = __nccwpck_require__(109);
const { isBranch, isMainBranch } = __nccwpck_require__(6381);
const { getShieldURL, getJSONBadge } = __nccwpck_require__(3673);
const { average } = __nccwpck_require__(8978);
const { renderDiff } = __nccwpck_require__(1397);
const { computeDiff } = __nccwpck_require__(1752);
const { addComment, deleteExistingComments } = __nccwpck_require__(427);

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

const diff = coverageDiff.diff(base, head);
const diff = computeDiff(base, head, { allowedToFail });

if (issue_number) {
await deleteExistingComments(
octokit,
context.repo,
issue_number
);
await deleteExistingComments(octokit, context.repo, issue_number);

core.info("Add a comment with the diff coverage report");
await addComment(
octokit,
context.repo,
issue_number,
renderDiff(base, head, diff, { allowedToFail })
);
await addComment(octokit, context.repo, issue_number, diff.markdown);
} else {
core.info(diff.results);
}

if (!allowedToFail && diff.regression) {
throw new Error("The coverage is below the minimum threshold");
throw new Error("Total coverage is lower than the default branch");
}
}
}
Expand Down
50 changes: 32 additions & 18 deletions src/render.js → src/diff.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const coverageDiff = require("coverage-diff");

const ICONS = {
OK: "✅",
WARN: "⚠️",
Expand All @@ -13,36 +15,34 @@ function _renderPct(pct, addSign = true) {
return `${pct.toFixed(2)}%`;
}

function renderDiff(base, head, diff, options = {}) {
function computeDiff(base, head, options = {}) {
const diff = coverageDiff.diff(base, head);

let totalTitle = "Total coverage";
if (diff.regression) {
totalTitle = `${
options.allowedToFail ? ICONS.WARN : ICONS.KO
} Total coverage is lower than the default branch`;
}
let summaryTitle = "click to open the diff coverage report";

let countRegression = 0;
let summaryTitle = "click to open the diff coverage report";
let table = [];

Object.keys(diff.diff).forEach((file) => {
if (file === "total") {
return;
}

let element = diff.diff[file];
const element = diff.diff[file];

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

let regression = CRITERIAS.some((criteria) => element[criteria].pct < 0);
if (regression) {
const fileRegression = CRITERIAS.some(
(criteria) => element[criteria].pct < 0
);
if (fileRegression) {
countRegression++;
}

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

let totals = {};
let globalRegression = false;
CRITERIAS.forEach((criteria) => {
let diffPct = head.total[criteria].pct - base.total[criteria].pct;
if (diffPct < 0) {
globalRegression = true;
}
totals[criteria] = `${_renderPct(
head.total[criteria].pct,
false
)} (${_renderPct(head.total[criteria].pct - base.total[criteria].pct)})`;
)} (${_renderPct(diffPct)})`;
});

return `
if (globalRegression) {
totalTitle = `${
options.allowedToFail ? ICONS.WARN : ICONS.KO
} Total coverage is lower than the default branch`;
}

return {
regression: globalRegression,
markdown: `
### ${totalTitle}
| Lines | Branches | Functions | Statements |
| --------------- | ------------------ | ------------------- | -------------------- |
| ${totals.lines} | ${totals.branches} | ${totals.functions} | ${
totals.statements
} |
totals.statements
} |
${
table.length > 0
? `
Expand All @@ -107,7 +120,8 @@ ${
</details>`
: ""
}
`;
`,
};
}

module.exports = { renderDiff };
module.exports = { computeDiff };
20 changes: 5 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ const { existsSync } = require("fs");
const path = require("path");
const core = require("@actions/core");
const github = require("@actions/github");
const coverageDiff = require("coverage-diff");

const { gitClone, gitUpdate } = require("./git");
const { isBranch, isMainBranch } = require("./branch");
const { getShieldURL, getJSONBadge } = require("./badge");
const { average } = require("./math");
const { renderDiff } = require("./render");
const { computeDiff } = require("./diff");
const { addComment, deleteExistingComments } = require("./comment");

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

const diff = coverageDiff.diff(base, head);
const diff = computeDiff(base, head, { allowedToFail });

if (issue_number) {
await deleteExistingComments(
octokit,
context.repo,
issue_number
);
await deleteExistingComments(octokit, context.repo, issue_number);

core.info("Add a comment with the diff coverage report");
await addComment(
octokit,
context.repo,
issue_number,
renderDiff(base, head, diff, { allowedToFail })
);
await addComment(octokit, context.repo, issue_number, diff.markdown);
} else {
core.info(diff.results);
}

if (!allowedToFail && diff.regression) {
throw new Error("The coverage is below the minimum threshold");
throw new Error("Total coverage is lower than the default branch");
}
}
}
Expand Down

0 comments on commit 73e41fb

Please sign in to comment.