@@ -177,6 +177,33 @@ export function addNoLanguageDiagnostic(
177177 }
178178}
179179
180+ /**
181+ * Writes {@link diagnostic} to a file in {@link diagnosticsPath}.
182+ */
183+ function writeDiagnosticFile (
184+ diagnosticsPath : string ,
185+ diagnostic : DiagnosticMessage ,
186+ ) {
187+ // Create the directory if it doesn't exist yet.
188+ mkdirSync ( diagnosticsPath , { recursive : true } ) ;
189+
190+ // Include a monotonically increasing suffix to avoid filename collisions
191+ // between diagnostics produced within the same millisecond.
192+ const uniqueSuffix = ( diagnosticCounter ++ ) . toString ( ) ;
193+ // We should only need to remove colons, but to be defensive, only allow a restricted set of
194+ // characters.
195+ const sanitizedTimestamp = diagnostic . timestamp . replace (
196+ / [ ^ a - z A - Z 0 - 9 . - ] / g,
197+ "" ,
198+ ) ;
199+ const jsonPath = path . resolve (
200+ diagnosticsPath ,
201+ `codeql-action-${ sanitizedTimestamp } -${ uniqueSuffix } .json` ,
202+ ) ;
203+
204+ writeFileSync ( jsonPath , JSON . stringify ( diagnostic ) ) ;
205+ }
206+
180207/**
181208 * Writes the given diagnostic to the database.
182209 *
@@ -200,24 +227,7 @@ function writeDiagnostic(
200227 ) ;
201228
202229 try {
203- // Create the directory if it doesn't exist yet.
204- mkdirSync ( diagnosticsPath , { recursive : true } ) ;
205-
206- // Include a monotonically increasing suffix to avoid filename collisions
207- // between diagnostics produced within the same millisecond.
208- const uniqueSuffix = ( diagnosticCounter ++ ) . toString ( ) ;
209- // We should only need to remove colons, but to be defensive, only allow a restricted set of
210- // characters.
211- const sanitizedTimestamp = diagnostic . timestamp . replace (
212- / [ ^ a - z A - Z 0 - 9 . - ] / g,
213- "" ,
214- ) ;
215- const jsonPath = path . resolve (
216- diagnosticsPath ,
217- `codeql-action-${ sanitizedTimestamp } -${ uniqueSuffix } .json` ,
218- ) ;
219-
220- writeFileSync ( jsonPath , JSON . stringify ( diagnostic ) ) ;
230+ writeDiagnosticFile ( diagnosticsPath , diagnostic ) ;
221231 } catch ( err ) {
222232 logger . warning ( `Unable to write diagnostic message to database: ${ err } ` ) ;
223233 logger . debug ( JSON . stringify ( diagnostic ) ) ;
0 commit comments