Skip to content

Commit

Permalink
Add comment-key input (#82)
Browse files Browse the repository at this point in the history
* Add comment-key input

* Fix typo

Co-authored-by: Bruno Bodian <[email protected]>

* Prevent mixing up comments with and without keys

* Add docs to readme for the comment-key option

* Apply suggestions from code review

---------

Co-authored-by: Bruno Bodian <[email protected]>
Co-authored-by: Ryan Christian <[email protected]>
  • Loading branch information
3 people committed Sep 27, 2024
1 parent 68c0fd1 commit ddbf3d9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,32 @@ By default, files are compared after gzip compression, but it's possible to use
```yaml
compression: "none"
```

### 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
name: Compressed Size
on: [pull_request]
jobs:
modern-bundle-size-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: preactjs/compressed-size-action@v2
with:
build-script: "build:modern"
+ comment-key: modern
legacy-bundle-size-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: preactjs/compressed-size-action@v2
with:
build-script: "build:legacy"
+ comment-key: legacy
```

If you do not provide this key, the action will attempt to use (and therefore replace) the same comment for both bundles, hiding the output for whichever run finished last.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ inputs:
default: '{**/*.map,**/node_modules/**}'
cwd:
description: 'A custom working directory to execute the action in relative to repo root (defaults to .)'
comment-key:
description: 'Optional key to include in the bot comment to allow for multiple bundle calculations to be posted in separate comments.'

runs:
using: 'node20'
main: 'index.js'
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,13 @@ async function run(octokit, context, token) {
issue_number: pull_number
};

const commentKey = getInput('comment-key')

const comment = {
...commentInfo,
body:
markdownDiff +
'\n\n<a href="https://github.com/preactjs/compressed-size-action"><sub>compressed-size-action</sub></a>'
`\n\n<a href="https://github.com/preactjs/compressed-size-action"><sub>compressed-size-action${commentKey ? `::${commentKey}` : ''}</sub></a>`
};

if (context.eventName !== 'pull_request' && context.eventName !== 'pull_request_target') {
Expand All @@ -213,9 +215,10 @@ async function run(octokit, context, token) {
let commentId;
try {
const comments = (await octokit.issues.listComments(commentInfo)).data;
const commentRegExp = new RegExp(`<sub>[\s\n]*(compressed|gzip)-size-action${commentKey ? `::${commentKey}` : ''}</sub>`)
for (let i = comments.length; i--; ) {
const c = comments[i];
if (/<sub>[\s\n]*(compressed|gzip)-size-action/.test(c.body)) {
if (commentRegExp.test(c.body)) {
commentId = c.id;
break;
}
Expand Down

0 comments on commit ddbf3d9

Please sign in to comment.