|
| 1 | +const THRESHOLD = 0.15; |
| 2 | +const MARKER = '<!-- bundle-size-limits-comment -->'; |
| 3 | + |
| 4 | +const getContent = async ({ github, context }, ref) => { |
| 5 | + const { data } = await github.rest.repos.getContent({ |
| 6 | + owner: context.repo.owner, |
| 7 | + repo: context.repo.repo, |
| 8 | + path: 'packages/kbn-optimizer/limits.yml', |
| 9 | + ref, |
| 10 | + }); |
| 11 | + return Buffer.from(data.content, 'base64').toString('utf8'); |
| 12 | +}; |
| 13 | + |
| 14 | +const parseYaml = (content) => { |
| 15 | + const result = {}; |
| 16 | + for (const line of content.split('\n')) { |
| 17 | + const match = line.match(/^\s{2}(\w+):\s*(\d+)/); |
| 18 | + if (match) result[match[1]] = parseInt(match[2], 10); |
| 19 | + } |
| 20 | + return result; |
| 21 | +}; |
| 22 | + |
| 23 | +module.exports = async ({ github, context }) => { |
| 24 | + const pr = context.payload.pull_request; |
| 25 | + |
| 26 | + const [baseContent, headContent] = await Promise.all([ |
| 27 | + getContent({ github, context }, pr.base.sha), |
| 28 | + getContent({ github, context }, pr.head.sha), |
| 29 | + ]); |
| 30 | + |
| 31 | + const baseMap = parseYaml(baseContent); |
| 32 | + const headMap = parseYaml(headContent); |
| 33 | + |
| 34 | + const bigIncreases = []; |
| 35 | + for (const [plugin, headSize] of Object.entries(headMap)) { |
| 36 | + const baseSize = baseMap[plugin]; |
| 37 | + if (baseSize != null && headSize > baseSize) { |
| 38 | + const pct = (headSize - baseSize) / baseSize; |
| 39 | + if (pct >= THRESHOLD) { |
| 40 | + bigIncreases.push({ plugin, baseSize, headSize, pct }); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + const { data: comments } = await github.rest.issues.listComments({ |
| 46 | + owner: context.repo.owner, |
| 47 | + repo: context.repo.repo, |
| 48 | + issue_number: context.issue.number, |
| 49 | + }); |
| 50 | + const existing = comments.find((c) => c.body && c.body.includes(MARKER)); |
| 51 | + |
| 52 | + if (bigIncreases.length === 0) { |
| 53 | + if (existing) { |
| 54 | + await github.rest.issues.deleteComment({ |
| 55 | + owner: context.repo.owner, |
| 56 | + repo: context.repo.repo, |
| 57 | + comment_id: existing.id, |
| 58 | + }); |
| 59 | + } |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + const rows = bigIncreases |
| 64 | + .sort((a, b) => b.pct - a.pct) |
| 65 | + .map( |
| 66 | + ({ plugin, baseSize, headSize, pct }) => |
| 67 | + `| \`${plugin}\` | ${baseSize.toLocaleString()} | ${headSize.toLocaleString()} | +${(pct * 100).toFixed(1)}% |` |
| 68 | + ) |
| 69 | + .join('\n'); |
| 70 | + |
| 71 | + const body = |
| 72 | + `@${pr.user.login}, this PR increases one or more page-load bundle sizes by 15% or more:\n\n` + |
| 73 | + `| Plugin | Before (bytes) | After (bytes) | Change |\n` + |
| 74 | + `|--------|----------------|---------------|--------|\n` + |
| 75 | + `${rows}\n\n` + |
| 76 | + `Large bundle size increases can affect page load performance. Consider whether dependencies can be lazy-loaded or code split to reduce the bundle.\n\n` + |
| 77 | + `See the [bundle optimization guide](https://www.elastic.co/docs/extend/kibana/ci-metrics#ci-metric-resolving-overages) for tips.\n\n` + |
| 78 | + `${MARKER}`; |
| 79 | + |
| 80 | + if (existing) { |
| 81 | + await github.rest.issues.updateComment({ |
| 82 | + owner: context.repo.owner, |
| 83 | + repo: context.repo.repo, |
| 84 | + comment_id: existing.id, |
| 85 | + body, |
| 86 | + }); |
| 87 | + } else { |
| 88 | + await github.rest.issues.createComment({ |
| 89 | + issue_number: context.issue.number, |
| 90 | + owner: context.repo.owner, |
| 91 | + repo: context.repo.repo, |
| 92 | + body, |
| 93 | + }); |
| 94 | + } |
| 95 | +}; |
0 commit comments