Improve docs and method naming

This commit is contained in:
Henry Mercer 2024-09-17 10:58:00 +02:00
parent 782de45248
commit 78d398ebc6
3 changed files with 43 additions and 31 deletions

31
lib/debug-artifacts.js generated
View file

@ -72,11 +72,11 @@ async function uploadCombinedSarifArtifacts() {
} }
} }
/** /**
* Try to get the SARIF result path for the given language. * Try to prepare a SARIF result debug artifact for the given language.
* *
* If an error occurs, log it and return an empty list. * @return The path to that debug artifact, or undefined if an error occurs.
*/ */
function tryGetSarifResultPath(config, language, logger) { function tryPrepareSarifDebugArtifact(config, language, logger) {
try { try {
const analyzeActionOutputDir = process.env[environment_1.EnvVar.SARIF_RESULTS_OUTPUT_DIR]; const analyzeActionOutputDir = process.env[environment_1.EnvVar.SARIF_RESULTS_OUTPUT_DIR];
if (analyzeActionOutputDir !== undefined && if (analyzeActionOutputDir !== undefined &&
@ -87,37 +87,36 @@ function tryGetSarifResultPath(config, language, logger) {
if (fs.existsSync(sarifFile)) { if (fs.existsSync(sarifFile)) {
const sarifInDbLocation = path.resolve(config.dbLocation, `${language}.sarif`); const sarifInDbLocation = path.resolve(config.dbLocation, `${language}.sarif`);
fs.copyFileSync(sarifFile, sarifInDbLocation); fs.copyFileSync(sarifFile, sarifInDbLocation);
return [sarifInDbLocation]; return sarifInDbLocation;
} }
} }
} }
catch (e) { catch (e) {
logger.warning(`Failed to find SARIF results path for ${language}. Reason: ${(0, util_1.getErrorMessage)(e)}`); logger.warning(`Failed to find SARIF results path for ${language}. Reason: ${(0, util_1.getErrorMessage)(e)}`);
} }
return []; return undefined;
} }
/** /**
* Try to bundle the database for the given language. Return a list containing * Try to bundle the database for the given language.
* the path to the database bundle.
* *
* If an error occurs, log it and return an empty list. * @return The path to the database bundle, or undefined if an error occurs.
*/ */
async function tryBundleDatabase(config, language, logger) { async function tryBundleDatabase(config, language, logger) {
try { try {
if ((0, analyze_1.dbIsFinalized)(config, language, logger)) { if ((0, analyze_1.dbIsFinalized)(config, language, logger)) {
try { try {
return [await createDatabaseBundleCli(config, language)]; return await createDatabaseBundleCli(config, language);
} }
catch (e) { catch (e) {
logger.warning(`Failed to bundle database for ${language} using the CLI. ` + logger.warning(`Failed to bundle database for ${language} using the CLI. ` +
`Falling back to a partial bundle. Reason: ${(0, util_1.getErrorMessage)(e)}`); `Falling back to a partial bundle. Reason: ${(0, util_1.getErrorMessage)(e)}`);
} }
} }
return [await createPartialDatabaseBundle(config, language)]; return await createPartialDatabaseBundle(config, language);
} }
catch (e) { catch (e) {
logger.warning(`Failed to bundle database for ${language}. Reason: ${(0, util_1.getErrorMessage)(e)}`); logger.warning(`Failed to bundle database for ${language}. Reason: ${(0, util_1.getErrorMessage)(e)}`);
return []; return undefined;
} }
} }
/** /**
@ -129,7 +128,10 @@ async function tryUploadAllAvailableDebugArtifacts(config, logger) {
try { try {
const filesToUpload = []; const filesToUpload = [];
for (const language of config.languages) { for (const language of config.languages) {
filesToUpload.push(...tryGetSarifResultPath(config, language, logger)); const sarifResultDebugArtifact = tryPrepareSarifDebugArtifact(config, language, logger);
if (sarifResultDebugArtifact) {
filesToUpload.push(sarifResultDebugArtifact);
}
// Add any log files // Add any log files
const databaseDirectory = (0, util_1.getCodeQLDatabasePath)(config, language); const databaseDirectory = (0, util_1.getCodeQLDatabasePath)(config, language);
const logsDirectory = path.resolve(databaseDirectory, "log"); const logsDirectory = path.resolve(databaseDirectory, "log");
@ -142,7 +144,10 @@ async function tryUploadAllAvailableDebugArtifacts(config, logger) {
filesToUpload.push(...(0, util_1.listFolder)(multiLanguageTracingLogsDirectory)); filesToUpload.push(...(0, util_1.listFolder)(multiLanguageTracingLogsDirectory));
} }
// Add database bundle // Add database bundle
filesToUpload.push(...(await tryBundleDatabase(config, language, logger))); const databaseBundle = await tryBundleDatabase(config, language, logger);
if (databaseBundle) {
filesToUpload.push(databaseBundle);
}
} }
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

@ -67,15 +67,15 @@ export async function uploadCombinedSarifArtifacts() {
} }
/** /**
* Try to get the SARIF result path for the given language. * Try to prepare a SARIF result debug artifact for the given language.
* *
* If an error occurs, log it and return an empty list. * @return The path to that debug artifact, or undefined if an error occurs.
*/ */
function tryGetSarifResultPath( function tryPrepareSarifDebugArtifact(
config: Config, config: Config,
language: Language, language: Language,
logger: Logger, logger: Logger,
): string[] { ): string | undefined {
try { try {
const analyzeActionOutputDir = process.env[EnvVar.SARIF_RESULTS_OUTPUT_DIR]; const analyzeActionOutputDir = process.env[EnvVar.SARIF_RESULTS_OUTPUT_DIR];
if ( if (
@ -94,7 +94,7 @@ function tryGetSarifResultPath(
`${language}.sarif`, `${language}.sarif`,
); );
fs.copyFileSync(sarifFile, sarifInDbLocation); fs.copyFileSync(sarifFile, sarifInDbLocation);
return [sarifInDbLocation]; return sarifInDbLocation;
} }
} }
} catch (e) { } catch (e) {
@ -104,24 +104,23 @@ function tryGetSarifResultPath(
)}`, )}`,
); );
} }
return []; return undefined;
} }
/** /**
* Try to bundle the database for the given language. Return a list containing * Try to bundle the database for the given language.
* the path to the database bundle.
* *
* If an error occurs, log it and return an empty list. * @return The path to the database bundle, or undefined if an error occurs.
*/ */
async function tryBundleDatabase( async function tryBundleDatabase(
config: Config, config: Config,
language: Language, language: Language,
logger: Logger, logger: Logger,
): Promise<string[]> { ): Promise<string | undefined> {
try { try {
if (dbIsFinalized(config, language, logger)) { if (dbIsFinalized(config, language, logger)) {
try { try {
return [await createDatabaseBundleCli(config, language)]; return await createDatabaseBundleCli(config, language);
} catch (e) { } catch (e) {
logger.warning( logger.warning(
`Failed to bundle database for ${language} using the CLI. ` + `Failed to bundle database for ${language} using the CLI. ` +
@ -129,14 +128,14 @@ async function tryBundleDatabase(
); );
} }
} }
return [await createPartialDatabaseBundle(config, language)]; return await createPartialDatabaseBundle(config, language);
} catch (e) { } catch (e) {
logger.warning( logger.warning(
`Failed to bundle database for ${language}. Reason: ${getErrorMessage( `Failed to bundle database for ${language}. Reason: ${getErrorMessage(
e, e,
)}`, )}`,
); );
return []; return undefined;
} }
} }
@ -153,7 +152,14 @@ export async function tryUploadAllAvailableDebugArtifacts(
const filesToUpload: string[] = []; const filesToUpload: string[] = [];
for (const language of config.languages) { for (const language of config.languages) {
filesToUpload.push(...tryGetSarifResultPath(config, language, logger)); const sarifResultDebugArtifact = tryPrepareSarifDebugArtifact(
config,
language,
logger,
);
if (sarifResultDebugArtifact) {
filesToUpload.push(sarifResultDebugArtifact);
}
// Add any log files // Add any log files
const databaseDirectory = getCodeQLDatabasePath(config, language); const databaseDirectory = getCodeQLDatabasePath(config, language);
@ -172,9 +178,10 @@ export async function tryUploadAllAvailableDebugArtifacts(
} }
// Add database bundle // Add database bundle
filesToUpload.push( const databaseBundle = await tryBundleDatabase(config, language, logger);
...(await tryBundleDatabase(config, language, logger)), if (databaseBundle) {
); filesToUpload.push(databaseBundle);
}
} }
await uploadDebugArtifacts( await uploadDebugArtifacts(