|
| 1 | +const fs = require("node:fs"); |
| 2 | + |
| 3 | +/** |
| 4 | + * @param {import("@octokit/rest")} github |
| 5 | + */ |
| 6 | +module.exports = async function action({ github, context }) { |
| 7 | + const commits = await github.rest.repos.listCommits({ |
| 8 | + owner: context.repo.owner, |
| 9 | + repo: context.repo.repo, |
| 10 | + per_page: 30 |
| 11 | + }); |
| 12 | + |
| 13 | + let baseSize = 0; |
| 14 | + let baseCommit = null; |
| 15 | + |
| 16 | + for (const commit of commits.data) { |
| 17 | + console.log(commit.sha); |
| 18 | + try { |
| 19 | + const data = await fetchDataBySha(commit.sha); |
| 20 | + if (data?.size) { |
| 21 | + baseCommit = commit; |
| 22 | + baseSize = data.size; |
| 23 | + console.log(`Commit ${commit.sha} has binary size: ${data.size}`); |
| 24 | + break; |
| 25 | + } |
| 26 | + } catch (e) { |
| 27 | + console.log(e); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + if (!baseCommit) { |
| 32 | + const error = `No base binary size found within ${commits.data.length} commits`; |
| 33 | + console.log(error); |
| 34 | + throw new Error(error); |
| 35 | + } |
| 36 | + |
| 37 | + const headSize = fs.statSync( |
| 38 | + "./crates/node_binding/rspack.linux-x64-gnu.node" |
| 39 | + ).size; |
| 40 | + |
| 41 | + const comment = compareBinarySize(headSize, baseSize, baseCommit); |
| 42 | + |
| 43 | + await commentToPullRequest(github, context, comment); |
| 44 | +}; |
| 45 | + |
| 46 | +async function commentToPullRequest(github, context, comment) { |
| 47 | + const { data: comments } = await github.rest.issues.listComments({ |
| 48 | + owner: context.repo.owner, |
| 49 | + repo: context.repo.repo, |
| 50 | + issue_number: context.payload.number |
| 51 | + }); |
| 52 | + |
| 53 | + const prevComment = comments.filter( |
| 54 | + comment => |
| 55 | + comment.user.login === "github-actions[bot]" && |
| 56 | + comment.body.startsWith(SIZE_LIMIT_HEADING) |
| 57 | + )[0]; |
| 58 | + |
| 59 | + if (prevComment) { |
| 60 | + await github.rest.issues.updateComment({ |
| 61 | + owner: context.repo.owner, |
| 62 | + repo: context.repo.repo, |
| 63 | + comment_id: prevComment.id, |
| 64 | + body: `${SIZE_LIMIT_HEADING}\n${comment}` |
| 65 | + }); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + await github.rest.issues.createComment({ |
| 70 | + owner: context.repo.owner, |
| 71 | + repo: context.repo.repo, |
| 72 | + issue_number: context.payload.number, |
| 73 | + body: `${SIZE_LIMIT_HEADING}\n${comment}` |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +function fetchDataBySha(sha) { |
| 78 | + const dataUrl = `${DATA_URL_BASE}/commits/${sha.slice(0, 2)}/${sha.slice(2)}/rspack-build.json`; |
| 79 | + console.log("fetching", dataUrl, "..."); |
| 80 | + return fetch(dataUrl).then(res => res.json()); |
| 81 | +} |
| 82 | + |
| 83 | +const SIZE_LIMIT_HEADING = "## 📦 Binary Size-limit"; |
| 84 | + |
| 85 | +const DATA_URL_BASE = |
| 86 | + "https://raw.githubusercontent.com/web-infra-dev/rspack-ecosystem-benchmark/data"; |
| 87 | + |
| 88 | +function compareBinarySize(headSize, baseSize, baseCommit) { |
| 89 | + const message = baseCommit.commit.message.split("\n")[0]; |
| 90 | + const author = baseCommit.commit.author.name; |
| 91 | + |
| 92 | + const info = `> Comparing binary size with Commit: [${message} by ${author}](${baseCommit.html_url})\n\n`; |
| 93 | + |
| 94 | + const diff = headSize - baseSize; |
| 95 | + const percentage = (Math.abs(diff / baseSize) * 100).toFixed(2); |
| 96 | + if (diff > 0) { |
| 97 | + return `${info}❌ Size increased by ${toHumanReadable(diff)} from ${toHumanReadable(baseSize)} to ${toHumanReadable(headSize)} (⬆️${percentage}%)`; |
| 98 | + } |
| 99 | + if (diff < 0) { |
| 100 | + return `${info}🎉 Size decreased by ${toHumanReadable(-diff)} from ${toHumanReadable(baseSize)} to ${toHumanReadable(headSize)} (⬇️${percentage}%)`; |
| 101 | + } |
| 102 | + return `${info}🙈 Size remains the same at ${toHumanReadable(headSize)}`; |
| 103 | +} |
| 104 | + |
| 105 | +function toHumanReadable(size) { |
| 106 | + if (size < 1024) { |
| 107 | + return `${size}bytes`; |
| 108 | + } |
| 109 | + if (size < 1024 * 1024) { |
| 110 | + return `${(size / 1024).toFixed(2)}KB`; |
| 111 | + } |
| 112 | + return `${(size / 1024 / 1024).toFixed(2)}MB`; |
| 113 | +} |
0 commit comments