@@ -25,146 +25,6 @@ async function fetchPreviousComment(octokit, repo, pr) {
25
25
return ! sizeLimitComment ? null : sizeLimitComment ;
26
26
}
27
27
28
- async function run ( ) {
29
- const { getInput, setFailed } = core ;
30
-
31
- try {
32
- const { payload, repo } = context ;
33
- const pr = payload . pull_request ;
34
-
35
- const comparisonBranch = getInput ( 'comparison_branch' ) ;
36
- const githubToken = getInput ( 'github_token' ) ;
37
- const threshold = getInput ( 'threshold' ) ;
38
-
39
- if ( ! comparisonBranch && ! pr ) {
40
- throw new Error ( 'No PR found. Only pull_request workflows are supported.' ) ;
41
- }
42
-
43
- const octokit = getOctokit ( githubToken ) ;
44
- const limit = new SizeLimit ( ) ;
45
- const artifactClient = artifact . create ( ) ;
46
- const resultsFilePath = path . resolve ( __dirname , RESULTS_FILE ) ;
47
-
48
- // If we have no comparison branch, we just run size limit & store the result as artifact
49
- if ( ! comparisonBranch ) {
50
- let base ;
51
- const { output : baseOutput } = await execSizeLimit ( ) ;
52
-
53
- try {
54
- base = limit . parseResults ( baseOutput ) ;
55
- } catch ( error ) {
56
- core . error ( 'Error parsing size-limit output. The output should be a json.' ) ;
57
- throw error ;
58
- }
59
-
60
- try {
61
- await fs . writeFile ( resultsFilePath , JSON . stringify ( base ) , 'utf8' ) ;
62
- } catch ( err ) {
63
- core . error ( err ) ;
64
- }
65
- const globber = await glob . create ( resultsFilePath , {
66
- followSymbolicLinks : false ,
67
- } ) ;
68
- const files = await globber . glob ( ) ;
69
-
70
- await artifactClient . uploadArtifact ( ARTIFACT_NAME , files , __dirname ) ;
71
-
72
- return ;
73
- }
74
-
75
- // Else, we run size limit for the current branch, AND fetch it for the comparison branch
76
- let base ;
77
- let current ;
78
-
79
- try {
80
- // Ignore failures here as it is likely that this only happens when introducing size-limit
81
- // and this has not been run on the main branch yet
82
- await download ( octokit , {
83
- ...repo ,
84
- artifactName : ARTIFACT_NAME ,
85
- branch : comparisonBranch ,
86
- downloadPath : __dirname ,
87
- workflowEvent : 'push' ,
88
- workflowName : `${ process . env . GITHUB_WORKFLOW || '' } ` ,
89
- } ) ;
90
- base = JSON . parse ( await fs . readFile ( resultsFilePath , { encoding : 'utf8' } ) ) ;
91
- } catch ( error ) {
92
- core . startGroup ( 'Warning, unable to find base results' ) ;
93
- core . debug ( error ) ;
94
- core . endGroup ( ) ;
95
- }
96
-
97
- const { status, output } = await execSizeLimit ( ) ;
98
- try {
99
- current = limit . parseResults ( output ) ;
100
- } catch ( error ) {
101
- core . error ( 'Error parsing size-limit output. The output should be a json.' ) ;
102
- throw error ;
103
- }
104
-
105
- const thresholdNumber = Number ( threshold ) ;
106
-
107
- // @ts -ignore
108
- const sizeLimitComment = await fetchPreviousComment ( octokit , repo , pr ) ;
109
-
110
- const shouldComment =
111
- isNaN ( thresholdNumber ) || limit . hasSizeChanges ( base , current , thresholdNumber ) || sizeLimitComment ;
112
-
113
- if ( shouldComment ) {
114
- const body = [ SIZE_LIMIT_HEADING , markdownTable ( limit . formatResults ( base , current ) ) ] . join ( '\r\n' ) ;
115
-
116
- try {
117
- if ( ! sizeLimitComment ) {
118
- await octokit . rest . issues . createComment ( {
119
- ...repo ,
120
- // eslint-disable-next-line camelcase
121
- issue_number : pr . number ,
122
- body,
123
- } ) ;
124
- } else {
125
- await octokit . rest . issues . updateComment ( {
126
- ...repo ,
127
- // eslint-disable-next-line camelcase
128
- comment_id : sizeLimitComment . id ,
129
- body,
130
- } ) ;
131
- }
132
- } catch ( error ) {
133
- core . error (
134
- "Error updating comment. This can happen for PR's originating from a fork without write permissions." ,
135
- ) ;
136
- }
137
- }
138
-
139
- if ( status > 0 ) {
140
- setFailed ( 'Size limit has been exceeded.' ) ;
141
- }
142
- } catch ( error ) {
143
- core . error ( error ) ;
144
- setFailed ( error . message ) ;
145
- }
146
- }
147
-
148
- run ( ) ;
149
-
150
- async function execSizeLimit ( ) {
151
- let output = '' ;
152
-
153
- const status = await exec ( 'yarn' , [ 'size-limit' , '--json' ] , {
154
- ignoreReturnCode : true ,
155
- listeners : {
156
- stdout : data => {
157
- output += data . toString ( ) ;
158
- } ,
159
- } ,
160
- } ) ;
161
-
162
- return { status, output } ;
163
- }
164
-
165
- const SIZE_RESULTS_HEADER = [ 'Path' , 'Size' ] ;
166
- const TIME_RESULTS_HEADER = [ 'Path' , 'Size' , 'Loading time (3g)' , 'Running time (snapdragon)' , 'Total time' ] ;
167
-
168
28
class SizeLimit {
169
29
formatBytes ( size ) {
170
30
return bytes . format ( size , { unitSeparator : ' ' } ) ;
@@ -287,10 +147,150 @@ class SizeLimit {
287
147
}
288
148
}
289
149
150
+ async function execSizeLimit ( ) {
151
+ let output = '' ;
152
+
153
+ const status = await exec ( 'yarn' , [ 'size-limit' , '--json' ] , {
154
+ ignoreReturnCode : true ,
155
+ listeners : {
156
+ stdout : data => {
157
+ output += data . toString ( ) ;
158
+ } ,
159
+ } ,
160
+ } ) ;
161
+
162
+ return { status, output } ;
163
+ }
164
+
165
+ const SIZE_RESULTS_HEADER = [ 'Path' , 'Size' ] ;
166
+ const TIME_RESULTS_HEADER = [ 'Path' , 'Size' , 'Loading time (3g)' , 'Running time (snapdragon)' , 'Total time' ] ;
167
+
290
168
const EmptyResult = {
291
169
name : '-' ,
292
170
size : 0 ,
293
171
running : 0 ,
294
172
loading : 0 ,
295
173
total : 0 ,
296
174
} ;
175
+
176
+ async function run ( ) {
177
+ const { getInput, setFailed } = core ;
178
+
179
+ try {
180
+ const { payload, repo } = context ;
181
+ const pr = payload . pull_request ;
182
+
183
+ const comparisonBranch = getInput ( 'comparison_branch' ) ;
184
+ const githubToken = getInput ( 'github_token' ) ;
185
+ const threshold = getInput ( 'threshold' ) ;
186
+
187
+ if ( ! comparisonBranch && ! pr ) {
188
+ throw new Error ( 'No PR found. Only pull_request workflows are supported.' ) ;
189
+ }
190
+
191
+ const octokit = getOctokit ( githubToken ) ;
192
+ const limit = new SizeLimit ( ) ;
193
+ const artifactClient = artifact . create ( ) ;
194
+ const resultsFilePath = path . resolve ( __dirname , RESULTS_FILE ) ;
195
+
196
+ // If we have no comparison branch, we just run size limit & store the result as artifact
197
+ if ( ! comparisonBranch ) {
198
+ let base ;
199
+ const { output : baseOutput } = await execSizeLimit ( ) ;
200
+
201
+ try {
202
+ base = limit . parseResults ( baseOutput ) ;
203
+ } catch ( error ) {
204
+ core . error ( 'Error parsing size-limit output. The output should be a json.' ) ;
205
+ throw error ;
206
+ }
207
+
208
+ try {
209
+ await fs . writeFile ( resultsFilePath , JSON . stringify ( base ) , 'utf8' ) ;
210
+ } catch ( err ) {
211
+ core . error ( err ) ;
212
+ }
213
+ const globber = await glob . create ( resultsFilePath , {
214
+ followSymbolicLinks : false ,
215
+ } ) ;
216
+ const files = await globber . glob ( ) ;
217
+
218
+ await artifactClient . uploadArtifact ( ARTIFACT_NAME , files , __dirname ) ;
219
+
220
+ return ;
221
+ }
222
+
223
+ // Else, we run size limit for the current branch, AND fetch it for the comparison branch
224
+ let base ;
225
+ let current ;
226
+
227
+ try {
228
+ // Ignore failures here as it is likely that this only happens when introducing size-limit
229
+ // and this has not been run on the main branch yet
230
+ await download ( octokit , {
231
+ ...repo ,
232
+ artifactName : ARTIFACT_NAME ,
233
+ branch : comparisonBranch ,
234
+ downloadPath : __dirname ,
235
+ workflowEvent : 'push' ,
236
+ workflowName : `${ process . env . GITHUB_WORKFLOW || '' } ` ,
237
+ } ) ;
238
+ base = JSON . parse ( await fs . readFile ( resultsFilePath , { encoding : 'utf8' } ) ) ;
239
+ } catch ( error ) {
240
+ core . startGroup ( 'Warning, unable to find base results' ) ;
241
+ core . debug ( error ) ;
242
+ core . endGroup ( ) ;
243
+ }
244
+
245
+ const { status, output } = await execSizeLimit ( ) ;
246
+ try {
247
+ current = limit . parseResults ( output ) ;
248
+ } catch ( error ) {
249
+ core . error ( 'Error parsing size-limit output. The output should be a json.' ) ;
250
+ throw error ;
251
+ }
252
+
253
+ const thresholdNumber = Number ( threshold ) ;
254
+
255
+ // @ts -ignore
256
+ const sizeLimitComment = await fetchPreviousComment ( octokit , repo , pr ) ;
257
+
258
+ const shouldComment =
259
+ isNaN ( thresholdNumber ) || limit . hasSizeChanges ( base , current , thresholdNumber ) || sizeLimitComment ;
260
+
261
+ if ( shouldComment ) {
262
+ const body = [ SIZE_LIMIT_HEADING , markdownTable ( limit . formatResults ( base , current ) ) ] . join ( '\r\n' ) ;
263
+
264
+ try {
265
+ if ( ! sizeLimitComment ) {
266
+ await octokit . rest . issues . createComment ( {
267
+ ...repo ,
268
+ // eslint-disable-next-line camelcase
269
+ issue_number : pr . number ,
270
+ body,
271
+ } ) ;
272
+ } else {
273
+ await octokit . rest . issues . updateComment ( {
274
+ ...repo ,
275
+ // eslint-disable-next-line camelcase
276
+ comment_id : sizeLimitComment . id ,
277
+ body,
278
+ } ) ;
279
+ }
280
+ } catch ( error ) {
281
+ core . error (
282
+ "Error updating comment. This can happen for PR's originating from a fork without write permissions." ,
283
+ ) ;
284
+ }
285
+ }
286
+
287
+ if ( status > 0 ) {
288
+ setFailed ( 'Size limit has been exceeded.' ) ;
289
+ }
290
+ } catch ( error ) {
291
+ core . error ( error ) ;
292
+ setFailed ( error . message ) ;
293
+ }
294
+ }
295
+
296
+ run ( ) ;
0 commit comments