Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for download failure when rest-client follows http redirect #334

Merged
merged 1 commit into from
Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
};
Expand All @@ -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);
});
Expand Down
31 changes: 25 additions & 6 deletions src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -195,6 +195,11 @@ async function downloadFile(
outputPath: string,
token: string
): Promise<string> {
// 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"
}
Expand All @@ -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<string> {
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)
Expand Down