Skip to content

Commit

Permalink
[core] Improve bundle size comment (#23110)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored Oct 20, 2020
1 parent 8023b1c commit 10646a5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 23 deletions.
10 changes: 10 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ steps:
yarn install
displayName: 'install dependencies'
- script: |
yarn danger ci
displayName: 'prepare danger on PRs'
condition: and(succeeded(), eq(variables['Build.Reason'], 'PullRequest'))
env:
AZURE_BUILD_ID: $(Build.BuildId)
DANGER_COMMAND: 'prepareBundleSizeReport'
DANGER_GITHUB_API_TOKEN: $(GITHUB_API_TOKEN)
- script: |
yarn lerna run --ignore @material-ui/icons --parallel --scope "@material-ui/*" build
displayName: 'build @material-ui packages'
Expand Down Expand Up @@ -135,4 +144,5 @@ steps:
condition: and(succeeded(), eq(variables['Build.Reason'], 'PullRequest'))
env:
AZURE_BUILD_ID: $(Build.BuildId)
DANGER_COMMAND: 'reportBundleSize'
DANGER_GITHUB_API_TOKEN: $(GITHUB_API_TOKEN)
57 changes: 34 additions & 23 deletions dangerfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const { danger, markdown } = require('danger');
const { exec } = require('child_process');
const { loadComparison } = require('./scripts/sizeSnapshot');

const azureBuildId = process.env.AZURE_BUILD_ID;
const azureBuildUrl = `https://dev.azure.com/mui-org/Material-UI/_build/results?buildId=${azureBuildId}`;
const dangerCommand = process.env.DANGER_COMMAND;

const parsedSizeChangeThreshold = 300;
const gzipSizeChangeThreshold = 100;

Expand All @@ -30,7 +34,7 @@ const UPSTREAM_REMOTE = 'danger-upstream';
* scripts exit to avoid adding internal remotes to the local machine. This is
* not an issue in CI.
*/
async function cleanup() {
async function reportBundleSizeCleanup() {
await git(`remote remove ${UPSTREAM_REMOTE}`);
}

Expand Down Expand Up @@ -103,7 +107,13 @@ function sieveResults(results) {
return { all: results, main, pages };
}

async function run() {
function prepareBundleSizeReport() {
markdown(
`Bundle size will be reported once [Azure build #${azureBuildId}](${azureBuildUrl}) finishes.`,
);
}

async function reportBundleSize() {
// Use git locally to grab the commit which represents the place
// where the branches differ
const upstreamRepo = danger.github.pr.base.repo.full_name;
Expand All @@ -116,7 +126,7 @@ async function run() {
await git(`fetch ${UPSTREAM_REMOTE}`);
const mergeBaseCommit = await git(`merge-base HEAD ${UPSTREAM_REMOTE}/${upstreamRef}`);

const commitRange = `${mergeBaseCommit}...${danger.github.pr.head.sha}`;
const detailedComparisonUrl = `https://mui-dashboard.netlify.app/size-comparison?buildId=${azureBuildId}&baseRef=${danger.github.pr.base.ref}&baseCommit=${mergeBaseCommit}&prNumber=${danger.github.pr.number}`;

const comparison = await loadComparison(mergeBaseCommit, upstreamRef);

Expand All @@ -134,31 +144,32 @@ async function run() {
markdown(importantChanges.join('\n'));
}

const details = `[Details of bundle changes](https://mui-dashboard.netlify.app/size-comparison?buildId=${process.env.AZURE_BUILD_ID}&baseRef=${danger.github.pr.base.ref}&baseCommit=${mergeBaseCommit}&prNumber=${danger.github.pr.number})`;
const details = `[Details of bundle changes](${detailedComparisonUrl})`;

markdown(details);
} else {
// this can later be removed to reduce PR noise. It is kept for now for debug
// purposes only. DangerJS will swallow console.logs if it completes successfully
markdown(`No bundle size changes comparing ${commitRange}`);
markdown(`[No bundle size changes](${detailedComparisonUrl})`);
}
}

(async () => {
let exitCode = 0;
try {
await run();
} catch (err) {
console.error(err);
exitCode = 1;
}

try {
await cleanup();
} catch (err) {
console.error(err);
exitCode = 1;
async function run() {
switch (dangerCommand) {
case 'prepareBundleSizeReport':
prepareBundleSizeReport();
break;
case 'reportBundleSize':
try {
await reportBundleSize();
} finally {
await reportBundleSizeCleanup();
}
break;
default:
throw new TypeError(`Unrecognized danger command '${dangerCommand}'`);
}
}

process.exit(exitCode);
})();
run().catch((error) => {
console.error(error);
process.exit(1);
});

0 comments on commit 10646a5

Please sign in to comment.