Handle CLI errors when creating debug artifacts

This commit is contained in:
Henry Mercer 2024-09-16 18:39:39 +02:00
parent 5618c9fc1e
commit d061f2cdd0
3 changed files with 91 additions and 45 deletions

49
lib/debug-artifacts.js generated
View file

@ -71,24 +71,46 @@ async function uploadCombinedSarifArtifacts() {
} }
} }
} }
async function uploadAllAvailableDebugArtifacts(config, logger) { function tryGetSarifResultPath(config, language, logger) {
const filesToUpload = []; try {
const analyzeActionOutputDir = process.env[environment_1.EnvVar.SARIF_RESULTS_OUTPUT_DIR]; const analyzeActionOutputDir = process.env[environment_1.EnvVar.SARIF_RESULTS_OUTPUT_DIR];
for (const lang of config.languages) {
// Add any SARIF files, if they exist
if (analyzeActionOutputDir !== undefined && if (analyzeActionOutputDir !== undefined &&
fs.existsSync(analyzeActionOutputDir) && fs.existsSync(analyzeActionOutputDir) &&
fs.lstatSync(analyzeActionOutputDir).isDirectory()) { fs.lstatSync(analyzeActionOutputDir).isDirectory()) {
const sarifFile = path.resolve(analyzeActionOutputDir, `${lang}.sarif`); const sarifFile = path.resolve(analyzeActionOutputDir, `${language}.sarif`);
// Move SARIF to DB location so that they can be uploaded with the same root directory as the other artifacts. // Move SARIF to DB location so that they can be uploaded with the same root directory as the other artifacts.
if (fs.existsSync(sarifFile)) { if (fs.existsSync(sarifFile)) {
const sarifInDbLocation = path.resolve(config.dbLocation, `${lang}.sarif`); const sarifInDbLocation = path.resolve(config.dbLocation, `${language}.sarif`);
fs.copyFileSync(sarifFile, sarifInDbLocation); fs.copyFileSync(sarifFile, sarifInDbLocation);
filesToUpload.push(sarifInDbLocation); return [sarifInDbLocation];
} }
} }
}
catch (e) {
logger.warning(`Failed to find SARIF results path for ${language}. ${(0, util_1.wrapError)(e)}`);
}
return [];
}
async function tryBundleDatabase(config, language, logger) {
try {
if (!(0, analyze_1.dbIsFinalized)(config, language, logger)) {
return [await createPartialDatabaseBundle(config, language)];
}
else {
return [await createDatabaseBundleCli(config, language)];
}
}
catch (e) {
logger.warning(`Failed to bundle database for ${language}. ${(0, util_1.wrapError)(e)}`);
return [];
}
}
async function uploadAllAvailableDebugArtifacts(config, logger) {
const filesToUpload = [];
for (const language of config.languages) {
filesToUpload.push(...tryGetSarifResultPath(config, language, logger));
// Add any log files // Add any log files
const databaseDirectory = (0, util_1.getCodeQLDatabasePath)(config, lang); const databaseDirectory = (0, util_1.getCodeQLDatabasePath)(config, language);
const logsDirectory = path.resolve(databaseDirectory, "log"); const logsDirectory = path.resolve(databaseDirectory, "log");
if ((0, util_1.doesDirectoryExist)(logsDirectory)) { if ((0, util_1.doesDirectoryExist)(logsDirectory)) {
filesToUpload.push(...(0, util_1.listFolder)(logsDirectory)); filesToUpload.push(...(0, util_1.listFolder)(logsDirectory));
@ -99,14 +121,7 @@ async function uploadAllAvailableDebugArtifacts(config, logger) {
filesToUpload.push(...(0, util_1.listFolder)(multiLanguageTracingLogsDirectory)); filesToUpload.push(...(0, util_1.listFolder)(multiLanguageTracingLogsDirectory));
} }
// Add database bundle // Add database bundle
let databaseBundlePath; filesToUpload.push(...(await tryBundleDatabase(config, language, logger)));
if (!(0, analyze_1.dbIsFinalized)(config, lang, logger)) {
databaseBundlePath = await createPartialDatabaseBundle(config, lang);
}
else {
databaseBundlePath = await createDatabaseBundleCli(config, lang);
}
filesToUpload.push(databaseBundlePath);
} }
await uploadDebugArtifacts(filesToUpload, config.dbLocation, config.debugArtifactName); await uploadDebugArtifacts(filesToUpload, config.dbLocation, config.debugArtifactName);
} }

File diff suppressed because one or more lines are too long

View file

@ -18,6 +18,7 @@ import {
doesDirectoryExist, doesDirectoryExist,
getCodeQLDatabasePath, getCodeQLDatabasePath,
listFolder, listFolder,
wrapError,
} from "./util"; } from "./util";
export function sanitizeArifactName(name: string): string { export function sanitizeArifactName(name: string): string {
@ -65,34 +66,70 @@ export async function uploadCombinedSarifArtifacts() {
} }
} }
function tryGetSarifResultPath(
config: Config,
language: Language,
logger: Logger,
): string[] {
try {
const analyzeActionOutputDir = process.env[EnvVar.SARIF_RESULTS_OUTPUT_DIR];
if (
analyzeActionOutputDir !== undefined &&
fs.existsSync(analyzeActionOutputDir) &&
fs.lstatSync(analyzeActionOutputDir).isDirectory()
) {
const sarifFile = path.resolve(
analyzeActionOutputDir,
`${language}.sarif`,
);
// Move SARIF to DB location so that they can be uploaded with the same root directory as the other artifacts.
if (fs.existsSync(sarifFile)) {
const sarifInDbLocation = path.resolve(
config.dbLocation,
`${language}.sarif`,
);
fs.copyFileSync(sarifFile, sarifInDbLocation);
return [sarifInDbLocation];
}
}
} catch (e) {
logger.warning(
`Failed to find SARIF results path for ${language}. ${wrapError(e)}`,
);
}
return [];
}
async function tryBundleDatabase(
config: Config,
language: Language,
logger: Logger,
): Promise<string[]> {
try {
if (!dbIsFinalized(config, language, logger)) {
return [await createPartialDatabaseBundle(config, language)];
} else {
return [await createDatabaseBundleCli(config, language)];
}
} catch (e) {
logger.warning(
`Failed to bundle database for ${language}. ${wrapError(e)}`,
);
return [];
}
}
export async function uploadAllAvailableDebugArtifacts( export async function uploadAllAvailableDebugArtifacts(
config: Config, config: Config,
logger: Logger, logger: Logger,
) { ) {
const filesToUpload: string[] = []; const filesToUpload: string[] = [];
const analyzeActionOutputDir = process.env[EnvVar.SARIF_RESULTS_OUTPUT_DIR]; for (const language of config.languages) {
for (const lang of config.languages) { filesToUpload.push(...tryGetSarifResultPath(config, language, logger));
// Add any SARIF files, if they exist
if (
analyzeActionOutputDir !== undefined &&
fs.existsSync(analyzeActionOutputDir) &&
fs.lstatSync(analyzeActionOutputDir).isDirectory()
) {
const sarifFile = path.resolve(analyzeActionOutputDir, `${lang}.sarif`);
// Move SARIF to DB location so that they can be uploaded with the same root directory as the other artifacts.
if (fs.existsSync(sarifFile)) {
const sarifInDbLocation = path.resolve(
config.dbLocation,
`${lang}.sarif`,
);
fs.copyFileSync(sarifFile, sarifInDbLocation);
filesToUpload.push(sarifInDbLocation);
}
}
// Add any log files // Add any log files
const databaseDirectory = getCodeQLDatabasePath(config, lang); const databaseDirectory = getCodeQLDatabasePath(config, language);
const logsDirectory = path.resolve(databaseDirectory, "log"); const logsDirectory = path.resolve(databaseDirectory, "log");
if (doesDirectoryExist(logsDirectory)) { if (doesDirectoryExist(logsDirectory)) {
filesToUpload.push(...listFolder(logsDirectory)); filesToUpload.push(...listFolder(logsDirectory));
@ -108,13 +145,7 @@ export async function uploadAllAvailableDebugArtifacts(
} }
// Add database bundle // Add database bundle
let databaseBundlePath: string; filesToUpload.push(...(await tryBundleDatabase(config, language, logger)));
if (!dbIsFinalized(config, lang, logger)) {
databaseBundlePath = await createPartialDatabaseBundle(config, lang);
} else {
databaseBundlePath = await createDatabaseBundleCli(config, lang);
}
filesToUpload.push(databaseBundlePath);
} }
await uploadDebugArtifacts( await uploadDebugArtifacts(