Truncate autobuild errors to 10 lines

This commit is contained in:
Henry Mercer
2024-03-15 11:24:50 +00:00
parent 88a0b7abb3
commit 3edd1bf725
6 changed files with 61 additions and 8 deletions

9
lib/cli-errors.js generated
View File

@@ -103,8 +103,13 @@ function extractFatalErrors(error) {
}
function extractAutobuildErrors(error) {
const pattern = /.*\[autobuild\] \[ERROR\] (.*)/gi;
return ([...error.matchAll(pattern)].map((match) => match[1]).join("\n") ||
undefined);
let errorLines = [...error.matchAll(pattern)].map((match) => match[1]);
// Truncate if there are more than 10 matching lines.
if (errorLines.length > 10) {
errorLines = errorLines.slice(0, 10);
errorLines.push("(truncated)");
}
return errorLines.join("\n") || undefined;
}
function ensureEndsInPeriod(text) {
return text[text.length - 1] === "." ? text : `${text}.`;

File diff suppressed because one or more lines are too long

17
lib/codeql.test.js generated
View File

@@ -629,6 +629,23 @@ for (const { codeqlVersion, flagPassed, githubVersion, negativeFlagPassed, } of
" and finished here.",
});
});
(0, ava_1.default)("runTool truncates long autobuilder errors", async (t) => {
const stderr = Array.from({ length: 20 }, (_, i) => `[2019-09-18 12:00:00] [autobuild] [ERROR] line${i + 1}`).join("\n");
stubToolRunnerConstructor(1, stderr);
const codeqlObject = await codeql.getCodeQLForTesting();
sinon.stub(codeqlObject, "getVersion").resolves((0, testing_utils_1.makeVersionInfo)("2.12.4"));
sinon.stub(codeqlObject, "resolveExtractor").resolves("/path/to/extractor");
// safeWhich throws because of the test CodeQL object.
sinon.stub(safeWhich, "safeWhich").resolves("");
await t.throwsAsync(async () => await codeqlObject.runAutobuild(languages_1.Language.java, false), {
instanceOf: cli_errors_1.CommandInvocationError,
message: "We were unable to automatically build your code. Please provide manual build steps. " +
"For more information, see " +
"https://docs.github.com/en/code-security/code-scanning/troubleshooting-code-scanning/automatic-build-failed. " +
"Encountered the following error: " +
`${Array.from({ length: 10 }, (_, i) => `line${i + 1}`).join("\n")}\n(truncated)`,
});
});
(0, ava_1.default)("runTool outputs last line of stderr if fatal error could not be found", async (t) => {
const cliStderr = "line1\nline2\nline3\nline4\nline5";
stubToolRunnerConstructor(32, cliStderr);

File diff suppressed because one or more lines are too long

View File

@@ -109,10 +109,13 @@ function extractFatalErrors(error: string): string | undefined {
function extractAutobuildErrors(error: string): string | undefined {
const pattern = /.*\[autobuild\] \[ERROR\] (.*)/gi;
return (
[...error.matchAll(pattern)].map((match) => match[1]).join("\n") ||
undefined
);
let errorLines = [...error.matchAll(pattern)].map((match) => match[1]);
// Truncate if there are more than 10 matching lines.
if (errorLines.length > 10) {
errorLines = errorLines.slice(0, 10);
errorLines.push("(truncated)");
}
return errorLines.join("\n") || undefined;
}
function ensureEndsInPeriod(text: string): string {

View File

@@ -1002,6 +1002,34 @@ test("runTool summarizes autobuilder errors", async (t) => {
);
});
test("runTool truncates long autobuilder errors", async (t) => {
const stderr = Array.from(
{ length: 20 },
(_, i) => `[2019-09-18 12:00:00] [autobuild] [ERROR] line${i + 1}`,
).join("\n");
stubToolRunnerConstructor(1, stderr);
const codeqlObject = await codeql.getCodeQLForTesting();
sinon.stub(codeqlObject, "getVersion").resolves(makeVersionInfo("2.12.4"));
sinon.stub(codeqlObject, "resolveExtractor").resolves("/path/to/extractor");
// safeWhich throws because of the test CodeQL object.
sinon.stub(safeWhich, "safeWhich").resolves("");
await t.throwsAsync(
async () => await codeqlObject.runAutobuild(Language.java, false),
{
instanceOf: CommandInvocationError,
message:
"We were unable to automatically build your code. Please provide manual build steps. " +
"For more information, see " +
"https://docs.github.com/en/code-security/code-scanning/troubleshooting-code-scanning/automatic-build-failed. " +
"Encountered the following error: " +
`${Array.from({ length: 10 }, (_, i) => `line${i + 1}`).join(
"\n",
)}\n(truncated)`,
},
);
});
test("runTool outputs last line of stderr if fatal error could not be found", async (t) => {
const cliStderr = "line1\nline2\nline3\nline4\nline5";
stubToolRunnerConstructor(32, cliStderr);