From 73e41fb0c411d0dc54583511bd3ecdd72012fde1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20G=C3=A9rard?= Date: Wed, 9 Feb 2022 10:43:19 +0100 Subject: [PATCH] fix: compute regression based on total percentages --- dist/index.js | 140 +++++++++++++++++++------------------ src/{render.js => diff.js} | 50 ++++++++----- src/index.js | 20 ++---- 3 files changed, 109 insertions(+), 101 deletions(-) rename src/{render.js => diff.js} (78%) diff --git a/dist/index.js b/dist/index.js index ececa05..108cf5d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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: "✅", @@ -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), @@ -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 ? ` @@ -14040,10 +14018,46 @@ ${ ` : "" } -`; +`, + }; } -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 }; /***/ }), @@ -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; @@ -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"); } } } diff --git a/src/render.js b/src/diff.js similarity index 78% rename from src/render.js rename to src/diff.js index a488e8c..f0d4952 100644 --- a/src/render.js +++ b/src/diff.js @@ -1,3 +1,5 @@ +const coverageDiff = require("coverage-diff"); + const ICONS = { OK: "✅", WARN: "⚠️", @@ -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), @@ -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 ? ` @@ -107,7 +120,8 @@ ${ ` : "" } -`; +`, + }; } -module.exports = { renderDiff }; +module.exports = { computeDiff }; diff --git a/src/index.js b/src/index.js index 9c16dcf..491e91b 100644 --- a/src/index.js +++ b/src/index.js @@ -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; @@ -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"); } } }