Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 58 additions & 9 deletions .github/actions/binary-limit/binary-limit-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ module.exports = async function action({ github, context, limit }) {

let baseCommit;
let baseSize;
let unpublishedBase;
try {
({ baseCommit, baseSize } = await findBaseCommit(github, context));
({ baseCommit, baseSize, unpublishedBase } = await findBaseCommit(
github,
context,
));
} catch (e) {
if (e instanceof PendingBinaryDataError) {
await tryComment(
Expand All @@ -27,11 +31,11 @@ module.exports = async function action({ github, context, limit }) {

console.log(`Base commit size: ${baseSize}`);

await tryComment(
github,
context,
compareBinarySize(headSize, baseSize, context, baseCommit),
);
let comment = compareBinarySize(headSize, baseSize, context, baseCommit);
if (unpublishedBase) {
comment += stackedBaseNote(unpublishedBase);
}
await tryComment(github, context, comment);

const increasedSize = headSize - baseSize;
if (increasedSize > limit) {
Expand Down Expand Up @@ -87,11 +91,21 @@ async function findBaseCommit(github, context) {
if (pendingBase) {
const data = await fetchDataBySha(github, commit.sha);
if (data?.size) {
console.log(`Fallback reference ${commit.sha}: ${data.size}`);
throw new PendingBinaryDataError(pendingBase, {
console.log(`Nearest published ancestor ${commit.sha}: ${data.size}`);
if (await dataWillBePublished(github, owner, repo, pendingBase.sha)) {
throw new PendingBinaryDataError(pendingBase, {
baseCommit: commit,
baseSize: data.size,
});
}
// The true base commit sits on a branch that never publishes size
// data (e.g. this PR is stacked on another PR), so waiting is
// pointless; the nearest published ancestor is the best baseline.
return {
baseCommit: commit,
baseSize: data.size,
});
unpublishedBase: pendingBase,
};
}
continue;
}
Expand Down Expand Up @@ -159,6 +173,31 @@ function isDocFile(filename) {
return filename.endsWith('.md') || filename.startsWith('website/');
}

// Ecosystem-benchmark only uploads size data on pushes to these branches
// (`on.push.branches` plus the upload step's `if: github.event_name == 'push'`),
// so data for a commit not reachable from one of them will never appear.
const DATA_PUBLISHING_BRANCHES = ['main', 'v1.x'];

async function dataWillBePublished(github, owner, repo, sha) {
for (const branch of DATA_PUBLISHING_BRANCHES) {
try {
const { data } = await github.rest.repos.compareCommitsWithBasehead({
owner,
repo,
basehead: `${sha}...${branch}`,
per_page: 1,
});
// "identical"/"ahead" means the branch contains the commit.
if (data.status === 'identical' || data.status === 'ahead') {
return true;
}
} catch (e) {
if (e.status !== 404) throw e;
}
}
return false;
}

async function tryComment(github, context, comment) {
try {
await commentToPullRequest(github, context, comment);
Expand Down Expand Up @@ -269,6 +308,16 @@ function referenceComparison(headSize, { baseCommit, baseSize }) {
);
}

function stackedBaseNote(unpublishedBase) {
const shortSha = unpublishedBase.sha.slice(0, 7);
return (
'\n\n> [!NOTE]\n' +
`> The base commit [\`${shortSha}\`](${unpublishedBase.html_url}) is not on a ` +
'branch that publishes binary-size data, so this compares against its nearest ' +
'published ancestor instead.'
);
}

function compareBinarySize(headSize, baseSize, context, baseCommit) {
return comparingInfo(context, baseCommit) + sizeDiffLine(headSize, baseSize);
}
Expand Down
Loading