Skip to content

Commit 254dbd5

Browse files
committed
[WIP] Get the before and after for the build results
1 parent c3f76fb commit 254dbd5

File tree

6 files changed

+1066
-1063
lines changed

6 files changed

+1066
-1063
lines changed

dangerfile.js

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,38 @@
99

1010
'use strict';
1111

12-
const { danger, markdown, warn } = require('danger');
12+
const { danger, markdown, schedule } = require('danger');
1313

14-
// Tags big PRs
15-
var bigPRThreshold = 600;
16-
if (danger.git.modified_files + danger.git.added_files + danger.git.deleted_files > bigPRThreshold) {
17-
const title = ':exclamation: Big PR';
18-
const files = danger.git.modified_files + danger.git.added_files + danger.git.deleted_files;
19-
const idea = `This PR is extremely unlikely to get reviewed because it touches ${files} files.`;
20-
warn(`${title} - <i>${idea}</i>`);
14+
const { resultsHeaders, generateResultsArray, currentBuildResults } = require('./scripts/rollup/stats');
2115

22-
markdown('@facebook-github-bot large-pr');
16+
/**
17+
* Generates a Markdown table
18+
* @param {string[]} headers
19+
* @param {string[][]} body
20+
*/
21+
function generateMDTable(headers, body) {
22+
const tableHeaders = [
23+
headers.join(' | '),
24+
headers.map(() => ' --- ').join(' | '),
25+
];
26+
27+
const tablebody = body.map(r => r.join(' | '));
28+
return tableHeaders.join('\n') + '\n' + tablebody.join('\n');
2329
}
30+
31+
// Grab the results.json before we ran CI via the GH API
32+
const getJSON = danger.github.utils
33+
.fileContents('scripts/rollup/results.json');
34+
35+
// @bug See https://github.com/danger/danger-js/issues/443
36+
schedule(getJSON);
37+
getJSON.then(APIPreviousBuildResults => {
38+
const previousBuildResults = JSON.parse(APIPreviousBuildResults);
39+
const results = generateResultsArray(currentBuildResults, previousBuildResults);
40+
41+
markdown('### Bundle Changes:\n');
42+
// const percentToWarrentShowing = 0.1
43+
// const onlyResultsToShow = results.filter(f => Math.abs(f[3]) > percentToWarrentShowing);
44+
45+
markdown(generateMDTable(resultsHeaders, results));
46+
});

scripts/circleci/test_entry_point.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ if [ $((2 % CIRCLE_NODE_TOTAL)) -eq "$CIRCLE_NODE_INDEX" ]; then
2626
COMMANDS_TO_RUN+=('yarn test-build --runInBand')
2727
COMMANDS_TO_RUN+=('node ./scripts/tasks/danger')
2828
COMMANDS_TO_RUN+=('yarn test-build-prod --runInBand')
29+
COMMANDS_TO_RUN+=('node ./scripts/tasks/danger')
2930
COMMANDS_TO_RUN+=('./scripts/circleci/upload_build.sh')
3031
fi
3132

scripts/rollup/results.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,4 +400,4 @@
400400
"gzip": 525
401401
}
402402
]
403-
}
403+
}

scripts/rollup/stats.js

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,35 @@ function saveResults() {
2020
);
2121
}
2222

23+
2324
function percentChange(prev, current) {
24-
const change = Math.floor((current - prev) / prev * 100);
25+
return Math.floor((current - prev) / prev * 100);
26+
}
2527

28+
function percentChangeString(change) {
2629
if (change > 0) {
2730
return chalk.red.bold(`+${change} %`);
2831
} else if (change <= 0) {
2932
return chalk.green.bold(change + ' %');
3033
}
3134
}
3235

33-
function printResults() {
34-
const table = new Table({
35-
head: [
36-
chalk.gray.yellow('Bundle'),
37-
chalk.gray.yellow('Prev Size'),
38-
chalk.gray.yellow('Current Size'),
39-
chalk.gray.yellow('Diff'),
40-
chalk.gray.yellow('Prev Gzip'),
41-
chalk.gray.yellow('Current Gzip'),
42-
chalk.gray.yellow('Diff'),
43-
],
44-
});
45-
currentBuildResults.bundleSizes.forEach(result => {
46-
const matches = prevBuildResults.bundleSizes.filter(
47-
({filename, bundleType}) =>
48-
filename === result.filename && bundleType === result.bundleType
49-
);
50-
if (matches.length > 1) {
51-
throw new Error(`Ambiguous bundle size record for: ${result.filename}`);
52-
}
53-
const prev = matches[0];
36+
const resultsHeaders = [
37+
'Bundle',
38+
'Prev Size',
39+
'Current Size',
40+
'Diff',
41+
'Prev Gzip',
42+
'Current Gzip',
43+
'Diff',
44+
];
45+
46+
function generateResultsArray(current, prevResults) {
47+
currentBuildResults.bundleSizes.forEach(index => {
48+
const result = currentBuildResults.bundleSizes[index];
49+
const prev = prevBuildResults.bundleSizes.filter(
50+
res => res.filename === result.filename
51+
)[0];
5452
if (result === prev) {
5553
// We didn't rebuild this bundle.
5654
return;
@@ -60,21 +58,45 @@ function printResults() {
6058
const gzip = result.gzip;
6159
let prevSize = prev ? prev.size : 0;
6260
let prevGzip = prev ? prev.gzip : 0;
63-
table.push([
64-
chalk.white.bold(`${result.filename} (${result.bundleType})`),
65-
chalk.gray.bold(filesize(prevSize)),
66-
chalk.white.bold(filesize(size)),
61+
62+
return [
63+
`${result.filename} (${result.bundleType}`,
64+
filesize(prevSize),
65+
filesize(size),
6766
percentChange(prevSize, size),
68-
chalk.gray.bold(filesize(prevGzip)),
69-
chalk.white.bold(filesize(gzip)),
67+
filesize(prevGzip),
68+
filesize(gzip),
7069
percentChange(prevGzip, gzip),
70+
];
71+
// Strip any nulls
72+
}).filter(f => f);
73+
}
74+
75+
function printResults() {
76+
const table = new Table({
77+
head: resultsHeaders.map(chalk.gray.yellow),
78+
});
79+
80+
const results = generateResultsArray(currentBuildResults, prevBuildResults)
81+
results.forEach(row => {
82+
table.push([
83+
chalk.white.bold(row[0]),
84+
chalk.gray.bold(row[1]),
85+
chalk.white.bold(row[2]),
86+
percentChangeString(row[3]),
87+
chalk.gray.bold(row[4]),
88+
chalk.white.bold(row[5]),
89+
percentChangeString(row[6]),
7190
]);
7291
});
92+
7393
return table.toString();
7494
}
7595

7696
module.exports = {
97+
currentBuildResults,
98+
generateResultsArray,
7799
printResults,
78100
saveResults,
79-
currentBuildResults,
101+
resultsHeaders,
80102
};

scripts/tasks/danger.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77

88
'use strict';
99

10-
var path = require('path');
11-
var spawn = require('child_process').spawn;
10+
const path = require('path');
11+
const spawn = require('child_process').spawn;
1212

13-
var extension = process.platform === 'win32' ? '.cmd' : '';
13+
const extension = process.platform === 'win32' ? '.cmd' : '';
1414

15-
// This came from React Native's circle.yaml
15+
// This came from React Native's circle.yml
1616
const token = 'e622517d9f1136ea8900' + '07c6373666312cdfaa69';
1717
spawn(path.join('node_modules', '.bin', 'danger' + extension), [], {
1818
// Allow colors to pass through
1919
stdio: 'inherit',
2020
env: {
21-
...process.env,
21+
...process.env,
2222
DANGER_GITHUB_API_TOKEN: token,
2323
},
2424
}).on('close', function(code) {

0 commit comments

Comments
 (0)