@@ -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 */
8497export 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+ }
0 commit comments