mirror of
https://github.com/github/codeql-action.git
synced 2026-10-03 09:14:58 +00:00
Use a plain error when the HTTP status is unknown
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
14
lib/entry-points.js
generated
14
lib/entry-points.js
generated
@@ -151767,14 +151767,16 @@ async function downloadAndExtractZstdWithStreaming(codeqlURL, dest, authorizatio
|
||||
);
|
||||
});
|
||||
});
|
||||
const statusCode = response.statusCode ?? 0;
|
||||
if (statusCode !== 200) {
|
||||
if (response.statusCode !== 200) {
|
||||
response.resume();
|
||||
let message = `Failed to download CodeQL bundle from ${codeqlURL}.`;
|
||||
if (statusCode !== 0) {
|
||||
message += ` HTTP status code: ${statusCode}.`;
|
||||
const baseMessage = `Failed to download CodeQL bundle from ${codeqlURL}.`;
|
||||
if (response.statusCode !== void 0) {
|
||||
throw new HTTPError(
|
||||
`${baseMessage} HTTP status code: ${response.statusCode}.`,
|
||||
response.statusCode
|
||||
);
|
||||
}
|
||||
throw new HTTPError(message, statusCode);
|
||||
throw new Error(baseMessage);
|
||||
}
|
||||
await extractTarZst(response, dest, tarVersion, logger);
|
||||
}
|
||||
|
||||
@@ -14,14 +14,14 @@ import { getRunnerLogger } from "./logging";
|
||||
import * as tar from "./tar";
|
||||
import { setupTests } from "./testing-utils";
|
||||
import { downloadAndExtract } from "./tools-download";
|
||||
import { HTTPError, withTmpDir } from "./util";
|
||||
import * as util from "./util";
|
||||
|
||||
setupTests(test);
|
||||
|
||||
test.serial(
|
||||
"downloadAndExtract reports the durations when downloading before extracting",
|
||||
async (t) => {
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
await util.withTmpDir(async (tmpDir) => {
|
||||
const archivePath = path.join(tmpDir, "codeql-bundle.tar.gz");
|
||||
const destination = path.join(tmpDir, "codeql");
|
||||
sinon.stub(toolcache, "downloadTool").resolves(archivePath);
|
||||
@@ -47,7 +47,7 @@ test.serial(
|
||||
test.serial(
|
||||
"downloadAndExtract falls back to downloading before extracting if streaming fails",
|
||||
async (t) => {
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
await util.withTmpDir(async (tmpDir) => {
|
||||
sinon.stub(process, "platform").value("linux");
|
||||
const archivePath = path.join(tmpDir, "codeql-bundle.tar.zst");
|
||||
const destination = path.join(tmpDir, "codeql");
|
||||
@@ -88,7 +88,7 @@ test.serial(
|
||||
test.serial(
|
||||
"downloadAndExtract rethrows a 404 rather than retrying the download",
|
||||
async (t) => {
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
await util.withTmpDir(async (tmpDir) => {
|
||||
sinon.stub(process, "platform").value("linux");
|
||||
const destination = path.join(tmpDir, "codeql");
|
||||
const downloadTool = sinon.stub(toolcache, "downloadTool");
|
||||
@@ -108,7 +108,7 @@ test.serial(
|
||||
getRunnerLogger(true),
|
||||
),
|
||||
{
|
||||
instanceOf: HTTPError,
|
||||
instanceOf: util.HTTPError,
|
||||
message:
|
||||
"Failed to download CodeQL bundle from https://example.com/codeql-bundle.tar.zst. HTTP status code: 404.",
|
||||
},
|
||||
@@ -126,7 +126,7 @@ test.serial(
|
||||
test.serial(
|
||||
"downloadAndExtract falls back to downloading before extracting on a server error",
|
||||
async (t) => {
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
await util.withTmpDir(async (tmpDir) => {
|
||||
sinon.stub(process, "platform").value("linux");
|
||||
const archivePath = path.join(tmpDir, "codeql-bundle.tar.zst");
|
||||
const destination = path.join(tmpDir, "codeql");
|
||||
@@ -162,9 +162,10 @@ test.serial(
|
||||
);
|
||||
|
||||
test.serial(
|
||||
"downloadAndExtract omits an unknown HTTP status from the error message",
|
||||
"downloadAndExtract handles an unknown status as a non-HTTP error",
|
||||
async (t) => {
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
const asHTTPError = sinon.spy(util, "asHTTPError");
|
||||
await util.withTmpDir(async (tmpDir) => {
|
||||
sinon.stub(process, "platform").value("linux");
|
||||
const archivePath = path.join(tmpDir, "codeql-bundle.tar.zst");
|
||||
const destination = path.join(tmpDir, "codeql");
|
||||
@@ -200,13 +201,18 @@ test.serial(
|
||||
t.true(downloadTool.calledOnce);
|
||||
t.true(extract.calledOnce);
|
||||
});
|
||||
|
||||
t.true(asHTTPError.calledOnce);
|
||||
t.true(asHTTPError.firstCall.args[0] instanceof Error);
|
||||
t.false(asHTTPError.firstCall.args[0] instanceof util.HTTPError);
|
||||
t.is(asHTTPError.firstCall.returnValue, undefined);
|
||||
},
|
||||
);
|
||||
|
||||
test.serial(
|
||||
"downloadAndExtract reports only the total duration when streaming extraction",
|
||||
async (t) => {
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
await util.withTmpDir(async (tmpDir) => {
|
||||
sinon.stub(process, "platform").value("linux");
|
||||
const downloadTool = sinon.stub(toolcache, "downloadTool");
|
||||
const extractTarZst = sinon
|
||||
|
||||
@@ -200,15 +200,17 @@ async function downloadAndExtractZstdWithStreaming(
|
||||
});
|
||||
});
|
||||
|
||||
const statusCode = response.statusCode ?? 0;
|
||||
if (statusCode !== 200) {
|
||||
if (response.statusCode !== 200) {
|
||||
// Discard the response body so that the connection can be released.
|
||||
response.resume();
|
||||
let message = `Failed to download CodeQL bundle from ${codeqlURL}.`;
|
||||
if (statusCode !== 0) {
|
||||
message += ` HTTP status code: ${statusCode}.`;
|
||||
const baseMessage = `Failed to download CodeQL bundle from ${codeqlURL}.`;
|
||||
if (response.statusCode !== undefined) {
|
||||
throw new HTTPError(
|
||||
`${baseMessage} HTTP status code: ${response.statusCode}.`,
|
||||
response.statusCode,
|
||||
);
|
||||
}
|
||||
throw new HTTPError(message, statusCode);
|
||||
throw new Error(baseMessage);
|
||||
}
|
||||
|
||||
await tar.extractTarZst(response, dest, tarVersion, logger);
|
||||
|
||||
Reference in New Issue
Block a user