Skip to content

Commit

Permalink
Wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jackw committed Nov 12, 2024
1 parent 6d40be4 commit f800513
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
31 changes: 19 additions & 12 deletions bundle-size/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
/** @typedef {import('./compareStats').EntryDiffResult} EntryDiffResult */

const prMessageSymbol = `<!-- grafana-plugin-actions-bundle-size-comment -->`;

const prMessageUpdate =
"Whenever this PR is updated, this comment will update to reflect the latest changes.";
const prMessageWelcome = `### Bundle Size Changes
Hello! πŸ‘‹ This comment was generated by a Github Action to help you and reviewers understand the impact of your PR on frontend bundle sizes.
Expand Down Expand Up @@ -72,6 +73,16 @@ function printAssetTableRow(assetDiff) {
)} | ${getPercentageString(assetDiff.diffPercentage)} |`;
}

/**
* @param {string} col1
* @param {string} col2
* @param {string} col3
*/
function printAssetTableHeader(col1 = "Name", col2 = "Size", col3 = "% Diff") {
return `| ${col1} | ${col2} | ${col3} |
| --- | --- | --- |`;
}

/**
* @param {DiffResult} assetDiff
*/
Expand All @@ -85,8 +96,7 @@ function printAssetTables(assetDiff) {
}

return `**${title}**\n\n
| Name | Size | % Diff |
| --- | --- | --- |
${printAssetTableHeader()}
${assets.map((assetDiff) => printAssetTableRow(assetDiff)).join("\n")}
`;
})
Expand All @@ -109,8 +119,7 @@ function printChunkModulesTable(modulesDiff) {
}

return `**${title}**\n\n
| Name | Size | % Diff |
| --- | --- | --- |
${printAssetTableHeader()}
${modules.map((moduleDiff) => printAssetTableRow(moduleDiff)).join("\n")}
`;
})
Expand All @@ -126,14 +135,12 @@ function getComment(assetsDiff, modulesDiff, entriesDiff) {
return `${prMessageSymbol}
${prMessageWelcome}
Whenever this PR is updated, this comment will update to reflect the latest changes.
${prMessageUpdate}
| EntryPoint | Size | % diff |
| --- | --- | --- |
${printAssetTableHeader("EntryPoint")}
${entriesDiff.entries.map((entry) => printAssetTableRow(entry)).join("\n")}
| Files | Total bundle size | % diff |
| --- | --- | --- |
${printAssetTableHeader("Files", "Total bundle size")}
${printAssetTableRow(assetsDiff.total)}
<details>
Expand Down Expand Up @@ -163,9 +170,9 @@ function getBelowThresholdComment(diffPercentage, diffThreshold) {
return `${prMessageSymbol}
${prMessageWelcome}
Bundle size increase of ${diffPercentage}% is now below threshold of ${diffThreshold}%.
Great job! Bundle size increase of ${diffPercentage}% is now below threshold of ${diffThreshold}%.
Whenever this PR is updated, this comment will update to reflect the latest changes.
${prMessageUpdate}
`;
}

Expand Down
11 changes: 7 additions & 4 deletions bundle-size/compareStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,23 @@ function entryStatsDiff(oldEntries, newEntries) {
const diffResults = [];
let oldTotal = 0;
let newTotal = 0;
// Iterate over newEntries to calculate differences

for (const [entryName, newEntry] of newEntries) {
const oldEntry = oldEntries.get(entryName) || { size: 0 };
oldTotal += oldEntry.size;
newTotal += newEntry.size;
// Use the getAssetDiff function to create the diff object
const diff = getAssetDiff(entryName, oldEntry, newEntry);

const diff = getAssetDiff(entryName, oldEntry, newEntry);
diffResults.push(diff);
}

return {
entries: diffResults,
total: getAssetDiff("Entrypoints Total", { size: oldTotal }, { size: newTotal }),
total: getAssetDiff(
"Entrypoints Total",
{ size: oldTotal },
{ size: newTotal }
),
};
}

Expand Down
14 changes: 7 additions & 7 deletions bundle-size/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = async (
mainStatsFile,
prStatsFile
);
console.log("Comparing stats done. πŸŽ‰");
console.log("Comparing stats done. πŸ‘");
const diffThreshold = parseInt(threshold, 10);
const commentBody = getComment(assetsDiff, modulesDiff, entriesDiff);
console.log("Checking PR comments... πŸ“");
Expand All @@ -51,9 +51,7 @@ module.exports = async (
entriesDiff.total.diffPercentage >= 0 &&
entriesDiff.total.diffPercentage < diffThreshold
) {
const msg = `Total entrypoint size increase of ${entriesDiff.total.diffPercentage}% is below threshold of ${diffThreshold}%`;
console.log(`${msg}`);
core.setOutput(msg);
const msg = `Total entrypoint size increase of ${entriesDiff.total.diffPercentage}% is below threshold of ${diffThreshold}%. Exiting... πŸšͺ`;
if (previousComment) {
console.log("Updating PR comment... πŸ”„");
await github.rest.issues.updateComment({
Expand All @@ -65,6 +63,8 @@ module.exports = async (
),
});
}
console.log(`${msg}`);
core.setOutput(msg);
return;
}

Expand All @@ -76,16 +76,16 @@ module.exports = async (
body: commentBody,
});
} else {
console.log("Creating PR comment... πŸš€");
console.log("Creating PR comment... πŸ“");
await github.rest.issues.createComment({
...repo,
issue_number: prNumber,
body: commentBody,
});
}

console.log("Finished.");
core.setOutput("report", commentBody);
console.log("Finished. πŸŽ‰");
core.setOutput("Finished.", commentBody);
} catch (error) {
core.setFailed(error.message);
}
Expand Down

0 comments on commit f800513

Please sign in to comment.