Skip to content

Commit ad1e0d8

Browse files
committed
ci: split out comment into workflow_run
1 parent da29084 commit ad1e0d8

File tree

2 files changed

+112
-22
lines changed

2 files changed

+112
-22
lines changed

.github/workflows/size-comment.yml

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
name: size-comment
2+
3+
on:
4+
workflow_run:
5+
workflows: [size]
6+
types: [completed]
7+
8+
permissions:
9+
pull-requests: write
10+
actions: read
11+
12+
jobs:
13+
comment:
14+
# Only run if the build workflow succeeded
15+
if: github.event.workflow_run.conclusion == 'success'
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: 🔍 Get PR number
19+
id: pr
20+
uses: actions/github-script@v8
21+
with:
22+
script: |
23+
const pulls = await github.rest.repos.listPullRequestsAssociatedWithCommit({
24+
owner: context.repo.owner,
25+
repo: context.repo.repo,
26+
commit_sha: context.payload.workflow_run.head_sha,
27+
});
28+
29+
if (pulls.data.length > 0) {
30+
return pulls.data[0].number;
31+
}
32+
return null;
33+
34+
- name: ⏬ Download artifacts from workflow run
35+
if: steps.pr.outputs.result
36+
uses: actions/download-artifact@v5
37+
with:
38+
github-token: ${{ secrets.GITHUB_TOKEN }}
39+
run-id: ${{ github.event.workflow_run.id }}
40+
41+
- name: 📊 Compare and comment on sizes
42+
if: steps.pr.outputs.result
43+
uses: actions/github-script@v8
44+
with:
45+
script: |
46+
const fs = require('fs');
47+
const path = require('path');
48+
const prNumber = ${{ steps.pr.outputs.result }};
49+
50+
if (!prNumber) {
51+
console.log('No PR number found, skipping comment');
52+
return;
53+
}
54+
55+
const packages = ['nuxi', 'nuxt-cli', 'create-nuxt'];
56+
let commentBody = '## 📦 Bundle Size Comparison\n\n';
57+
58+
for (const pkg of packages) {
59+
try {
60+
const headStats = JSON.parse(fs.readFileSync(`./head-stats/${pkg}/stats.json`, 'utf8'));
61+
const baseStats = JSON.parse(fs.readFileSync(`./base-stats/${pkg}/stats.json`, 'utf8'));
62+
63+
// Simple size comparison - adjust based on your stats.json structure
64+
const headSize = headStats.bundleSize || headStats.size || 0;
65+
const baseSize = baseStats.bundleSize || baseStats.size || 0;
66+
const diff = headSize - baseSize;
67+
const diffPercent = baseSize ? ((diff / baseSize) * 100).toFixed(2) : 0;
68+
69+
const icon = diff > 0 ? '📈' : diff < 0 ? '📉' : '➡️';
70+
const sign = diff > 0 ? '+' : '';
71+
72+
commentBody += `### ${icon} ${pkg}\n`;
73+
commentBody += `- Base: ${(baseSize / 1024).toFixed(2)} KB\n`;
74+
commentBody += `- Head: ${(headSize / 1024).toFixed(2)} KB\n`;
75+
commentBody += `- Diff: ${sign}${(diff / 1024).toFixed(2)} KB (${sign}${diffPercent}%)\n\n`;
76+
} catch (error) {
77+
console.log(`Error processing ${pkg}:`, error.message);
78+
commentBody += `### ⚠️ ${pkg}\nCould not compare sizes\n\n`;
79+
}
80+
}
81+
82+
// Find existing comment
83+
const comments = await github.rest.issues.listComments({
84+
owner: context.repo.owner,
85+
repo: context.repo.repo,
86+
issue_number: prNumber,
87+
});
88+
89+
const botComment = comments.data.find(comment =>
90+
comment.user.type === 'Bot' &&
91+
comment.body.includes('Bundle Size Comparison')
92+
);
93+
94+
if (botComment) {
95+
// Update existing comment
96+
await github.rest.issues.updateComment({
97+
owner: context.repo.owner,
98+
repo: context.repo.repo,
99+
comment_id: botComment.id,
100+
body: commentBody
101+
});
102+
} else {
103+
// Create new comment
104+
await github.rest.issues.createComment({
105+
owner: context.repo.owner,
106+
repo: context.repo.repo,
107+
issue_number: prNumber,
108+
body: commentBody
109+
});
110+
}

.github/workflows/size.yml

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: size
2+
23
on:
3-
# this action will error unless run in a pr context
4-
pull_request_target:
4+
pull_request:
55
branches:
66
- main
77

@@ -66,23 +66,3 @@ jobs:
6666
with:
6767
name: base-stats
6868
path: ./packages/*/stats.json
69-
70-
# run the action against the stats.json files
71-
compare:
72-
runs-on: ubuntu-latest
73-
needs: [build-base, build-head]
74-
permissions:
75-
pull-requests: write
76-
issues: write
77-
strategy:
78-
matrix:
79-
package: [nuxi, nuxt-cli, create-nuxt]
80-
steps:
81-
- name: ⏬ Download stats.json
82-
uses: actions/download-artifact@v5
83-
- uses: twk3/rollup-size-compare-action@v1.2.0
84-
with:
85-
title: ${{ matrix.package }} size comparison
86-
github-token: ${{ secrets.GITHUB_TOKEN }}
87-
current-stats-json-path: ./head-stats/${{ matrix.package }}/stats.json
88-
base-stats-json-path: ./base-stats/${{ matrix.package }}/stats.json

0 commit comments

Comments
 (0)