Refactor writeDiagnosticFile out of writeDiagnostic

This commit is contained in:
Michael B. Gale
2026-10-09 18:56:55 +01:00
parent d6b8ed80f2
commit 11fd255a0b
2 changed files with 42 additions and 29 deletions

25
lib/entry-points.js generated
View File

@@ -173028,6 +173028,19 @@ function addNoLanguageDiagnostic(config, diagnostic) {
unwrittenDefaultLanguageDiagnostics.push(diagnostic);
}
}
function writeDiagnosticFile(diagnosticsPath, diagnostic) {
(0, import_fs.mkdirSync)(diagnosticsPath, { recursive: true });
const uniqueSuffix = (diagnosticCounter++).toString();
const sanitizedTimestamp = diagnostic.timestamp.replace(
/[^a-zA-Z0-9.-]/g,
""
);
const jsonPath = import_path2.default.resolve(
diagnosticsPath,
`codeql-action-${sanitizedTimestamp}-${uniqueSuffix}.json`
);
(0, import_fs.writeFileSync)(jsonPath, JSON.stringify(diagnostic));
}
function writeDiagnostic(config, language, diagnostic) {
const logger2 = getActionsLogger();
const databasePath = language ? getCodeQLDatabasePath(config, language) : config.dbLocation;
@@ -173037,17 +173050,7 @@ function writeDiagnostic(config, language, diagnostic) {
"codeql-action"
);
try {
(0, import_fs.mkdirSync)(diagnosticsPath, { recursive: true });
const uniqueSuffix = (diagnosticCounter++).toString();
const sanitizedTimestamp = diagnostic.timestamp.replace(
/[^a-zA-Z0-9.-]/g,
""
);
const jsonPath = import_path2.default.resolve(
diagnosticsPath,
`codeql-action-${sanitizedTimestamp}-${uniqueSuffix}.json`
);
(0, import_fs.writeFileSync)(jsonPath, JSON.stringify(diagnostic));
writeDiagnosticFile(diagnosticsPath, diagnostic);
} catch (err) {
logger2.warning(`Unable to write diagnostic message to database: ${err}`);
logger2.debug(JSON.stringify(diagnostic));

View File

@@ -177,6 +177,33 @@ export function addNoLanguageDiagnostic(
}
}
/**
* Writes {@link diagnostic} to a file in {@link diagnosticsPath}.
*/
function writeDiagnosticFile(
diagnosticsPath: string,
diagnostic: DiagnosticMessage,
) {
// Create the directory if it doesn't exist yet.
mkdirSync(diagnosticsPath, { recursive: true });
// Include a monotonically increasing suffix to avoid filename collisions
// between diagnostics produced within the same millisecond.
const uniqueSuffix = (diagnosticCounter++).toString();
// We should only need to remove colons, but to be defensive, only allow a restricted set of
// characters.
const sanitizedTimestamp = diagnostic.timestamp.replace(
/[^a-zA-Z0-9.-]/g,
"",
);
const jsonPath = path.resolve(
diagnosticsPath,
`codeql-action-${sanitizedTimestamp}-${uniqueSuffix}.json`,
);
writeFileSync(jsonPath, JSON.stringify(diagnostic));
}
/**
* Writes the given diagnostic to the database.
*
@@ -200,24 +227,7 @@ function writeDiagnostic(
);
try {
// Create the directory if it doesn't exist yet.
mkdirSync(diagnosticsPath, { recursive: true });
// Include a monotonically increasing suffix to avoid filename collisions
// between diagnostics produced within the same millisecond.
const uniqueSuffix = (diagnosticCounter++).toString();
// We should only need to remove colons, but to be defensive, only allow a restricted set of
// characters.
const sanitizedTimestamp = diagnostic.timestamp.replace(
/[^a-zA-Z0-9.-]/g,
"",
);
const jsonPath = path.resolve(
diagnosticsPath,
`codeql-action-${sanitizedTimestamp}-${uniqueSuffix}.json`,
);
writeFileSync(jsonPath, JSON.stringify(diagnostic));
writeDiagnosticFile(diagnosticsPath, diagnostic);
} catch (err) {
logger.warning(`Unable to write diagnostic message to database: ${err}`);
logger.debug(JSON.stringify(diagnostic));