Skip to content

Commit 2437038

Browse files
committed
Migrate to @octokit/rest from @octokit/request
1 parent 4700143 commit 2437038

File tree

3 files changed

+210
-56
lines changed

3 files changed

+210
-56
lines changed

index.js

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const ToolCache = require("@actions/tool-cache");
33
const Cache = require("@actions/cache");
44
const IO = require("@actions/io");
55
const Glob = require("@actions/glob");
6-
const Octokit = require("@octokit/request");
6+
const {Octokit} = require("@octokit/rest");
77
const fetch = require("node-fetch");
88
const Path = require("path");
99
const ChildProcess = require("child_process");
@@ -221,7 +221,7 @@ async function installShards({shards, path}, crystalPromise) {
221221
if (NumericVersion.test(shards)) {
222222
shards = "v" + shards;
223223
}
224-
const ref = await findRef({name: "Shards", apiBase: GitHubApiBaseShards, version: shards});
224+
const ref = await findRef({name: "Shards", repo: RepoShards, version: shards});
225225
Core.setOutput("shards", ref);
226226

227227
const cacheKey = `install-shards-v1-${ref}--${getArch()}-${getPlatform()}`;
@@ -234,7 +234,7 @@ async function installShards({shards, path}, crystalPromise) {
234234
}
235235
if (!restored) {
236236
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});
238238
await IO.mv(await fetchSrcTask, path);
239239
await crystalPromise;
240240
await rebuildShards({path});
@@ -261,64 +261,69 @@ async function rebuildShards({path}) {
261261
Core.endGroup();
262262
}
263263

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"};
266266
const CircleApiBase = "https://circleci.com/api/v1.1/project/github/crystal-lang/crystal";
267267

268-
async function findRelease({name, apiBase, tag}) {
268+
async function findRelease({name, repo, tag}) {
269269
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));
273273
const release = releasesResp.data;
274274
Core.info(`Found ${name} release ${release["html_url"]}`);
275275
return release;
276276
}
277277

278-
async function findLatestCommit({name, apiBase, branch = "master"}) {
278+
async function findLatestCommit({name, repo, branch = "master"}) {
279279
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,
283282
});
284283
const commit = commitsResp.data;
285284
Core.info(`Found ${name} commit ${commit["html_url"]}`);
286285
return commit["sha"];
287286
}
288287

289288
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});
291290
Core.setOutput("crystal", release["tag_name"]);
292291

293292
const asset = release["assets"].find((a) => a["name"].endsWith([`-${suffix}.tar.gz`]));
294293

295294
Core.info(`Downloading Crystal build from ${asset["url"]}`);
296-
const downloadedPath = await githubDownloadViaRedirect({
295+
const resp = await github.request({
297296
url: asset["url"],
298297
headers: {"accept": "application/octet-stream"},
298+
request: {redirect: "manual"},
299299
});
300+
const url = resp.headers["location"];
300301

302+
const downloadedPath = await ToolCache.downloadTool(url);
301303
Core.info("Extracting Crystal build");
302304
const extractedPath = await ToolCache.extractTar(downloadedPath);
303305
return onlySubdir(extractedPath);
304306
}
305307

306-
async function findRef({name, apiBase, version}) {
308+
async function findRef({name, repo, version}) {
307309
if (version === Nightly) {
308-
return findLatestCommit({name, apiBase});
310+
return findLatestCommit({name, repo});
309311
} else if (version === Latest) {
310-
const release = await findRelease({name, apiBase});
312+
const release = await findRelease({name, repo});
311313
return release["tag_name"];
312314
}
313315
return version;
314316
}
315317

316-
async function downloadSource({name, apiBase, ref}) {
318+
async function downloadSource({name, repo, ref}) {
317319
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"},
321324
});
325+
const url = resp.headers["location"];
326+
const downloadedPath = await ToolCache.downloadTool(url);
322327
Core.info(`Extracting ${name} source`);
323328
return onlySubdir(await ToolCache.extractZip(downloadedPath));
324329
}
@@ -367,42 +372,40 @@ async function installCrystalForWindows({crystal, arch = "x86_64", path}) {
367372
async function downloadCrystalNightlyForWindows() {
368373
Core.info("Looking for latest Crystal build");
369374

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,
373378
});
374379
const [workflowRun] = runsResp.data["workflow_runs"];
375380
const {"head_sha": ref, "id": runId} = workflowRun;
376381
Core.info(`Found Crystal release ${workflowRun["html_url"]}`);
377382
Core.setOutput("crystal", ref);
378383

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,
382386
});
383387
const artifact = artifactsResp.data["artifacts"].find((x) => x.name === "crystal");
384388

385389
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"},
389393
});
394+
const url = resp.headers["location"];
395+
const downloadedPath = await ToolCache.downloadTool(url);
390396

391397
Core.info("Extracting Crystal build");
392398
return ToolCache.extractZip(downloadedPath);
393399
}
394400

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});
401402

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+
}
406409
}
407410

408411
async function onlySubdir(path) {

0 commit comments

Comments
 (0)