|
1 | 1 | name: bundle-size-comment |
2 | 2 |
|
| 3 | +# Runs from the default branch once `bundle-size` completes, also for fork PRs. The runs it reads come from the PR's |
| 4 | +# build and are only parsed by nuxt-bundle-analyzer, never executed. |
3 | 5 | on: |
4 | 6 | workflow_run: |
5 | 7 | workflows: |
|
10 | 12 | permissions: |
11 | 13 | actions: read |
12 | 14 | contents: read |
13 | | - issues: write |
14 | | - pull-requests: read |
| 15 | + # Proves to bundle.nuxt.dev that the upload comes from this workflow on the default branch. |
| 16 | + id-token: write |
| 17 | + |
| 18 | +env: |
| 19 | + NUXT_BUNDLE_ANALYZER_VERSION: 0.1.7 |
15 | 20 |
|
16 | 21 | jobs: |
17 | 22 | comment: |
18 | 23 | if: github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'pull_request' |
19 | | - runs-on: ubuntu-latest |
| 24 | + runs-on: ubuntu-slim |
20 | 25 | timeout-minutes: 10 |
21 | 26 | steps: |
22 | | - - name: Validate triggering workflow |
23 | | - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
24 | | - with: |
25 | | - script: | |
26 | | - const run = context.payload.workflow_run |
27 | | - const shaPattern = /^[0-9a-f]{40}$/ |
28 | | -
|
29 | | - if (run.name !== 'bundle-size') { |
30 | | - core.setFailed('The triggering workflow name is invalid.') |
31 | | - return |
32 | | - } |
33 | | -
|
34 | | - if (!Number.isSafeInteger(run.id) || run.id <= 0 || !shaPattern.test(run.head_sha) || !Number.isSafeInteger(run.head_repository?.id) || run.head_repository.id <= 0) { |
35 | | - core.setFailed('The workflow payload contains invalid head repository metadata.') |
36 | | - return |
37 | | - } |
38 | | - - name: Resolve bundle artifact |
39 | | - id: artifact |
40 | | - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 27 | + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7 |
41 | 28 | with: |
42 | | - script: | |
43 | | - const run = context.payload.workflow_run |
44 | | - const artifacts = await github.paginate(github.rest.actions.listWorkflowRunArtifacts, { |
45 | | - owner: context.repo.owner, |
46 | | - repo: context.repo.repo, |
47 | | - run_id: run.id, |
48 | | - per_page: 100 |
49 | | - }) |
50 | | - const matches = artifacts.filter(artifact => artifact.name === 'bundle-size-report' && !artifact.expired) |
51 | | -
|
52 | | - if (matches.length !== 1) { |
53 | | - core.setFailed('Expected exactly one bundle report artifact from the triggering run.') |
54 | | - return |
55 | | - } |
56 | | -
|
57 | | - const artifact = matches[0] |
58 | | - if (!Number.isSafeInteger(artifact.id) || artifact.id <= 0 || !Number.isSafeInteger(artifact.size_in_bytes) || artifact.size_in_bytes <= 0 || artifact.size_in_bytes > 12 * 1024 * 1024) { |
59 | | - core.setFailed('The bundle report artifact metadata is invalid or too large.') |
60 | | - return |
61 | | - } |
62 | | -
|
63 | | - core.setOutput('id', String(artifact.id)) |
64 | | - - name: Download untrusted bundle snapshots |
65 | | - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 |
| 29 | + node-version: lts/-1 |
| 30 | + - name: Download the runs |
| 31 | + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 |
66 | 32 | with: |
67 | | - artifact-ids: ${{ steps.artifact.outputs.id }} |
68 | | - path: ${{ runner.temp }}/bundle-size-report |
69 | | - merge-multiple: true |
70 | | - github-token: ${{ secrets.GITHUB_TOKEN }} |
| 33 | + path: runs |
71 | 34 | run-id: ${{ github.event.workflow_run.id }} |
72 | | - - name: Resolve triggering pull request |
73 | | - id: pull |
74 | | - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 35 | + github-token: ${{ github.token }} |
| 36 | + # The comment is still posted without the comparison link when this fails. |
| 37 | + - name: Upload the runs to bundle.nuxt.dev |
| 38 | + id: upload |
| 39 | + continue-on-error: true |
| 40 | + run: npx --yes "nuxt-bundle-analyzer@$NUXT_BUNDLE_ANALYZER_VERSION" upload --runs runs --url https://bundle.nuxt.dev |
75 | 41 | env: |
76 | | - BUNDLE_CONTEXT_PATH: ${{ runner.temp }}/bundle-size-report/context.json |
77 | | - with: |
78 | | - script: | |
79 | | - const fs = require('node:fs') |
80 | | - const path = require('node:path') |
81 | | - const run = context.payload.workflow_run |
82 | | - const repository = context.payload.repository |
83 | | - const candidates = run.pull_requests || [] |
84 | | - const contextPath = process.env.BUNDLE_CONTEXT_PATH |
85 | | - const shaPattern = /^[0-9a-f]{40}$/ |
86 | | -
|
87 | | - core.setOutput('should-comment', 'false') |
88 | | -
|
89 | | - const artifactDirectory = path.dirname(contextPath) |
90 | | - const files = fs.readdirSync(artifactDirectory).sort() |
91 | | - if (files.length !== 3 || files[0] !== 'base.json' || files[1] !== 'context.json' || files[2] !== 'pr.json') { |
92 | | - core.setFailed('The bundle artifact contains unexpected files.') |
93 | | - return |
94 | | - } |
95 | | -
|
96 | | - const contextStats = fs.lstatSync(contextPath) |
97 | | - const baseStats = fs.lstatSync(path.join(artifactDirectory, 'base.json')) |
98 | | - const headStats = fs.lstatSync(path.join(artifactDirectory, 'pr.json')) |
99 | | - if (!contextStats.isFile() || contextStats.isSymbolicLink() || contextStats.size > 1_024 || |
100 | | - !baseStats.isFile() || baseStats.isSymbolicLink() || baseStats.size > 5 * 1024 * 1024 || |
101 | | - !headStats.isFile() || headStats.isSymbolicLink() || headStats.size > 5 * 1024 * 1024) { |
102 | | - core.setFailed('The bundle artifact does not contain bounded regular files.') |
103 | | - return |
104 | | - } |
105 | | -
|
106 | | - const bundleContext = JSON.parse(fs.readFileSync(contextPath, 'utf8')) |
107 | | - const contextKeys = Object.keys(bundleContext) |
108 | | - if (contextKeys.length !== 1 || contextKeys[0] !== 'prNumber' || !Number.isSafeInteger(bundleContext.prNumber) || bundleContext.prNumber <= 0) { |
109 | | - core.setFailed('The bundle context contains an invalid pull request number.') |
110 | | - return |
111 | | - } |
112 | | -
|
113 | | - if (candidates.length > 1 || (candidates.length === 1 && candidates[0].number !== bundleContext.prNumber)) { |
114 | | - core.setFailed('The bundle context does not match the workflow payload.') |
115 | | - return |
116 | | - } |
117 | | -
|
118 | | - const { data: pull } = await github.rest.pulls.get({ |
119 | | - owner: context.repo.owner, |
120 | | - repo: context.repo.repo, |
121 | | - pull_number: bundleContext.prNumber |
122 | | - }) |
123 | | -
|
124 | | - if (pull.state !== 'open' || pull.head.sha !== run.head_sha) { |
125 | | - core.info(`Skipping stale or closed PR #${bundleContext.prNumber}.`) |
126 | | - return |
127 | | - } |
128 | | -
|
129 | | - if (pull.head.repo?.id !== run.head_repository.id) { |
130 | | - core.setFailed('The pull request head repository does not match the triggering workflow.') |
131 | | - return |
132 | | - } |
133 | | -
|
134 | | - if (pull.base.repo.id !== repository.id || pull.base.ref !== repository.default_branch || !shaPattern.test(pull.base.sha)) { |
135 | | - core.setFailed('The pull request does not target the expected default branch.') |
136 | | - return |
137 | | - } |
138 | | -
|
139 | | - core.setOutput('should-comment', 'true') |
140 | | - core.setOutput('number', String(bundleContext.prNumber)) |
141 | | - core.setOutput('base-sha', pull.base.sha) |
142 | | - core.setOutput('head-sha', run.head_sha) |
143 | | - - name: Check out the trusted reporter |
144 | | - if: steps.pull.outputs.should-comment == 'true' |
145 | | - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 |
146 | | - with: |
147 | | - persist-credentials: false |
148 | | - ref: ${{ github.sha }} |
149 | | - - if: steps.pull.outputs.should-comment == 'true' |
150 | | - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7 |
151 | | - with: |
152 | | - node-version: lts/-1 |
153 | | - - name: Validate snapshots and render report |
154 | | - if: steps.pull.outputs.should-comment == 'true' |
155 | | - env: |
156 | | - BASE_SHA: ${{ steps.pull.outputs.base-sha }} |
157 | | - BASE_SNAPSHOT: ${{ runner.temp }}/bundle-size-report/base.json |
158 | | - HEAD_SHA: ${{ steps.pull.outputs.head-sha }} |
159 | | - HEAD_SNAPSHOT: ${{ runner.temp }}/bundle-size-report/pr.json |
160 | | - REPORT_PATH: ${{ runner.temp }}/bundle-size-comment.md |
161 | | - run: | |
162 | | - node scripts/bundle-size/report.mjs compare \ |
163 | | - --base "$BASE_SNAPSHOT" \ |
164 | | - --head "$HEAD_SNAPSHOT" \ |
165 | | - --base-sha "$BASE_SHA" \ |
166 | | - --head-sha "$HEAD_SHA" \ |
167 | | - --output "$REPORT_PATH" |
| 42 | + GITHUB_TOKEN: ${{ github.token }} |
| 43 | + # GitHub makes `GITHUB_TOKEN` read-only in run chains that start from a fork's PR, so the app comments. |
168 | 44 | - name: Mint bundle report app token |
169 | | - if: steps.pull.outputs.should-comment == 'true' |
170 | 45 | id: app-token |
171 | | - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 |
| 46 | + uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 |
172 | 47 | with: |
173 | 48 | app-id: ${{ secrets.BUNDLE_REPORT_APP_ID }} |
174 | 49 | private-key: ${{ secrets.BUNDLE_REPORT_APP_PRIVATE_KEY }} |
175 | | - - name: Update PR comment |
176 | | - if: steps.pull.outputs.should-comment == 'true' |
177 | | - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 50 | + # What `comment --post` calls: it lists the open PRs, then creates or updates its comment. |
| 51 | + permission-issues: write |
| 52 | + permission-pull-requests: read |
| 53 | + - name: Comment on the PR |
| 54 | + run: npx --yes "nuxt-bundle-analyzer@$NUXT_BUNDLE_ANALYZER_VERSION" comment --runs runs --post --link "$LINK" |
178 | 55 | env: |
179 | | - BUNDLE_REPORT_PATH: ${{ runner.temp }}/bundle-size-comment.md |
180 | | - PR_NUMBER: ${{ steps.pull.outputs.number }} |
181 | | - with: |
182 | | - github-token: ${{ steps.app-token.outputs.token }} |
183 | | - script: | |
184 | | - const fs = require('node:fs') |
185 | | - const marker = '<!-- nuxt.com-bundle-size-report -->' |
186 | | - const run = context.payload.workflow_run |
187 | | - const prNumber = Number(process.env.PR_NUMBER) |
188 | | -
|
189 | | - if (!Number.isSafeInteger(prNumber) || prNumber <= 0) { |
190 | | - core.setFailed('The validated pull request number is invalid.') |
191 | | - return |
192 | | - } |
193 | | -
|
194 | | - const report = fs.readFileSync(process.env.BUNDLE_REPORT_PATH, 'utf8') |
195 | | - if (Buffer.byteLength(report, 'utf8') > 60_000) { |
196 | | - core.setFailed('The bundle report is too large to post safely.') |
197 | | - return |
198 | | - } |
199 | | -
|
200 | | - const body = `${marker}\n${report}\n[Workflow run](${run.html_url})` |
201 | | -
|
202 | | - try { |
203 | | - const comments = await github.paginate(github.rest.issues.listComments, { |
204 | | - owner: context.repo.owner, |
205 | | - repo: context.repo.repo, |
206 | | - issue_number: prNumber, |
207 | | - per_page: 100 |
208 | | - }) |
209 | | - const previous = comments.find(comment => comment.user?.login?.endsWith('[bot]') && comment.body?.includes(marker)) |
210 | | -
|
211 | | - if (previous) { |
212 | | - await github.rest.issues.updateComment({ |
213 | | - owner: context.repo.owner, |
214 | | - repo: context.repo.repo, |
215 | | - comment_id: previous.id, |
216 | | - body |
217 | | - }) |
218 | | - } else { |
219 | | - await github.rest.issues.createComment({ |
220 | | - owner: context.repo.owner, |
221 | | - repo: context.repo.repo, |
222 | | - issue_number: prNumber, |
223 | | - body |
224 | | - }) |
225 | | - } |
226 | | - } catch (error) { |
227 | | - core.warning(`Unable to post the bundle report comment: ${error.message}`) |
228 | | - await core.summary.addRaw(report).write() |
229 | | - core.info('The bundle report was written to the workflow run summary instead.') |
230 | | - } |
| 56 | + GITHUB_TOKEN: ${{ steps.app-token.outputs.token }} |
| 57 | + LINK: ${{ steps.upload.outputs.url }} |
0 commit comments