diff --git a/dist/index.js b/dist/index.js index d46377f0..87a0a628 100644 --- a/dist/index.js +++ b/dist/index.js @@ -3627,6 +3627,9 @@ function downloadReleaseAssets(dData, out, token) { } function downloadFile(asset, outputPath, token) { return __awaiter(this, void 0, void 0, function* () { + const ghClient = new thc.HttpClient("gh-api-client", [], { + allowRedirects: false + }); const headers = { Accept: "application/octet-stream" }; @@ -3637,16 +3640,29 @@ function downloadFile(asset, outputPath, token) { headers["Authorization"] = `token ${token}`; } core.info(`Downloading file: ${asset.fileName} to: ${outputPath}`); - const response = yield httpClient.get(asset.url, headers); - if (response.message.statusCode !== 200) { + const response = yield ghClient.get(asset.url, headers); + if (response.message.statusCode === 200) { + return saveFile(outputPath, asset.fileName, response); + } + else if (response.message.statusCode === 302) { + delete headers["Authorization"]; + const assetLocation = response.message.headers.location; + const assetResponse = yield ghClient.get(assetLocation, headers); + return saveFile(outputPath, asset.fileName, assetResponse); + } + else { const err = new Error(`Unexpected response: ${response.message.statusCode}`); throw err; } - const outFilePath = path.resolve(outputPath, asset.fileName); + }); +} +function saveFile(outputPath, fileName, httpClientResponse) { + return __awaiter(this, void 0, void 0, function* () { + const outFilePath = path.resolve(outputPath, fileName); const fileStream = fs.createWriteStream(outFilePath); return new Promise((resolve, reject) => { fileStream.on("error", err => reject(err)); - const outStream = response.message.pipe(fileStream); + const outStream = httpClientResponse.message.pipe(fileStream); outStream.on("close", () => { resolve(outFilePath); }); diff --git a/src/download.ts b/src/download.ts index 4c3ac46d..39aeb51b 100644 --- a/src/download.ts +++ b/src/download.ts @@ -5,7 +5,7 @@ import * as io from "@actions/io" import * as thc from "typed-rest-client/HttpClient" import {IReleaseDownloadSettings} from "./download-settings" import {GithubRelease, DownloadMetaData} from "./gh-api" -import {IHeaders} from "typed-rest-client/Interfaces" +import {IHeaders, IHttpClientResponse} from "typed-rest-client/Interfaces" const API_ROOT = "https://api.github.com/repos" const httpClient: thc.HttpClient = new thc.HttpClient("gh-api-client") @@ -195,6 +195,11 @@ async function downloadFile( outputPath: string, token: string ): Promise { + // Temporary fix for https://github.com/microsoft/typed-rest-client/issues/302 + const ghClient: thc.HttpClient = new thc.HttpClient("gh-api-client", [], { + allowRedirects: false + }) + const headers: IHeaders = { Accept: "application/octet-stream" } @@ -208,20 +213,34 @@ async function downloadFile( } core.info(`Downloading file: ${asset.fileName} to: ${outputPath}`) - const response = await httpClient.get(asset.url, headers) - - if (response.message.statusCode !== 200) { + const response = await ghClient.get(asset.url, headers) + + if (response.message.statusCode === 200) { + return saveFile(outputPath, asset.fileName, response) + } else if (response.message.statusCode === 302) { + delete headers["Authorization"] + const assetLocation = response.message.headers.location as string + const assetResponse = await ghClient.get(assetLocation, headers) + return saveFile(outputPath, asset.fileName, assetResponse) + } else { const err: Error = new Error( `Unexpected response: ${response.message.statusCode}` ) throw err } - const outFilePath: string = path.resolve(outputPath, asset.fileName) +} + +async function saveFile( + outputPath: string, + fileName: string, + httpClientResponse: IHttpClientResponse +): Promise { + const outFilePath: string = path.resolve(outputPath, fileName) const fileStream: NodeJS.WritableStream = fs.createWriteStream(outFilePath) return new Promise((resolve, reject) => { fileStream.on("error", err => reject(err)) - const outStream = response.message.pipe(fileStream) + const outStream = httpClientResponse.message.pipe(fileStream) outStream.on("close", () => { resolve(outFilePath)