Skip to content

Commit f384866

Browse files
feat: add base-ref input to override PR base branch (#146)
* feat: add base-ref input to override PR base branch * Apply suggestions from code review Co-authored-by: Ryan Christian <33403762+rschristian@users.noreply.github.com> --------- Co-authored-by: Ryan Christian <33403762+rschristian@users.noreply.github.com>
1 parent 68177c9 commit f384866

3 files changed

Lines changed: 30 additions & 7 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ By default, files are compared after gzip compression, but it's possible to use
201201
compression: "none"
202202
```
203203

204+
### Specifying the base ref
205+
206+
Use the `base-ref` option to compare against a specific ref. Otherwise, the action compares against the PR's base branch.
207+
208+
209+
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.
210+
204211
### Checking multiple bundles
205212

206213
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:

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ inputs:
99
description: 'The GITHUB_TOKEN secret'
1010
required: false
1111
default: ${{ github.token }}
12+
base-ref:
13+
description: 'A specific git ref (branch, tag, or SHA) to compare against instead of the PR base branch'
14+
required: false
1215
clean-script:
1316
description: 'An npm-script that cleans/resets state between branch builds'
1417
install-script:

src/index.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ async function run(octokit, context, token) {
3737
);
3838
}
3939

40+
const inputBaseRef = getInput('base-ref');
41+
if (inputBaseRef) {
42+
console.log(`Setting base ref to: "${inputBaseRef}"`);
43+
baseRef = inputBaseRef;
44+
// fallback base sha doesn't make sense if given an explicit base ref:
45+
baseSha = null;
46+
}
47+
4048
if (getInput('cwd')) process.chdir(getInput('cwd'));
4149

4250
const plugin = new SizePlugin({
@@ -76,15 +84,19 @@ async function run(octokit, context, token) {
7684
console.log('successfully fetched base.ref');
7785
} catch (e) {
7886
console.log('fetching base.ref failed', e.message);
79-
try {
80-
await exec(`git fetch -n origin ${baseSha}`);
81-
console.log('successfully fetched base.sha');
82-
} catch (e) {
83-
console.log('fetching base.sha failed', e.message);
87+
if (baseSha === null) {
88+
throw new Error('base.ref fetch failed and no base.sha as fallback');
89+
} else {
8490
try {
85-
await exec(`git fetch -n`);
91+
await exec(`git fetch -n origin ${baseSha}`);
92+
console.log('successfully fetched base.sha');
8693
} catch (e) {
87-
console.log('fetch failed', e.message);
94+
console.log('fetching base.sha failed', e.message);
95+
try {
96+
await exec(`git fetch -n`);
97+
} catch (e) {
98+
console.log('fetch failed', e.message);
99+
}
88100
}
89101
}
90102
}
@@ -101,6 +113,7 @@ async function run(octokit, context, token) {
101113
if (!baseRef) throw Error('missing context.payload.base.ref');
102114
await exec(`git reset --hard ${baseRef}`);
103115
} catch (e) {
116+
if (!baseSha) throw e;
104117
await exec(`git reset --hard ${baseSha}`);
105118
}
106119
endGroup();

0 commit comments

Comments
 (0)