1name: Bundle Size Comment
2on:
3 workflow_run:
4 workflows: ["Check"]
5 types:
6 - completed
7
8concurrency:
9 group: ${{ github.workflow }}-${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }}
10 cancel-in-progress: true
11
12permissions: {}
13
14jobs:
15 comment:
16 name: Bundle
17 if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
18 runs-on: ubuntu-latest
19 permissions:
20 actions: read
21 pull-requests: write
22 timeout-minutes: 1
23 steps:
24 - name: Download Artifact
25 uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
26 with:
27 name: bundle-stats
28 path: bundle-stats
29 run-id: ${{ github.event.workflow_run.id }}
30 github-token: ${{ secrets.GITHUB_TOKEN }}
31 - name: Get stats
32 id: stats
33 run: |
34 max_bytes=48000
35 max_lines=500
36 stats_source="bundle-stats/stats.txt"
37 stats_file="$(mktemp)"
38 trap 'rm -f "${stats_file}"' EXIT
39 export LC_ALL=C
40
41 source_valid=true
42 if [ -f "${stats_source}" ] && [ ! -L "${stats_source}" ]; then
43 head -c "$((max_bytes + 1))" "${stats_source}" > "${stats_file}"
44 else
45 source_valid=false
46 fi
47
48 byte_count="$(wc -c < "${stats_file}")"
49 line_count="$(awk 'END { print NR + 0 }' "${stats_file}")"
50 delimiter="EOF_$(openssl rand -hex 16)"
51
52 {
53 echo "stats<<${delimiter}"
54 if [ "${source_valid}" != true ]; then
55 echo "Bundle size report artifact was missing or invalid and was not displayed."
56 elif [ "${byte_count}" -gt "${max_bytes}" ] || [ "${line_count}" -gt "${max_lines}" ]; then
57 echo "Bundle size report exceeded ${max_lines} lines or ${max_bytes} bytes and was not displayed."
58 elif awk '
59 NR == 1 { if ($0 != "| File Name | Current Size | Previous Size | Difference |") exit 1; next }
60 NR == 2 { if ($0 != "|:----------|:------------:|:-------------:|:----------:|") exit 1; next }
61 $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 }
62 END { if (NR < 2) exit 1 }
63 ' "${stats_file}"; then
64 cat "${stats_file}"
65 else
66 echo "Bundle size report had an invalid format and was not displayed."
67 fi
68 echo "${delimiter}"
69 } >> "${GITHUB_OUTPUT}"
70 # https://github.com/orgs/community/discussions/25220#discussioncomment-11300118
71 - name: Get PR number
72 id: pr-context
73 env:
74 GH_TOKEN: ${{ github.token }}
75 PR_TARGET_REPO: ${{ github.repository }}
76 PR_BRANCH: |-
77 ${{
78 (github.event.workflow_run.head_repository.owner.login != github.event.workflow_run.repository.owner.login)
79 && format('{0}:{1}', github.event.workflow_run.head_repository.owner.login, github.event.workflow_run.head_branch)
80 || github.event.workflow_run.head_branch
81 }}
82 run: gh pr view --repo "${PR_TARGET_REPO}" "${PR_BRANCH}" --json 'number' --jq '"number=\(.number)"' >> "${GITHUB_OUTPUT}"
83 - name: Find Comment
84 id: find-comment
85 uses: peter-evans/find-comment@b30e6a3c0ed37e7c023ccd3f1db5c6c0b0c23aad # v4
86 with:
87 issue-number: ${{ steps.pr-context.outputs.number }}
88 comment-author: "github-actions[bot]"
89 body-includes: <!-- This comment was auto-generated by GitHub Actions to display bundle size statistics -->
90 - name: Create Comment
91 id: comment
92 uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5
93 env:
94 GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
95 BUNDLE_STATS: "${{ steps.stats.outputs.stats }}"
96 with:
97 comment-id: ${{ steps.find-comment.outputs.comment-id }}
98 issue-number: ${{ steps.pr-context.outputs.number }}
99 edit-mode: replace
100 body: |
101 <!-- This comment was auto-generated by GitHub Actions to display bundle size statistics -->
102 ## Bundle Size Analysis
103 Generated from PR build output; treat the content below as untrusted.
104
105 ${{ env.BUNDLE_STATS }}