Omit unknown HTTP status codes from download errors

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Henry Mercer
2026-09-14 11:41:48 +01:00
parent 5bdaf5d4af
commit 13354125bc
3 changed files with 59 additions and 10 deletions

View File

@@ -1,9 +1,12 @@
import { once } from "events";
import * as fs from "fs";
import { ClientRequest, IncomingMessage } from "http";
import * as path from "path";
import * as core from "@actions/core";
import * as toolcache from "@actions/tool-cache";
import test from "ava";
import { https } from "follow-redirects";
import nock from "nock";
import * as sinon from "sinon";
@@ -158,6 +161,48 @@ test.serial(
},
);
test.serial(
"downloadAndExtract omits an unknown HTTP status from the error message",
async (t) => {
await withTmpDir(async (tmpDir) => {
sinon.stub(process, "platform").value("linux");
const archivePath = path.join(tmpDir, "codeql-bundle.tar.zst");
const destination = path.join(tmpDir, "codeql");
const response = sinon.createStubInstance(IncomingMessage);
response.statusCode = undefined;
sinon
.stub(https, "get")
.callsArgWith(2, response)
.returns(sinon.createStubInstance(ClientRequest));
const warning = sinon.stub(core, "warning");
const downloadTool = sinon
.stub(toolcache, "downloadTool")
.resolves(archivePath);
const extract = sinon.stub(tar, "extract").resolves(destination);
const extractTarZst = sinon.stub(tar, "extractTarZst").resolves();
await downloadAndExtract(
"https://example.com/codeql-bundle.tar.zst",
"zstd",
destination,
undefined,
{},
{ type: "gnu", version: "1.34" },
getRunnerLogger(true),
);
t.is(
warning.firstCall.args[0],
"Failed to download and extract CodeQL bundle using streaming with error: Failed to download CodeQL bundle from https://example.com/codeql-bundle.tar.zst.",
);
t.true(response.resume.calledOnce);
t.false(extractTarZst.called);
t.true(downloadTool.calledOnce);
t.true(extract.calledOnce);
});
},
);
test.serial(
"downloadAndExtract reports only the total duration when streaming extraction",
async (t) => {

View File

@@ -200,13 +200,15 @@ async function downloadAndExtractZstdWithStreaming(
});
});
if (response.statusCode !== 200) {
const statusCode = response.statusCode ?? 0;
if (statusCode !== 200) {
// Discard the response body so that the connection can be released.
response.resume();
throw new HTTPError(
`Failed to download CodeQL bundle from ${codeqlURL}. HTTP status code: ${response.statusCode}.`,
response.statusCode ?? 0,
);
let message = `Failed to download CodeQL bundle from ${codeqlURL}.`;
if (statusCode !== 0) {
message += ` HTTP status code: ${statusCode}.`;
}
throw new HTTPError(message, statusCode);
}
await tar.extractTarZst(response, dest, tarVersion, logger);