Skip to content

Commit 69bc4cf

Browse files
committed
Refactor writeDiagnosticFile out of writeDiagnostic
1 parent daee991 commit 69bc4cf

2 files changed

Lines changed: 42 additions & 29 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 14 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/diagnostics.ts‎

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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-zA-Z0-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-zA-Z0-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

Comments
 (0)