From 10fda8a9b9cb40ecec52ea9180fd1c78e52eeaad Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Sat, 13 Jun 2020 05:31:12 -0500 Subject: [PATCH] Update stats action for running locally (#14143) Noticed a few things that could be updated to make running the stats locally better e.g. if the folder name didn't match the name of the repo in the `stats-config.js` file it would fail to run and the `yarn` cache could break diffing from a cached package being used instead --- .../next-stats-action/src/constants.js | 4 ++++ .../actions/next-stats-action/src/index.js | 6 ++++++ .../next-stats-action/src/run/index.js | 19 ++++++++++--------- .../next-stats-action/src/util/exec.js | 8 ++++++-- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/.github/actions/next-stats-action/src/constants.js b/.github/actions/next-stats-action/src/constants.js index 2b31994f5d..ba3ec39b2f 100644 --- a/.github/actions/next-stats-action/src/constants.js +++ b/.github/actions/next-stats-action/src/constants.js @@ -8,6 +8,9 @@ const mainRepoDir = path.join(workDir, mainRepoName) const diffRepoDir = path.join(workDir, diffRepoName) const statsAppDir = path.join(workDir, 'stats-app') const diffingDir = path.join(workDir, 'diff') +const yarnEnvValues = { + YARN_CACHE_FOLDER: path.join(workDir, 'yarn-cache'), +} const allowedConfigLocations = [ './', '.stats-app', @@ -24,5 +27,6 @@ module.exports = { mainRepoDir, diffRepoDir, statsAppDir, + yarnEnvValues, allowedConfigLocations, } diff --git a/.github/actions/next-stats-action/src/index.js b/.github/actions/next-stats-action/src/index.js index add5ab4b52..2bacd52b35 100644 --- a/.github/actions/next-stats-action/src/index.js +++ b/.github/actions/next-stats-action/src/index.js @@ -46,6 +46,12 @@ if (!allowedActions.has(actionInfo.actionName) && !actionInfo.isRelease) { ) } + if (actionInfo.isLocal) { + // make sure to use local repo location instead of the + // one provided in statsConfig + statsConfig.mainRepo = actionInfo.prRepo + } + // clone main repository/ref if (!actionInfo.skipClone) { await cloneRepo(statsConfig.mainRepo, mainRepoDir) diff --git a/.github/actions/next-stats-action/src/run/index.js b/.github/actions/next-stats-action/src/run/index.js index c1556b1362..e8ddf32dea 100644 --- a/.github/actions/next-stats-action/src/run/index.js +++ b/.github/actions/next-stats-action/src/run/index.js @@ -6,7 +6,7 @@ const logger = require('../util/logger') const getDirSize = require('./get-dir-size') const collectStats = require('./collect-stats') const collectDiffs = require('./collect-diffs') -const { statsAppDir, diffRepoDir, mainRepoDir } = require('../constants') +const { statsAppDir, diffRepoDir, yarnEnvValues } = require('../constants') async function runConfigs( configs = [], @@ -32,12 +32,7 @@ async function runConfigs( // if stats-config is in root of project we're analyzing // the whole project so copy from each repo - const curStatsAppPath = - relativeStatsAppDir === './' - ? mainRepoStats - ? diffRepoDir - : mainRepoDir - : path.join(diffRepoDir, relativeStatsAppDir) + const curStatsAppPath = path.join(diffRepoDir, relativeStatsAppDir) // clean statsAppDir await fs.remove(statsAppDir) @@ -61,7 +56,9 @@ async function runConfigs( } const buildStart = new Date().getTime() - await exec(`cd ${statsAppDir} && ${statsConfig.appBuildCommand}`) + await exec(`cd ${statsAppDir} && ${statsConfig.appBuildCommand}`, false, { + env: yarnEnvValues, + }) curStats.General.buildDuration = new Date().getTime() - buildStart // apply renames to get deterministic output names @@ -182,7 +179,11 @@ async function linkPkgs(pkgDir = '', pkgPaths) { } } await fs.writeFile(pkgJsonPath, JSON.stringify(pkgData, null, 2), 'utf8') - await exec(`cd ${pkgDir} && yarn install`) + + await fs.remove(yarnEnvValues.YARN_CACHE_FOLDER) + await exec(`cd ${pkgDir} && yarn install`, false, { + env: yarnEnvValues, + }) } module.exports = runConfigs diff --git a/.github/actions/next-stats-action/src/util/exec.js b/.github/actions/next-stats-action/src/util/exec.js index ea16221b90..689bd5b295 100644 --- a/.github/actions/next-stats-action/src/util/exec.js +++ b/.github/actions/next-stats-action/src/util/exec.js @@ -9,9 +9,13 @@ const env = { PR_STATS_COMMENT_TOKEN: '', } -function exec(command, noLog = false) { +function exec(command, noLog = false, opts = {}) { if (!noLog) logger(`exec: ${command}`) - return execP(command, { env, timeout: 180 * 1000 }) + return execP(command, { + timeout: 180 * 1000, + ...opts, + env: { ...env, ...opts.env }, + }) } exec.spawn = function spawn(command = '', opts = {}) { -- GitLab