From 522eb72e49b86718cf3efd2b7247d26d1305c223 Mon Sep 17 00:00:00 2001 From: Michele Esposito <34438276+mikesposito@users.noreply.github.com> Date: Wed, 9 Nov 2022 16:02:44 +0100 Subject: [PATCH] chore: add bundle size diff to metamaskbot (#16098) --- .circleci/config.yml | 7 ++- development/metamaskbot-build-announce.js | 66 +++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index eed7250c0..f9decf829 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -541,7 +541,7 @@ jobs: - store_artifacts: path: test-artifacts destination: test-artifacts - + test-e2e-chrome-mv3: executor: node-browsers parallelism: 8 @@ -797,6 +797,11 @@ jobs: - store_artifacts: path: development/ts-migration-dashboard/build destination: ts-migration-dashboard + - run: + name: Set branch parent commit env var + command: | + echo "export PARENT_COMMIT=$(git rev-parse "$(git rev-list --topo-order --reverse HEAD ^origin/develop | head -1)"^)" >> $BASH_ENV + source $BASH_ENV - run: name: build:announce command: ./development/metamaskbot-build-announce.js diff --git a/development/metamaskbot-build-announce.js b/development/metamaskbot-build-announce.js index 8ce98ce67..d23f45871 100755 --- a/development/metamaskbot-build-announce.js +++ b/development/metamaskbot-build-announce.js @@ -23,6 +23,8 @@ async function start() { console.log('CIRCLE_BUILD_NUM', CIRCLE_BUILD_NUM); const { CIRCLE_WORKFLOW_JOB_ID } = process.env; console.log('CIRCLE_WORKFLOW_JOB_ID', CIRCLE_WORKFLOW_JOB_ID); + const { PARENT_COMMIT } = process.env; + console.log('PARENT_COMMIT', PARENT_COMMIT); if (!CIRCLE_PULL_REQUEST) { console.warn(`No pull request detected for commit "${CIRCLE_SHA1}"`); @@ -87,6 +89,9 @@ async function start() { .map((key) => `
  • ${key}: ${bundles[key].join(', ')}
  • `) .join('')}`; + const bundleSizeDataUrl = + 'https://raw.githubusercontent.com/MetaMask/extension_bundlesize_stats/main/stats/bundle_size_data.json'; + const coverageUrl = `${BUILD_LINK_BASE}/coverage/index.html`; const coverageLink = `Report`; @@ -243,6 +248,67 @@ async function start() { console.log(`No results for ${summaryPlatform} found; skipping benchmark`); } + try { + const prBundleSizeStats = JSON.parse( + await fs.readFile( + path.resolve( + __dirname, + '..', + path.join('test-artifacts', 'chrome', 'mv3', 'bundle_size.json'), + ), + 'utf-8', + ), + ); + + const devBundleSizeStats = await ( + await fetch(bundleSizeDataUrl, { + method: 'GET', + }) + ).json(); + + const prSizes = { + background: prBundleSizeStats.background.size, + ui: prBundleSizeStats.ui.size, + common: prBundleSizeStats.common.size, + }; + + const devSizes = Object.keys(prSizes).reduce((sizes, part) => { + sizes[part] = devBundleSizeStats[PARENT_COMMIT][part] || 0; + return sizes; + }, {}); + + const diffs = Object.keys(prSizes).reduce((output, part) => { + output[part] = prSizes[part] - devSizes[part]; + return output; + }, {}); + + const sizeDiffRows = Object.keys(diffs).map( + (part) => `${part}: ${diffs[part]} bytes`, + ); + + const sizeDiffHiddenContent = ``; + + const sizeDiff = diffs.background + diffs.common; + + const sizeDiffWarning = + sizeDiff > 0 + ? `🚨 Warning! Bundle size has increased!` + : `🚀 Bundle size reduced!`; + + const sizeDiffExposedContent = + sizeDiff === 0 + ? `Bundle size diffs` + : `Bundle size diffs [${sizeDiffWarning}]`; + + const sizeDiffBody = `
    ${sizeDiffExposedContent}${sizeDiffHiddenContent}
    \n\n`; + + commentBody += sizeDiffBody; + } catch (error) { + console.error(`Error constructing bundle size diffs results: '${error}'`); + } + try { const highlights = await getHighlights({ artifactBase: BUILD_LINK_BASE }); if (highlights) {