diff --git a/README.md b/README.md index 4042f11..2fdaea8 100644 --- a/README.md +++ b/README.md @@ -201,6 +201,13 @@ By default, files are compared after gzip compression, but it's possible to use compression: "none" ``` +### Specifying the base ref + +Use the `base-ref` option to compare against a specific ref. Otherwise, the action compares against the PR's base branch. + + +For example, a project could set `base-ref` conditionally to use the PR target branch for feature PRs but compare against `production` for release PRs. Comparing against the previous release tag could show cumulative size change across all changes going into the release. + ### Checking multiple bundles The action reuses the same comment each time it runs on a PR. In order to run the action multiple times against separate bundles for a single PR, you must provide a `comment-key` option, which the action will use to determine which comment to add or update for the run. The example below demonstrates this for separate "modern" and "legacy" bundles: diff --git a/action.yml b/action.yml index aa4dae3..a88f084 100644 --- a/action.yml +++ b/action.yml @@ -9,6 +9,9 @@ inputs: description: 'The GITHUB_TOKEN secret' required: false default: ${{ github.token }} + base-ref: + description: 'A specific git ref (branch, tag, or SHA) to compare against instead of the PR base branch' + required: false clean-script: description: 'An npm-script that cleans/resets state between branch builds' install-script: diff --git a/src/index.js b/src/index.js index b99602f..2ad852a 100644 --- a/src/index.js +++ b/src/index.js @@ -37,6 +37,14 @@ async function run(octokit, context, token) { ); } + const inputBaseRef = getInput('base-ref'); + if (inputBaseRef) { + console.log(`Setting base ref to: "${inputBaseRef}"`); + baseRef = inputBaseRef; + // fallback base sha doesn't make sense if given an explicit base ref: + baseSha = null; + } + if (getInput('cwd')) process.chdir(getInput('cwd')); const plugin = new SizePlugin({ @@ -76,15 +84,19 @@ async function run(octokit, context, token) { console.log('successfully fetched base.ref'); } catch (e) { console.log('fetching base.ref failed', e.message); - try { - await exec(`git fetch -n origin ${baseSha}`); - console.log('successfully fetched base.sha'); - } catch (e) { - console.log('fetching base.sha failed', e.message); + if (baseSha === null) { + throw new Error('base.ref fetch failed and no base.sha as fallback'); + } else { try { - await exec(`git fetch -n`); + await exec(`git fetch -n origin ${baseSha}`); + console.log('successfully fetched base.sha'); } catch (e) { - console.log('fetch failed', e.message); + console.log('fetching base.sha failed', e.message); + try { + await exec(`git fetch -n`); + } catch (e) { + console.log('fetch failed', e.message); + } } } } @@ -101,6 +113,7 @@ async function run(octokit, context, token) { if (!baseRef) throw Error('missing context.payload.base.ref'); await exec(`git reset --hard ${baseRef}`); } catch (e) { + if (!baseSha) throw e; await exec(`git reset --hard ${baseSha}`); } endGroup();