@@ -10,7 +10,8 @@ import * as glob from '@actions/glob';
10
10
import * as io from '@actions/io' ;
11
11
import { markdownTable } from 'markdown-table' ;
12
12
13
- import { SizeLimit } from './utils/size-limit-formatter.mjs' ;
13
+ import { SizeLimit } from './utils/SizeLimitFormatter.mjs' ;
14
+ import { getArtifactsForBranchAndWorkflow } from './utils/getArtifactsForBranchAndWorkflow.mjs' ;
14
15
15
16
const SIZE_LIMIT_HEADING = '## size-limit report 📦 ' ;
16
17
const ARTIFACT_NAME = 'size-limit-action' ;
@@ -206,138 +207,6 @@ async function runSizeLimitOnComparisonBranch() {
206
207
await artifactClient . uploadArtifact ( ARTIFACT_NAME , files , __dirname ) ;
207
208
}
208
209
209
- // max pages of workflows to pagination through
210
- const DEFAULT_MAX_PAGES = 50 ;
211
- // max results per page
212
- const DEFAULT_PAGE_LIMIT = 10 ;
213
-
214
- /**
215
- * Fetch artifacts from a workflow run from a branch
216
- *
217
- * This is a bit hacky since GitHub Actions currently does not directly
218
- * support downloading artifacts from other workflows
219
- */
220
- /**
221
- * Fetch artifacts from a workflow run from a branch
222
- *
223
- * This is a bit hacky since GitHub Actions currently does not directly
224
- * support downloading artifacts from other workflows
225
- */
226
- async function getArtifactsForBranchAndWorkflow ( octokit , { owner, repo, workflowName, branch, artifactName } ) {
227
- core . startGroup ( `getArtifactsForBranchAndWorkflow - workflow:"${ workflowName } ", branch:"${ branch } "` ) ;
228
-
229
- let repositoryWorkflow = null ;
230
-
231
- // For debugging
232
- const allWorkflows = [ ] ;
233
-
234
- //
235
- // Find workflow id from `workflowName`
236
- //
237
- for await ( const response of octokit . paginate . iterator ( octokit . rest . actions . listRepoWorkflows , {
238
- owner,
239
- repo,
240
- } ) ) {
241
- const targetWorkflow = response . data . find ( ( { name } ) => name === workflowName ) ;
242
-
243
- allWorkflows . push ( ...response . data . map ( ( { name } ) => name ) ) ;
244
-
245
- // If not found in responses, continue to search on next page
246
- if ( ! targetWorkflow ) {
247
- continue ;
248
- }
249
-
250
- repositoryWorkflow = targetWorkflow ;
251
- break ;
252
- }
253
-
254
- if ( ! repositoryWorkflow ) {
255
- core . info (
256
- `Unable to find workflow with name "${ workflowName } " in the repository. Found workflows: ${ allWorkflows . join (
257
- ', ' ,
258
- ) } `,
259
- ) ;
260
- core . endGroup ( ) ;
261
- return null ;
262
- }
263
-
264
- const workflow_id = repositoryWorkflow . id ;
265
-
266
- let currentPage = 0 ;
267
- let latestWorkflowRun = null ;
268
-
269
- for await ( const response of octokit . paginate . iterator ( octokit . rest . actions . listWorkflowRuns , {
270
- owner,
271
- repo,
272
- workflow_id,
273
- branch,
274
- per_page : DEFAULT_PAGE_LIMIT ,
275
- event : 'push' ,
276
- } ) ) {
277
- if ( ! response . data . length ) {
278
- core . warning ( `Workflow ${ workflow_id } not found in branch ${ branch } ` ) ;
279
- core . endGroup ( ) ;
280
- return null ;
281
- }
282
-
283
- // Do not allow downloading artifacts from a fork.
284
- const filtered = response . data . filter ( workflowRun => workflowRun . head_repository . full_name === `${ owner } /${ repo } ` ) ;
285
-
286
- // Sort to ensure the latest workflow run is the first
287
- filtered . sort ( ( a , b ) => {
288
- return new Date ( b . created_at ) . getTime ( ) - new Date ( a . created_at ) . getTime ( ) ;
289
- } ) ;
290
-
291
- // Store the first workflow run, to determine if this is the latest one...
292
- if ( ! latestWorkflowRun ) {
293
- latestWorkflowRun = filtered [ 0 ] ;
294
- }
295
-
296
- // Search through workflow artifacts until we find a workflow run w/ artifact name that we are looking for
297
- for ( const workflowRun of filtered ) {
298
- core . info ( `Checking artifacts for workflow run: ${ workflowRun . html_url } ` ) ;
299
-
300
- const {
301
- data : { artifacts } ,
302
- } = await octokit . rest . actions . listWorkflowRunArtifacts ( {
303
- owner,
304
- repo,
305
- run_id : workflowRun . id ,
306
- } ) ;
307
-
308
- if ( ! artifacts ) {
309
- core . warning (
310
- `Unable to fetch artifacts for branch: ${ branch } , workflow: ${ workflow_id } , workflowRunId: ${ workflowRun . id } ` ,
311
- ) ;
312
- } else {
313
- const foundArtifact = artifacts . find ( ( { name } ) => name === artifactName ) ;
314
- if ( foundArtifact ) {
315
- core . info ( `Found suitable artifact XXX: ${ foundArtifact . url } ` ) ;
316
- return {
317
- artifact : foundArtifact ,
318
- workflowRun,
319
- isLatest : latestWorkflowRun . id === workflowRun . id ,
320
- } ;
321
- } else {
322
- core . info ( `No artifact found for ${ artifactName } , trying next workflow run...` ) ;
323
- }
324
- }
325
- }
326
-
327
- if ( currentPage > DEFAULT_MAX_PAGES ) {
328
- core . warning ( `Workflow ${ workflow_id } not found in branch: ${ branch } ` ) ;
329
- core . endGroup ( ) ;
330
- return null ;
331
- }
332
-
333
- currentPage ++ ;
334
- }
335
-
336
- core . warning ( `Artifact not found: ${ artifactName } ` ) ;
337
- core . endGroup ( ) ;
338
- return null ;
339
- }
340
-
341
210
run ( ) ;
342
211
343
212
/**
0 commit comments