|
| 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 | + } |
0 commit comments