Skip to content

Commit 1335412

Browse files
henrymercerCopilot
andcommitted
Omit unknown HTTP status codes from download errors
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 5bdaf5d commit 1335412

3 files changed

Lines changed: 59 additions & 10 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/tools-download.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { once } from "events";
22
import * as fs from "fs";
3+
import { ClientRequest, IncomingMessage } from "http";
34
import * as path from "path";
45

6+
import * as core from "@actions/core";
57
import * as toolcache from "@actions/tool-cache";
68
import test from "ava";
9+
import { https } from "follow-redirects";
710
import nock from "nock";
811
import * as sinon from "sinon";
912

@@ -158,6 +161,48 @@ test.serial(
158161
},
159162
);
160163

164+
test.serial(
165+
"downloadAndExtract omits an unknown HTTP status from the error message",
166+
async (t) => {
167+
await withTmpDir(async (tmpDir) => {
168+
sinon.stub(process, "platform").value("linux");
169+
const archivePath = path.join(tmpDir, "codeql-bundle.tar.zst");
170+
const destination = path.join(tmpDir, "codeql");
171+
const response = sinon.createStubInstance(IncomingMessage);
172+
response.statusCode = undefined;
173+
sinon
174+
.stub(https, "get")
175+
.callsArgWith(2, response)
176+
.returns(sinon.createStubInstance(ClientRequest));
177+
const warning = sinon.stub(core, "warning");
178+
const downloadTool = sinon
179+
.stub(toolcache, "downloadTool")
180+
.resolves(archivePath);
181+
const extract = sinon.stub(tar, "extract").resolves(destination);
182+
const extractTarZst = sinon.stub(tar, "extractTarZst").resolves();
183+
184+
await downloadAndExtract(
185+
"https://example.com/codeql-bundle.tar.zst",
186+
"zstd",
187+
destination,
188+
undefined,
189+
{},
190+
{ type: "gnu", version: "1.34" },
191+
getRunnerLogger(true),
192+
);
193+
194+
t.is(
195+
warning.firstCall.args[0],
196+
"Failed to download and extract CodeQL bundle using streaming with error: Failed to download CodeQL bundle from https://example.com/codeql-bundle.tar.zst.",
197+
);
198+
t.true(response.resume.calledOnce);
199+
t.false(extractTarZst.called);
200+
t.true(downloadTool.calledOnce);
201+
t.true(extract.calledOnce);
202+
});
203+
},
204+
);
205+
161206
test.serial(
162207
"downloadAndExtract reports only the total duration when streaming extraction",
163208
async (t) => {

‎src/tools-download.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,15 @@ async function downloadAndExtractZstdWithStreaming(
200200
});
201201
});
202202

203-
if (response.statusCode !== 200) {
203+
const statusCode = response.statusCode ?? 0;
204+
if (statusCode !== 200) {
204205
// Discard the response body so that the connection can be released.
205206
response.resume();
206-
throw new HTTPError(
207-
`Failed to download CodeQL bundle from ${codeqlURL}. HTTP status code: ${response.statusCode}.`,
208-
response.statusCode ?? 0,
209-
);
207+
let message = `Failed to download CodeQL bundle from ${codeqlURL}.`;
208+
if (statusCode !== 0) {
209+
message += ` HTTP status code: ${statusCode}.`;
210+
}
211+
throw new HTTPError(message, statusCode);
210212
}
211213

212214
await tar.extractTarZst(response, dest, tarVersion, logger);

0 commit comments

Comments
 (0)