@@ -3,7 +3,7 @@ const ToolCache = require("@actions/tool-cache");
3
3
const Cache = require ( "@actions/cache" ) ;
4
4
const IO = require ( "@actions/io" ) ;
5
5
const Glob = require ( "@actions/glob" ) ;
6
- const Octokit = require ( "@octokit/request " ) ;
6
+ const { Octokit} = require ( "@octokit/rest " ) ;
7
7
const fetch = require ( "node-fetch" ) ;
8
8
const Path = require ( "path" ) ;
9
9
const ChildProcess = require ( "child_process" ) ;
@@ -221,7 +221,7 @@ async function installShards({shards, path}, crystalPromise) {
221
221
if ( NumericVersion . test ( shards ) ) {
222
222
shards = "v" + shards ;
223
223
}
224
- const ref = await findRef ( { name : "Shards" , apiBase : GitHubApiBaseShards , version : shards } ) ;
224
+ const ref = await findRef ( { name : "Shards" , repo : RepoShards , version : shards } ) ;
225
225
Core . setOutput ( "shards" , ref ) ;
226
226
227
227
const cacheKey = `install-shards-v1-${ ref } --${ getArch ( ) } -${ getPlatform ( ) } ` ;
@@ -234,7 +234,7 @@ async function installShards({shards, path}, crystalPromise) {
234
234
}
235
235
if ( ! restored ) {
236
236
Core . info ( `Cache not found for key '${ cacheKey } '` ) ;
237
- const fetchSrcTask = downloadSource ( { name : "Shards" , apiBase : GitHubApiBaseShards , ref} ) ;
237
+ const fetchSrcTask = downloadSource ( { name : "Shards" , repo : RepoShards , ref} ) ;
238
238
await IO . mv ( await fetchSrcTask , path ) ;
239
239
await crystalPromise ;
240
240
await rebuildShards ( { path} ) ;
@@ -261,64 +261,69 @@ async function rebuildShards({path}) {
261
261
Core . endGroup ( ) ;
262
262
}
263
263
264
- const GitHubApiBase = "/repos/ crystal-lang/ crystal";
265
- const GitHubApiBaseShards = "/repos/ crystal-lang/ shards";
264
+ const RepoCrystal = { owner : " crystal-lang" , repo : " crystal"} ;
265
+ const RepoShards = { owner : " crystal-lang" , repo : " shards"} ;
266
266
const CircleApiBase = "https://circleci.com/api/v1.1/project/github/crystal-lang/crystal" ;
267
267
268
- async function findRelease ( { name, apiBase , tag} ) {
268
+ async function findRelease ( { name, repo , tag} ) {
269
269
Core . info ( `Looking for ${ name } release (${ tag || "latest" } )` ) ;
270
- const releasesResp = await githubGet ( {
271
- url : apiBase + "/releases/" + ( tag ? "tags/" + tag : "latest" ) ,
272
- } ) ;
270
+ const releasesResp = await ( tag
271
+ ? github . rest . repos . getReleaseByTag ( { ... repo , tag} )
272
+ : github . rest . repos . getLatestRelease ( repo ) ) ;
273
273
const release = releasesResp . data ;
274
274
Core . info ( `Found ${ name } release ${ release [ "html_url" ] } ` ) ;
275
275
return release ;
276
276
}
277
277
278
- async function findLatestCommit ( { name, apiBase , branch = "master" } ) {
278
+ async function findLatestCommit ( { name, repo , branch = "master" } ) {
279
279
Core . info ( `Looking for latest ${ name } commit` ) ;
280
- const commitsResp = await githubGet ( {
281
- url : apiBase + "/commits/:branch" ,
282
- "branch" : branch ,
280
+ const commitsResp = await github . rest . repos . getCommit ( {
281
+ ...repo , "ref" : branch ,
283
282
} ) ;
284
283
const commit = commitsResp . data ;
285
284
Core . info ( `Found ${ name } commit ${ commit [ "html_url" ] } ` ) ;
286
285
return commit [ "sha" ] ;
287
286
}
288
287
289
288
async function downloadCrystalRelease ( suffix , version = null ) {
290
- const release = await findRelease ( { name : "Crystal" , apiBase : GitHubApiBase , tag : version } ) ;
289
+ const release = await findRelease ( { name : "Crystal" , repo : RepoCrystal , tag : version } ) ;
291
290
Core . setOutput ( "crystal" , release [ "tag_name" ] ) ;
292
291
293
292
const asset = release [ "assets" ] . find ( ( a ) => a [ "name" ] . endsWith ( [ `-${ suffix } .tar.gz` ] ) ) ;
294
293
295
294
Core . info ( `Downloading Crystal build from ${ asset [ "url" ] } ` ) ;
296
- const downloadedPath = await githubDownloadViaRedirect ( {
295
+ const resp = await github . request ( {
297
296
url : asset [ "url" ] ,
298
297
headers : { "accept" : "application/octet-stream" } ,
298
+ request : { redirect : "manual" } ,
299
299
} ) ;
300
+ const url = resp . headers [ "location" ] ;
300
301
302
+ const downloadedPath = await ToolCache . downloadTool ( url ) ;
301
303
Core . info ( "Extracting Crystal build" ) ;
302
304
const extractedPath = await ToolCache . extractTar ( downloadedPath ) ;
303
305
return onlySubdir ( extractedPath ) ;
304
306
}
305
307
306
- async function findRef ( { name, apiBase , version} ) {
308
+ async function findRef ( { name, repo , version} ) {
307
309
if ( version === Nightly ) {
308
- return findLatestCommit ( { name, apiBase } ) ;
310
+ return findLatestCommit ( { name, repo } ) ;
309
311
} else if ( version === Latest ) {
310
- const release = await findRelease ( { name, apiBase } ) ;
312
+ const release = await findRelease ( { name, repo } ) ;
311
313
return release [ "tag_name" ] ;
312
314
}
313
315
return version ;
314
316
}
315
317
316
- async function downloadSource ( { name, apiBase , ref} ) {
318
+ async function downloadSource ( { name, repo , ref} ) {
317
319
Core . info ( `Downloading ${ name } source for ${ ref } ` ) ;
318
- const downloadedPath = await githubDownloadViaRedirect ( {
319
- url : apiBase + "/zipball/:ref" ,
320
- "ref" : ref ,
320
+
321
+ const resp = await github . rest . repos . downloadZipballArchive ( {
322
+ ...repo , ref,
323
+ request : { redirect : "manual" } ,
321
324
} ) ;
325
+ const url = resp . headers [ "location" ] ;
326
+ const downloadedPath = await ToolCache . downloadTool ( url ) ;
322
327
Core . info ( `Extracting ${ name } source` ) ;
323
328
return onlySubdir ( await ToolCache . extractZip ( downloadedPath ) ) ;
324
329
}
@@ -367,42 +372,40 @@ async function installCrystalForWindows({crystal, arch = "x86_64", path}) {
367
372
async function downloadCrystalNightlyForWindows ( ) {
368
373
Core . info ( "Looking for latest Crystal build" ) ;
369
374
370
- const runsResp = await githubGet ( {
371
- url : GitHubApiBase + "/actions/workflows/ win.yml/runs? branch= master&event=push&status=success ",
372
- "per_page" : 1 ,
375
+ const runsResp = await github . rest . actions . listWorkflowRuns ( {
376
+ ... RepoCrystal , "workflow_id" : " win.yml" , " branch" : " master",
377
+ "event" : "push" , "status" : "success" , " per_page" : 1 ,
373
378
} ) ;
374
379
const [ workflowRun ] = runsResp . data [ "workflow_runs" ] ;
375
380
const { "head_sha" : ref , "id" : runId } = workflowRun ;
376
381
Core . info ( `Found Crystal release ${ workflowRun [ "html_url" ] } ` ) ;
377
382
Core . setOutput ( "crystal" , ref ) ;
378
383
379
- const artifactsResp = await githubGet ( {
380
- url : GitHubApiBase + "/actions/runs/:run_id/artifacts" ,
381
- "run_id" : runId ,
384
+ const artifactsResp = await github . rest . actions . listWorkflowRunArtifacts ( {
385
+ ...RepoCrystal , "run_id" : runId ,
382
386
} ) ;
383
387
const artifact = artifactsResp . data [ "artifacts" ] . find ( ( x ) => x . name === "crystal" ) ;
384
388
385
389
Core . info ( "Downloading Crystal build" ) ;
386
- const downloadedPath = await githubDownloadViaRedirect ( {
387
- url : GitHubApiBase + "/actions/artifacts/:artifact_id/ zip",
388
- "artifact_id" : artifact . id ,
390
+ const resp = await github . rest . actions . downloadArtifact ( {
391
+ ... RepoCrystal , "artifact_id" : artifact . id , "archive_format" : " zip",
392
+ request : { redirect : "manual" } ,
389
393
} ) ;
394
+ const url = resp . headers [ "location" ] ;
395
+ const downloadedPath = await ToolCache . downloadTool ( url ) ;
390
396
391
397
Core . info ( "Extracting Crystal build" ) ;
392
398
return ToolCache . extractZip ( downloadedPath ) ;
393
399
}
394
400
395
- function githubGet ( request ) {
396
- Core . debug ( request ) ;
397
- return Octokit . request . defaults ( {
398
- headers : { "authorization" : "token " + Core . getInput ( "token" ) } ,
399
- } ) ( request ) ;
400
- }
401
+ const github = new Octokit ( { auth : Core . getInput ( "token" ) || null } ) ;
401
402
402
- async function githubDownloadViaRedirect ( request ) {
403
- request . request = { redirect : "manual" } ;
404
- const resp = await githubGet ( request ) ;
405
- return ToolCache . downloadTool ( resp . headers [ "location" ] ) ;
403
+ async function * getItemsFromPages ( pages ) {
404
+ for await ( const page of github . paginate . iterator ( pages ) ) {
405
+ for ( const item of page . data ) {
406
+ yield item ;
407
+ }
408
+ }
406
409
}
407
410
408
411
async function onlySubdir ( path ) {
0 commit comments