Skip to content

Commit 4b0172d

Browse files
committed
Store diagnostics in memory until the database is available
1 parent d4e7b0e commit 4b0172d

6 files changed

+118
-35
lines changed

lib/diagnostics.js

+41-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/diagnostics.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/diagnostics.ts

+67-19
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ export interface DiagnosticMessage {
5555
attributes?: { [key: string]: any };
5656
}
5757

58+
/** Represents a diagnostic message that has not yet been written to the database. */
59+
interface UnwrittenDiagnostic {
60+
/** The diagnostic message that has not yet been written. */
61+
diagnostic: DiagnosticMessage;
62+
/** The language the diagnostic is for. */
63+
language: Language;
64+
}
65+
66+
/** A list of diagnostics which have not yet been written to disk. */
67+
let unwrittenDiagnostics: UnwrittenDiagnostic[] = [];
68+
5869
/**
5970
* Constructs a new diagnostic message with the specified id and name, as well as optional additional data.
6071
*
@@ -76,9 +87,11 @@ export function makeDiagnostic(
7687
}
7788

7889
/**
79-
* Writes the given diagnostic to the database.
90+
* Adds the given diagnostic to the database. If the database does not yet exist,
91+
* the diagnostic will be written to it once it has been created.
8092
*
8193
* @param config The configuration that tells us where to store the diagnostic.
94+
* @param language The language which the diagnostic is for.
8295
* @param diagnostic The diagnostic message to add to the database.
8396
*/
8497
export function addDiagnostic(
@@ -88,30 +101,65 @@ export function addDiagnostic(
88101
) {
89102
const logger = getActionsLogger();
90103
const databasePath = getCodeQLDatabasePath(config, language);
104+
105+
// Check that the database exists before writing to it. If the database does not yet exist,
106+
// store the diagnostic in memory and write it later.
107+
if (existsSync(databasePath)) {
108+
writeDiagnostic(config, language, diagnostic);
109+
} else {
110+
logger.info(
111+
`Writing a diagnostic for ${language}, but the database at ${databasePath} does not exist yet.`,
112+
);
113+
114+
unwrittenDiagnostics.push({ diagnostic, language });
115+
}
116+
}
117+
118+
/**
119+
* Writes the given diagnostic to the database.
120+
*
121+
* @param config The configuration that tells us where to store the diagnostic.
122+
* @param language The language which the diagnostic is for.
123+
* @param diagnostic The diagnostic message to add to the database.
124+
*/
125+
function writeDiagnostic(
126+
config: Config,
127+
language: Language,
128+
diagnostic: DiagnosticMessage,
129+
) {
130+
const logger = getActionsLogger();
91131
const diagnosticsPath = path.resolve(
92-
databasePath,
132+
getCodeQLDatabasePath(config, language),
93133
"diagnostic",
94134
"codeql-action",
95135
);
96136

97-
// Check that the database exists before writing to it.
98-
if (existsSync(databasePath)) {
99-
try {
100-
// Create the directory if it doesn't exist yet.
101-
mkdirSync(diagnosticsPath, { recursive: true });
102-
103-
const jsonPath = path.resolve(
104-
diagnosticsPath,
105-
`codeql-action-${diagnostic.timestamp}.json`,
106-
);
137+
try {
138+
// Create the directory if it doesn't exist yet.
139+
mkdirSync(diagnosticsPath, { recursive: true });
107140

108-
writeFileSync(jsonPath, JSON.stringify(diagnostic));
109-
} catch (err) {
110-
logger.warning(`Unable to write diagnostic message to database: ${err}`);
111-
}
112-
} else {
113-
logger.info(
114-
`Writing a diagnostic for ${language}, but the database at ${databasePath} does not exist yet.`,
141+
const jsonPath = path.resolve(
142+
diagnosticsPath,
143+
`codeql-action-${diagnostic.timestamp}.json`,
115144
);
145+
146+
writeFileSync(jsonPath, JSON.stringify(diagnostic));
147+
} catch (err) {
148+
logger.warning(`Unable to write diagnostic message to database: ${err}`);
116149
}
117150
}
151+
152+
/** Writes all unwritten diagnostics to disk. */
153+
export function flushDiagnostics(config: Config) {
154+
const logger = getActionsLogger();
155+
logger.info(
156+
`Writing ${unwrittenDiagnostics.length} diagnostic(s) to database.`,
157+
);
158+
159+
for (const unwritten of unwrittenDiagnostics) {
160+
writeDiagnostic(config, unwritten.language, unwritten.diagnostic);
161+
}
162+
163+
// Reset the unwritten diagnostics array.
164+
unwrittenDiagnostics = [];
165+
}

src/init-action.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
import { getGitHubVersion } from "./api-client";
1717
import { CodeQL } from "./codeql";
1818
import * as configUtils from "./config-utils";
19-
import { addDiagnostic, makeDiagnostic } from "./diagnostics";
19+
import { addDiagnostic, flushDiagnostics, makeDiagnostic } from "./diagnostics";
2020
import { EnvVar } from "./environment";
2121
import { Feature, Features } from "./feature-flags";
2222
import { checkInstallPython311, initCodeQL, initConfig, runInit } from "./init";
@@ -522,6 +522,10 @@ async function run() {
522522
}
523523
}
524524

525+
// Write diagnostics to the database that we previously stored in memory because the database
526+
// did not exist until now.
527+
flushDiagnostics(config);
528+
525529
core.setOutput("codeql-path", config.codeQLCmd);
526530
} catch (unwrappedError) {
527531
const error = wrapError(unwrappedError);

0 commit comments

Comments
 (0)