Skip to content

Commit

Permalink
Check that the database exists before writing diagnostics to it
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael B. Gale committed Apr 11, 2024
1 parent 96f44cb commit d4e7b0e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
23 changes: 15 additions & 8 deletions lib/diagnostics.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/diagnostics.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 20 additions & 12 deletions src/diagnostics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdirSync, writeFileSync } from "fs";
import { existsSync, mkdirSync, writeFileSync } from "fs";
import path from "path";

import { Config } from "./config-utils";
Expand Down Expand Up @@ -87,23 +87,31 @@ export function addDiagnostic(
diagnostic: DiagnosticMessage,
) {
const logger = getActionsLogger();
const databasePath = getCodeQLDatabasePath(config, language);
const diagnosticsPath = path.resolve(
getCodeQLDatabasePath(config, language),
databasePath,
"diagnostic",
"codeql-action",
);

try {
// Create the directory if it doesn't exist yet.
mkdirSync(diagnosticsPath, { recursive: true });
// Check that the database exists before writing to it.
if (existsSync(databasePath)) {
try {
// Create the directory if it doesn't exist yet.
mkdirSync(diagnosticsPath, { recursive: true });

const jsonPath = path.resolve(
diagnosticsPath,
`codeql-action-${diagnostic.timestamp}.json`,
);
const jsonPath = path.resolve(
diagnosticsPath,
`codeql-action-${diagnostic.timestamp}.json`,
);

writeFileSync(jsonPath, JSON.stringify(diagnostic));
} catch (err) {
logger.warning(`Unable to write diagnostic message to database: ${err}`);
writeFileSync(jsonPath, JSON.stringify(diagnostic));
} catch (err) {
logger.warning(`Unable to write diagnostic message to database: ${err}`);
}
} else {
logger.info(
`Writing a diagnostic for ${language}, but the database at ${databasePath} does not exist yet.`,
);
}
}

0 comments on commit d4e7b0e

Please sign in to comment.