Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add comment-key input #82

Merged
merged 7 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading