26
// `DANGER_GITHUB_API_TOKEN=[ENV_ABOVE] yarn danger pr https://github.com/facebook/react/pull/11865
27
28
const {markdown, danger, warn} = require('danger');
29
-const fetch = require('node-fetch');
29
30
const {generateResultsArray} = require('./scripts/rollup/stats');
32
-const {existsSync, readFileSync} = require('fs');
33
-const {exec} = require('child_process');
34
-
35
-// This must match the name of the CI job that creates the build artifacts
36
-const RELEASE_CHANNEL =
37
- process.env.RELEASE_CHANNEL === 'experimental' ? 'experimental' : 'stable';
38
-const artifactsJobName =
39
- process.env.RELEASE_CHANNEL === 'experimental'
40
- ? 'process_artifacts_experimental'
41
- : 'process_artifacts';
42
-
43
-if (!existsSync('./build/bundle-sizes.json')) {
44
- // This indicates the build failed previously.
45
- // In that case, there's nothing for the Dangerfile to do.
46
- // Exit early to avoid leaving a redundant (and potentially confusing) PR comment.
47
- warn(
48
- 'No bundle size information found. This indicates the build ' +
49
- 'job failed.'
50
- );
51
- process.exit(0);
52
-}
53
-
54
-const currentBuildResults = JSON.parse(
55
- readFileSync('./build/bundle-sizes.json')
56
-);
31
+const {readFileSync, readdirSync} = require('fs');
32
+const path = require('path');
33
34
/**
35
* Generates a Markdown table
76
}
77
}
78
103
-/**
104
- * Gets the commit that represents the merge between the current branch
105
- * and master.
106
- */
107
-function git(args) {
108
- return new Promise(res => {
109
- exec('git ' + args, (err, stdout, stderr) => {
110
- if (err) {
111
- throw err;
112
- } else {
113
- res(stdout.trim());
114
- }
115
- });
116
- });
117
-}
118
-
119
-(async function() {
120
- // Use git locally to grab the commit which represents the place
121
- // where the branches differ
122
- const upstreamRepo = danger.github.pr.base.repo.full_name;
123
- if (upstreamRepo !== 'facebook/react') {
124
- // Exit unless we're running in the main repo
125
- return;
126
- }
127
-
128
- markdown(`## Size changes (${RELEASE_CHANNEL})`);
129
-
130
- const upstreamRef = danger.github.pr.base.ref;
131
- await git(`remote add upstream https://github.com/facebook/react.git`);
132
- await git('fetch upstream');
133
- const baseCommit = await git(`merge-base HEAD upstream/${upstreamRef}`);
134
-
135
- let previousBuildResults = null;
136
- try {
137
- let baseCIBuildId = null;
138
- const statusesResponse = await fetch(
139
- `https://api.github.com/repos/facebook/react/commits/${baseCommit}/status`
140
- );
141
- const {statuses, state} = await statusesResponse.json();
142
- if (state === 'failure') {
143
- warn(`Base commit is broken: ${baseCommit}`);
144
- return;
145
- }
146
- for (let i = 0; i < statuses.length; i++) {
147
- const status = statuses[i];
148
- if (status.context === `ci/circleci: ${artifactsJobName}`) {
149
- if (status.state === 'success') {
150
- baseCIBuildId = /\/facebook\/react\/([0-9]+)/.exec(
151
- status.target_url
152
- )[1];
153
- break;
154
- }
155
- if (status.state === 'pending') {
156
- warn(`Build job for base commit is still pending: ${baseCommit}`);
157
- return;
158
- }
159
- }
79
+function getBundleSizes(pathToSizesDir) {
80
+ const filenames = readdirSync(pathToSizesDir);
81
+ let bundleSizes = [];
82
+ for (let i = 0; i < filenames.length; i++) {
83
+ const filename = filenames[i];
84
+ if (filename.endsWith('.json')) {
85
+ const json = readFileSync(path.join(pathToSizesDir, filename));
86
+ bundleSizes.push(...JSON.parse(json).bundleSizes);
87
}
161
-
162
- if (baseCIBuildId === null) {
163
- warn(`Could not find build artifacts for base commit: ${baseCommit}`);
164
- return;
165
- }
166
-
167
- const baseArtifactsInfoResponse = await fetch(
168
- `https://circleci.com/api/v1.1/project/github/facebook/react/${baseCIBuildId}/artifacts`
169
- );
170
- const baseArtifactsInfo = await baseArtifactsInfoResponse.json();
171
-
172
- for (let i = 0; i < baseArtifactsInfo.length; i++) {
173
- const info = baseArtifactsInfo[i];
174
- if (info.path.endsWith('bundle-sizes.json')) {
175
- const resultsResponse = await fetch(info.url);
176
- previousBuildResults = await resultsResponse.json();
177
- break;
178
- }
179
- }
180
- } catch (error) {
181
- warn(`Failed to fetch build artifacts for base commit: ${baseCommit}`);
182
- return;
183
- }
184
-
185
- if (previousBuildResults === null) {
186
- warn(`Could not find build artifacts for base commit: ${baseCommit}`);
187
- return;
88
}
89
+ return {bundleSizes};
90
+}
91
92
+async function printResultsForChannel(baseResults, headResults) {
93
// Take the JSON of the build response and
94
// make an array comparing the results for printing
192
- const results = generateResultsArray(
193
- currentBuildResults,
194
- previousBuildResults
195
- );
95
+ const results = generateResultsArray(baseResults, headResults);
96
97
const packagesToShow = results
98
.filter(
181
<details>
182
<summary>Details of bundled changes.</summary>
183
284
- <p>Comparing: ${baseCommit}...${danger.github.pr.head.sha}</p>
285
-
286
-
184
${allTables.join('\n')}
185
186
</details>
187
`;
291
- markdown(summary);
188
+ return summary;
189
} else {
293
- markdown('No significant bundle size changes to report.');
190
+ return 'No significant bundle size changes to report.';
191
}
192
+}
193
+
194
+(async function() {
195
+ // Use git locally to grab the commit which represents the place
196
+ // where the branches differ
197
+
198
+ const upstreamRepo = danger.github.pr.base.repo.full_name;
199
+ if (upstreamRepo !== 'facebook/react') {
200
+ // Exit unless we're running in the main repo
201
+ return;
202
+ }
203
+
204
+ let headSha;
205
+ let headSizesStable;
206
+ let headSizesExperimental;
207
+
208
+ let baseSha;
209
+ let baseSizesStable;
210
+ let baseSizesExperimental;
211
+
212
+ try {
213
+ headSha = (readFileSync('./build2/COMMIT_SHA') + '').trim();
214
+ headSizesStable = getBundleSizes('./build2/sizes-stable');
215
+ headSizesExperimental = getBundleSizes('./build2/sizes-experimental');
216
+
217
+ baseSha = (readFileSync('./base-build/COMMIT_SHA') + '').trim();
218
+ baseSizesStable = getBundleSizes('./base-build/sizes-stable');
219
+ baseSizesExperimental = getBundleSizes('./base-build/sizes-experimental');
220
+ } catch {
221
+ warn(
222
+ "Failed to read build artifacts. It's possible a build configuration " +
223
+ 'has changed upstream. Try pulling the latest changes from the ' +
224
+ 'main branch.'
225
+ );
226
+ return;
227
+ }
228
+
229
+ markdown(`
230
+## Size changes
231
+
232
+<p>Comparing: ${baseSha}...${headSha}</p>
233
+
234
+### Stable channel
235
+
236
+${await printResultsForChannel(baseSizesStable, headSizesStable)}
237
+
238
+### Experimental channel
239
+
240
+${await printResultsForChannel(baseSizesExperimental, headSizesExperimental)}
241
+`);
242
})();