|
| 1 | +import { writeFileSync } from "fs"; |
| 2 | +import path from "path"; |
| 3 | + |
| 4 | +import { Config } from "./config-utils"; |
| 5 | +import { getActionsLogger } from "./logging"; |
| 6 | + |
| 7 | +/** Represents a diagnostic message for the tool status page, etc. */ |
| 8 | +export interface DiagnosticMessage { |
| 9 | + /** ISO 8601 timestamp */ |
| 10 | + timestamp: string; |
| 11 | + source: { |
| 12 | + /** |
| 13 | + * An identifier under which it makes sense to group this diagnostic message. |
| 14 | + * This is used to build the SARIF reporting descriptor object. |
| 15 | + */ |
| 16 | + id: string; |
| 17 | + /** Display name for the ID. This is used to build the SARIF reporting descriptor object. */ |
| 18 | + name: string; |
| 19 | + /** |
| 20 | + * Name of the CodeQL extractor. This is used to identify which tool component the reporting |
| 21 | + * descriptor object should be nested under in SARIF. |
| 22 | + */ |
| 23 | + extractorName?: string; |
| 24 | + }; |
| 25 | + /** GitHub flavored Markdown formatted message. Should include inline links to any help pages. */ |
| 26 | + markdownMessage?: string; |
| 27 | + /** Plain text message. Used by components where the string processing needed to support Markdown is cumbersome. */ |
| 28 | + plaintextMessage?: string; |
| 29 | + /** List of help links intended to supplement the `plaintextMessage`. */ |
| 30 | + helpLinks?: string[]; |
| 31 | + /** SARIF severity */ |
| 32 | + severity?: "error" | "warning" | "note"; |
| 33 | + visibility?: { |
| 34 | + /** True if the message should be displayed on the status page (defaults to false) */ |
| 35 | + statusPage?: boolean; |
| 36 | + /** |
| 37 | + * True if the message should be counted in the diagnostics summary table printed by `codeql database analyze` |
| 38 | + * (defaults to false) |
| 39 | + */ |
| 40 | + cliSummaryTable?: boolean; |
| 41 | + /** True if the message should be sent to telemetry (defaults to false) */ |
| 42 | + telemetry?: boolean; |
| 43 | + }; |
| 44 | + location?: { |
| 45 | + /** Path to the affected file if appropriate, relative to the source root */ |
| 46 | + file?: string; |
| 47 | + startLine?: number; |
| 48 | + startColumn?: number; |
| 49 | + endLine?: number; |
| 50 | + endColumn?: number; |
| 51 | + }; |
| 52 | + /** Structured metadata about the diagnostic message */ |
| 53 | + attributes?: { [key: string]: any }; |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Constructs a new diagnostic message with the specified id and name, as well as optional additional data. |
| 58 | + * |
| 59 | + * @param id An identifier under which it makes sense to group this diagnostic message. |
| 60 | + * @param name Display name for the ID. |
| 61 | + * @param data Optional additional data to initialize the diagnostic with. |
| 62 | + * @returns Returns the new diagnostic message. |
| 63 | + */ |
| 64 | +export function makeDiagnostic( |
| 65 | + id: string, |
| 66 | + name: string, |
| 67 | + data: Partial<DiagnosticMessage> | undefined = undefined, |
| 68 | +): DiagnosticMessage { |
| 69 | + return { |
| 70 | + ...data, |
| 71 | + timestamp: data?.timestamp ?? new Date().toISOString(), |
| 72 | + source: { ...data?.source, id, name }, |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Writes the given diagnostic to the database. |
| 78 | + * |
| 79 | + * @param config The configuration that tells us where to store the diagnostic. |
| 80 | + * @param diagnostic The diagnostic message to add to the database. |
| 81 | + */ |
| 82 | +export function addDiagnostic(config: Config, diagnostic: DiagnosticMessage) { |
| 83 | + const logger = getActionsLogger(); |
| 84 | + const diagnosticsPath = path.resolve(config.dbLocation, "diagnostic"); |
| 85 | + const jsonPath = path.resolve( |
| 86 | + diagnosticsPath, |
| 87 | + `codeql-action-${diagnostic.timestamp}.json`, |
| 88 | + ); |
| 89 | + |
| 90 | + try { |
| 91 | + writeFileSync(jsonPath, JSON.stringify(diagnostic)); |
| 92 | + } catch (err) { |
| 93 | + logger.warning(`Unable to write diagnostic message to database: ${err}`); |
| 94 | + } |
| 95 | +} |
0 commit comments