From 61e88f4bdfe8a4c3f7ce6f0593b428b660d4a761 Mon Sep 17 00:00:00 2001 From: Michele Esposito <34438276+mikesposito@users.noreply.github.com> Date: Wed, 19 Jul 2023 17:16:36 +0200 Subject: [PATCH] ci: write human readable bundle sizes and specify delta (#20092) * ci: write human readable sizes and specify delta * fix: getHumanReadableSize negative values support * refactor: change formula --- development/metamaskbot-build-announce.js | 29 ++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/development/metamaskbot-build-announce.js b/development/metamaskbot-build-announce.js index 729f2ce5b..576b038f7 100755 --- a/development/metamaskbot-build-announce.js +++ b/development/metamaskbot-build-announce.js @@ -15,6 +15,29 @@ function capitalizeFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1); } +function getHumanReadableSize(bytes) { + if (!bytes) { + return '0 Bytes'; + } + + const absBytes = Math.abs(bytes); + const kibibyteSize = 1024; + const magnitudes = ['Bytes', 'KiB', 'MiB']; + let magnitudeIndex = 0; + if (absBytes > Math.pow(kibibyteSize, 2)) { + magnitudeIndex = 2; + } else if (absBytes > kibibyteSize) { + magnitudeIndex = 1; + } + return `${parseFloat( + (bytes / Math.pow(kibibyteSize, magnitudeIndex)).toFixed(2), + )} ${magnitudes[magnitudeIndex]}`; +} + +function getPercentageChange(from, to) { + return parseFloat(((to - from) / Math.abs(from)) * 100).toFixed(2); +} + async function start() { const { GITHUB_COMMENT_TOKEN, CIRCLE_PULL_REQUEST } = process.env; console.log('CIRCLE_PULL_REQUEST', CIRCLE_PULL_REQUEST); @@ -278,7 +301,11 @@ async function start() { }, {}); const sizeDiffRows = Object.keys(diffs).map( - (part) => `${part}: ${diffs[part]} bytes`, + (part) => + `${part}: ${getHumanReadableSize(diffs[part])} (${getPercentageChange( + devSizes[part], + prSizes[part], + )}%)`, ); const sizeDiffHiddenContent = `