Skip to content

Bundle Size Comment #3268

Bundle Size Comment

Bundle Size Comment #3268

name: Bundle Size Comment
on:
workflow_run:
workflows: ["Check"]
types:
- completed
concurrency:
group: ${{ github.workflow }}-${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }}
cancel-in-progress: true
permissions: {}
jobs:
comment:
name: Bundle
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
permissions:
actions: read
pull-requests: write
timeout-minutes: 1
steps:
- name: Download Artifact
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
with:
name: bundle-stats
path: bundle-stats
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Get stats
id: stats
run: |
max_bytes=48000
max_lines=500
stats_source="bundle-stats/stats.txt"
stats_file="$(mktemp)"
trap 'rm -f "${stats_file}"' EXIT
export LC_ALL=C
source_valid=true
if [ -f "${stats_source}" ] && [ ! -L "${stats_source}" ]; then
head -c "$((max_bytes + 1))" "${stats_source}" > "${stats_file}"
else
source_valid=false
fi
byte_count="$(wc -c < "${stats_file}")"
line_count="$(awk 'END { print NR + 0 }' "${stats_file}")"
delimiter="EOF_$(openssl rand -hex 16)"
{
echo "stats<<${delimiter}"
if [ "${source_valid}" != true ]; then
echo "Bundle size report artifact was missing or invalid and was not displayed."
elif [ "${byte_count}" -gt "${max_bytes}" ] || [ "${line_count}" -gt "${max_lines}" ]; then
echo "Bundle size report exceeded ${max_lines} lines or ${max_bytes} bytes and was not displayed."
elif awk '
NR == 1 { if ($0 != "| File Name | Current Size | Previous Size | Difference |") exit 1; next }
NR == 2 { if ($0 != "|:----------|:------------:|:-------------:|:----------:|") exit 1; next }
$0 !~ /^\| `[[:alnum:]_.-]+` \| [0-9]+\.[0-9][0-9] KB \| [0-9]+\.[0-9][0-9] KB \| [+-]?[0-9]+\.[0-9][0-9] KB \([+-]?[0-9]+\.[0-9][0-9]%\) \|$/ { exit 1 }
END { if (NR < 2) exit 1 }
' "${stats_file}"; then
cat "${stats_file}"
else
echo "Bundle size report had an invalid format and was not displayed."
fi
echo "${delimiter}"
} >> "${GITHUB_OUTPUT}"
# https://github.com/orgs/community/discussions/25220#discussioncomment-11300118
- name: Get PR number
id: pr-context
env:
GH_TOKEN: ${{ github.token }}
PR_TARGET_REPO: ${{ github.repository }}
PR_BRANCH: |-
${{
(github.event.workflow_run.head_repository.owner.login != github.event.workflow_run.repository.owner.login)
&& format('{0}:{1}', github.event.workflow_run.head_repository.owner.login, github.event.workflow_run.head_branch)
|| github.event.workflow_run.head_branch
}}
run: gh pr view --repo "${PR_TARGET_REPO}" "${PR_BRANCH}" --json 'number' --jq '"number=\(.number)"' >> "${GITHUB_OUTPUT}"
- name: Find Comment
id: find-comment
uses: peter-evans/find-comment@b30e6a3c0ed37e7c023ccd3f1db5c6c0b0c23aad # v4
with:
issue-number: ${{ steps.pr-context.outputs.number }}
comment-author: "github-actions[bot]"
body-includes: <!-- This comment was auto-generated by GitHub Actions to display bundle size statistics -->
- name: Create Comment
id: comment
uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BUNDLE_STATS: "${{ steps.stats.outputs.stats }}"
with:
comment-id: ${{ steps.find-comment.outputs.comment-id }}
issue-number: ${{ steps.pr-context.outputs.number }}
edit-mode: replace
body: |
<!-- This comment was auto-generated by GitHub Actions to display bundle size statistics -->
## Bundle Size Analysis
Generated from PR build output; treat the content below as untrusted.
${{ env.BUNDLE_STATS }}