25
25
//
26
26
// `DANGER_GITHUB_API_TOKEN=[ENV_ABOVE] yarn danger pr https://github.com/facebook/react/pull/11865
27
27
28
- const { markdown, danger, warn} = require ( 'danger' ) ;
29
- const fetch = require ( 'node-fetch' ) ;
28
+ const { markdown, danger} = require ( 'danger' ) ;
30
29
31
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' ) ;
57
33
58
34
/**
59
35
* Generates a Markdown table
@@ -100,99 +76,23 @@ function setBoldness(row, isBold) {
100
76
}
101
77
}
102
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 ;
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 ) ;
145
87
}
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 = / \/ f a c e b o o k \/ r e a c t \/ ( [ 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
- }
160
- }
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 ;
188
88
}
89
+ return { bundleSizes} ;
90
+ }
189
91
92
+ async function printResultsForChannel ( baseResults , headResults ) {
190
93
// Take the JSON of the build response and
191
94
// make an array comparing the results for printing
192
- const results = generateResultsArray (
193
- currentBuildResults ,
194
- previousBuildResults
195
- ) ;
95
+ const results = generateResultsArray ( baseResults , headResults ) ;
196
96
197
97
const packagesToShow = results
198
98
. filter (
@@ -281,9 +181,6 @@ function git(args) {
281
181
<details>
282
182
<summary>Details of bundled changes.</summary>
283
183
284
- <p>Comparing: ${ baseCommit } ...${ danger . github . pr . head . sha } </p>
285
-
286
-
287
184
${ allTables . join ( '\n' ) }
288
185
289
186
</details>
@@ -292,4 +189,35 @@ function git(args) {
292
189
} else {
293
190
markdown ( 'No significant bundle size changes to report.' ) ;
294
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
+ markdown ( '## Size changes' ) ;
205
+
206
+ const headSha = ( readFileSync ( './build2/COMMIT_SHA' ) + '' ) . trim ( ) ;
207
+ const headSizesStable = getBundleSizes ( './build2/sizes-stable' ) ;
208
+ const headSizesExperimental = getBundleSizes ( './build2/sizes-experimental' ) ;
209
+
210
+ const baseSha = ( readFileSync ( './base-build/COMMIT_SHA' ) + '' ) . trim ( ) ;
211
+ const baseSizesStable = getBundleSizes ( './base-build/sizes-stable' ) ;
212
+ const baseSizesExperimental = getBundleSizes (
213
+ './base-build/sizes-experimental'
214
+ ) ;
215
+
216
+ markdown ( `<p>Comparing: ${ baseSha } ...${ headSha } </p>` ) ;
217
+
218
+ markdown ( '## Stable channel' ) ;
219
+ printResultsForChannel ( baseSizesStable , headSizesStable ) ;
220
+
221
+ markdown ( '## Experimental channel' ) ;
222
+ printResultsForChannel ( baseSizesExperimental , headSizesExperimental ) ;
295
223
} ) ( ) ;
0 commit comments