@@ -55,6 +55,17 @@ export interface DiagnosticMessage {
55
55
attributes ?: { [ key : string ] : any } ;
56
56
}
57
57
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
+
58
69
/**
59
70
* Constructs a new diagnostic message with the specified id and name, as well as optional additional data.
60
71
*
@@ -76,9 +87,11 @@ export function makeDiagnostic(
76
87
}
77
88
78
89
/**
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.
80
92
*
81
93
* @param config The configuration that tells us where to store the diagnostic.
94
+ * @param language The language which the diagnostic is for.
82
95
* @param diagnostic The diagnostic message to add to the database.
83
96
*/
84
97
export function addDiagnostic (
@@ -88,30 +101,65 @@ export function addDiagnostic(
88
101
) {
89
102
const logger = getActionsLogger ( ) ;
90
103
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 ( ) ;
91
131
const diagnosticsPath = path . resolve (
92
- databasePath ,
132
+ getCodeQLDatabasePath ( config , language ) ,
93
133
"diagnostic" ,
94
134
"codeql-action" ,
95
135
) ;
96
136
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 } ) ;
107
140
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` ,
115
144
) ;
145
+
146
+ writeFileSync ( jsonPath , JSON . stringify ( diagnostic ) ) ;
147
+ } catch ( err ) {
148
+ logger . warning ( `Unable to write diagnostic message to database: ${ err } ` ) ;
116
149
}
117
150
}
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