Skip to content

Commit 7716bb3

Browse files
committed
Improve handling of download redirects
1 parent 52c91cb commit 7716bb3

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

index.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ async function findRelease({name, apiBase, tag}) {
207207
async function findLatestCommit({name, apiBase, branch = "master"}) {
208208
Core.info(`Looking for latest ${name} commit`);
209209
const commitsResp = await githubGet({
210-
url: apiBase + "/commits/" + branch,
210+
url: apiBase + "/commits/:branch",
211+
"branch": branch,
211212
});
212213
const commit = commitsResp.data;
213214
Core.info(`Found ${name} commit ${commit["html_url"]}`);
@@ -220,8 +221,11 @@ async function downloadCrystalRelease(suffix, version = null, destination = null
220221

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

223-
Core.info(`Downloading Crystal build from ${asset["browser_download_url"]}`);
224-
const downloadedPath = await ToolCache.downloadTool(asset["browser_download_url"]);
224+
Core.info(`Downloading Crystal build from ${asset["url"]}`);
225+
const downloadedPath = await githubDownloadViaRedirect({
226+
url: asset["url"],
227+
headers: {"accept": "application/octet-stream"},
228+
});
225229

226230
Core.info("Extracting Crystal build");
227231
return onlySubdir(await ToolCache.extractTar(downloadedPath, destination));
@@ -236,15 +240,11 @@ async function downloadSource({name, apiBase, version = null, destination = null
236240
ref = release["tag_name"];
237241
}
238242

239-
const zipballLinkResp = await githubGet({
243+
Core.info(`Downloading ${name} source for ${ref}`);
244+
const downloadedPath = await githubDownloadViaRedirect({
240245
url: apiBase + "/zipball/:ref",
241246
"ref": ref,
242-
request: {redirect: "manual"},
243247
});
244-
const downloadUrl = zipballLinkResp.headers["location"];
245-
246-
Core.info(`Downloading ${name} source from ${downloadUrl}`);
247-
const downloadedPath = await ToolCache.downloadTool(downloadUrl);
248248
Core.info(`Extracting ${name} source`);
249249
const path = await onlySubdir(await ToolCache.extractZip(downloadedPath, destination));
250250

@@ -361,15 +361,11 @@ async function downloadCrystalNightlyForWindows(destination = null) {
361361
});
362362
const artifact = artifactsResp.data["artifacts"].find((x) => x.name === "crystal");
363363

364-
const artifactLinkResp = await githubGet({
364+
Core.info("Downloading Crystal build");
365+
return githubDownloadViaRedirect({
365366
url: GitHubApiBase + "/actions/artifacts/:artifact_id/zip",
366367
"artifact_id": artifact.id,
367-
request: {redirect: "manual"},
368368
});
369-
const downloadUrl = artifactLinkResp.headers["location"];
370-
371-
Core.info("Downloading Crystal build");
372-
return ToolCache.downloadTool(downloadUrl);
373369
};
374370

375371
const [{path: srcPath}, exeDownloadedPath] = await Promise.all([
@@ -388,6 +384,12 @@ function githubGet(request) {
388384
})(request);
389385
}
390386

387+
async function githubDownloadViaRedirect(request) {
388+
request.request = {redirect: "manual"};
389+
const resp = await githubGet(request);
390+
return ToolCache.downloadTool(resp.headers["location"]);
391+
}
392+
391393
async function onlySubdir(path) {
392394
const [subDir] = await FS.readdir(path);
393395
return Path.join(path, subDir);

0 commit comments

Comments
 (0)