FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
codeql-action/src/diagnostics.ts at default-setup-testing · github/codeql-action · GitHub
github
codeql-action
Repository navigation
Code
Issues
165
(165)
Pull requests
44
(44)
Actions
Security and quality
2
(2)
Insights
Expand file tree
Breadcrumbs
codeql-action
/
src
/
diagnostics.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
426 lines (383 loc) · 13.7 KB
Breadcrumbs
codeql-action
/
src
/
diagnostics.ts
Copy path
File metadata and controls
426 lines (383 loc) · 13.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import
*
as
fs
from
"fs"
;
import
path
from
"path"
;
import
type
{
ActionState
}
from
"./action-common"
;
import
{
getTemporaryDirectory
}
from
"./actions-util"
;
import
type
{
ActionConfig
}
from
"./config/action-config"
;
import
{
Language
}
from
"./languages"
;
import
{
getActionsLogger
,
type
Logger
}
from
"./logging"
;
import
{
getCodeQLDatabasePath
,
getErrorMessage
,
isErrnoException
,
}
from
"./util"
;
/** The name of the directory we use to temporarily store diagnostics. */
export
const
TEMP_DIAGNOSTIC_DIR_NAME
=
"codeql-action-temp-diagnostics"
;
export
const
CODEQL_ACTION_DIAGNOSTIC_SUBDIR_PARTS
=
[
"diagnostic"
,
"codeql-action"
,
]
;
/**
* Known tags for diagnostics. There is currently only "internal-error",
* but others may be added in the future.
*/
export
type
DiagnosticTag
=
"internal-error"
;
/** Optional information about the origin of a diagnostic. */
export
type
DiagnosticSourceOptions
=
{
/**
* Name of the CodeQL extractor. This is used to identify which tool component the reporting
* descriptor object should be nested under in SARIF.
*/
extractorName
?:
string
;
/** An array of tags for the diagnostic. */
tags
?:
DiagnosticTag
[
]
;
}
;
/** Represents information about the origin of a diagnostic. */
export
type
DiagnosticSource
=
{
/**
* An identifier under which it makes sense to group this diagnostic message.
* This is used to build the SARIF reporting descriptor object.
*/
id
:
string
;
/** Display name for the ID. This is used to build the SARIF reporting descriptor object. */
name
:
string
;
}
&
DiagnosticSourceOptions
;
/**
* Represents a diagnostic message for the tool status page, etc.
*
* Unlike {
@link
DiagnosticMessage}, properties which can automatically
* be populated are optional in this type.
*/
export
type
DiagnosticMessageOptions
=
{
/** ISO 8601 timestamp */
timestamp
?:
string
;
/** Information about the origin of the diagnostic. */
source
?:
DiagnosticSourceOptions
;
/** GitHub flavored Markdown formatted message. Should include inline links to any help pages. */
markdownMessage
?:
string
;
/** Plain text message. Used by components where the string processing needed to support Markdown is cumbersome. */
plaintextMessage
?:
string
;
/** List of help links intended to supplement the `plaintextMessage`. */
helpLinks
?:
string
[
]
;
/** SARIF severity */
severity
?:
"error"
|
"warning"
|
"note"
;
visibility
?:
{
/** True if the message should be displayed on the status page (defaults to false) */
statusPage
?:
boolean
;
/**
* True if the message should be counted in the diagnostics summary table printed by `codeql database analyze`
* (defaults to false)
*/
cliSummaryTable
?:
boolean
;
/** True if the message should be sent to telemetry (defaults to false) */
telemetry
?:
boolean
;
}
;
location
?:
{
/** Path to the affected file if appropriate, relative to the source root */
file
?:
string
;
startLine
?:
number
;
startColumn
?:
number
;
endLine
?:
number
;
endColumn
?:
number
;
}
;
/** Structured metadata about the diagnostic message */
attributes
?:
{
[
key
:
string
]
:
any
}
;
}
;
/** Represents a diagnostic message for the tool status page, etc. */
export
type
DiagnosticMessage
=
DiagnosticMessageOptions
&
{
/** ISO 8601 timestamp */
timestamp
:
string
;
/** Information about the origin of the diagnostic. */
source
:
DiagnosticSource
;
}
;
/** Represents a diagnostic message that has not yet been saved to the database. */
interface
TemporaryDiagnostic
{
/** The path to which the diagnostic has temporarily been written to. */
path
:
string
;
/** The language the diagnostic is for, if any. */
language
?:
Language
;
}
/** A list of diagnostics which have not yet been saved to the database. */
let
temporaryDiagnostics
:
TemporaryDiagnostic
[
]
=
[
]
;
/**
* Counter used to generate a unique suffix for each diagnostic filename, so that
* two diagnostics produced within the same millisecond do not overwrite each
* other on disk.
*/
let
diagnosticCounter
=
0
;
/**
* Constructs a new diagnostic message with the specified id and name, as well as optional additional data.
*
*
@param
id An identifier under which it makes sense to group this diagnostic message.
*
@param
name Display name for the ID.
*
@param
data Optional additional data to initialize the diagnostic with.
*
@returns
Returns the new diagnostic message.
*/
export
function
makeDiagnostic
(
id
:
string
,
name
:
string
,
data
:
DiagnosticMessageOptions
|
undefined
=
undefined
,
)
:
DiagnosticMessage
{
return
{
...
data
,
timestamp
:
data
?.
timestamp
??
new
Date
(
)
.
toISOString
(
)
,
source
:
{
...
data
?.
source
,
id
,
name
}
,
}
;
}
/**
* Gets a path relative to {
@link
basePath} where diagnostics for {
@link
language} should be stored at.
*/
export
function
getLanguageDiagnosticsPath
(
basePath
:
string
,
language
:
Language
,
)
{
return
path
.
resolve
(
basePath
,
language
,
...
CODEQL_ACTION_DIAGNOSTIC_SUBDIR_PARTS
,
)
;
}
/**
* Gets a path relative to {
@link
tmpDir} where diagnostics should temporarily be stored
* until they can be moved to the database.
*/
export
function
getTempDiagnosticsRoot
(
tmpDir
:
string
)
:
string
{
return
path
.
resolve
(
tmpDir
,
TEMP_DIAGNOSTIC_DIR_NAME
)
;
}
/**
* Gets a path relative to {
@link
tmpDir} where diagnostics for {
@link
language}
* can temporarily be stored at before the database is initialised. If {
@link
language}
* is `undefined`, then a non-language-specific path is returned.
*/
export
function
getTempLanguageDiagnosticsPath
(
tmpDir
:
string
,
language
?:
Language
,
)
{
const
root
=
getTempDiagnosticsRoot
(
tmpDir
)
;
if
(
language
!==
undefined
)
{
return
getLanguageDiagnosticsPath
(
root
,
language
)
;
}
return
path
.
resolve
(
root
,
...
CODEQL_ACTION_DIAGNOSTIC_SUBDIR_PARTS
)
;
}
/** Gets the path where diagnostics for {
@link
language} should be stored in the database. */
export
function
getDatabaseDiagnosticsPath
(
config
:
ActionConfig
<
"dbLocation"
>
,
language
:
Language
,
)
{
return
getLanguageDiagnosticsPath
(
config
.
dbLocation
,
language
)
;
}
/**
* Adds the given diagnostic to the database. If the database does not yet exist,
* the diagnostic will be written to it once it has been created.
*
*
@param
config The configuration that tells us where to store the diagnostic.
*
@param
language The language which the diagnostic is for.
*
@param
diagnostic The diagnostic message to add to the database.
*/
export
function
addDiagnostic
(
config
:
ActionConfig
<
"dbLocation"
|
"tempDir"
>
,
language
:
Language
,
diagnostic
:
DiagnosticMessage
,
logger
:
Logger
=
getActionsLogger
(
)
,
)
{
const
databasePath
=
language
?
getCodeQLDatabasePath
(
config
,
language
)
:
config
.
dbLocation
;
// Check that the database exists before writing to it. If the database does not yet exist,
// store the diagnostic in a temporary location and move it to the database later.
if
(
fs
.
existsSync
(
databasePath
)
)
{
writeDiagnostic
(
{
logger
}
,
config
,
language
,
diagnostic
)
;
}
else
{
logger
.
debug
(
`Writing a diagnostic for
${
language
}
, but the database at
${
databasePath
}
does not exist yet.`
,
)
;
// Write the diagnostic to a temporary location.
writeTemporaryDiagnosticFile
(
logger
,
config
.
tempDir
,
language
,
diagnostic
)
;
}
}
/** Adds a diagnostic that is not specific to any language. */
export
function
addNoLanguageDiagnostic
(
config
:
ActionConfig
<
"dbLocation"
|
"languages"
|
"tempDir"
>
|
undefined
,
diagnostic
:
DiagnosticMessage
,
logger
:
Logger
=
getActionsLogger
(
)
,
)
{
if
(
config
!==
undefined
)
{
addDiagnostic
(
config
,
// Arbitrarily choose the first language. We could also choose all languages, but that
// increases the risk of misinterpreting the data.
config
.
languages
[
0
]
,
diagnostic
,
)
;
}
else
{
logger
.
debug
(
`Writing a diagnostic not associated with any language, but we have no configuration to tell us where the database is yet.`
,
)
;
// Write the diagnostic to a temporary location.
writeTemporaryDiagnosticFile
(
logger
,
getTemporaryDirectory
(
)
,
undefined
,
diagnostic
,
)
;
}
}
/**
* Writes {
@link
diagnostic} for an optional {
@link
language} to a subdirectory of {
@link
tempDir}
* and tracks it as a diagnostic that was written to a temporary location.
*/
function
writeTemporaryDiagnosticFile
(
logger
:
Logger
,
tempDir
:
string
,
language
:
Language
|
undefined
,
diagnostic
:
DiagnosticMessage
,
)
{
const
tempDiagnosticsPath
=
getTempLanguageDiagnosticsPath
(
tempDir
,
language
)
;
try
{
// Write the diagnostic to a temporary location.
const
diagnosticPath
=
writeDiagnosticFile
(
tempDiagnosticsPath
,
diagnostic
)
;
// Track the temporary file.
temporaryDiagnostics
.
push
(
{
path
:
diagnosticPath
,
language
}
)
;
}
catch
(
err
)
{
logger
.
warning
(
`Unable to write temporary diagnostic to '
${
tempDiagnosticsPath
}
':
${
getErrorMessage
(
err
)
}
\n\n
${
JSON
.
stringify
(
diagnostic
)
}
`
,
)
;
}
}
/**
* Writes {
@link
diagnostic} to a file in {
@link
diagnosticsPath}.
*/
function
writeDiagnosticFile
(
diagnosticsPath
:
string
,
diagnostic
:
DiagnosticMessage
,
)
:
string
{
// Create the directory if it doesn't exist yet.
fs
.
mkdirSync
(
diagnosticsPath
,
{
recursive
:
true
}
)
;
// Include a monotonically increasing suffix to avoid filename collisions
// between diagnostics produced within the same millisecond.
const
uniqueSuffix
=
(
diagnosticCounter
++
)
.
toString
(
)
;
// We should only need to remove colons, but to be defensive, only allow a restricted set of
// characters.
const
sanitizedTimestamp
=
diagnostic
.
timestamp
.
replace
(
/
[
^
a
-
z
A
-
Z
0
-
9
.
-
]
/
g
,
""
,
)
;
const
jsonPath
=
path
.
resolve
(
diagnosticsPath
,
`codeql-action-
${
sanitizedTimestamp
}
-
${
uniqueSuffix
}
.json`
,
)
;
fs
.
writeFileSync
(
jsonPath
,
JSON
.
stringify
(
diagnostic
)
)
;
return
jsonPath
;
}
/**
* Writes the given diagnostic to the database.
*
*
@param
config The configuration that tells us where to store the diagnostic.
*
@param
language The language which the diagnostic is for.
*
@param
diagnostic The diagnostic message to add to the database.
*/
function
writeDiagnostic
(
action
:
ActionState
<
[
"Logger"
]
>
,
config
:
ActionConfig
<
"dbLocation"
>
,
language
:
Language
,
diagnostic
:
DiagnosticMessage
,
)
{
const
diagnosticsPath
=
getDatabaseDiagnosticsPath
(
config
,
language
)
;
try
{
writeDiagnosticFile
(
diagnosticsPath
,
diagnostic
)
;
}
catch
(
err
)
{
action
.
logger
.
warning
(
`Unable to write diagnostic message to database:
${
err
}
`
,
)
;
action
.
logger
.
debug
(
JSON
.
stringify
(
diagnostic
)
)
;
}
}
/** Report if there are temporary diagnostics and write them to the log. */
export
function
logTemporaryDiagnostics
(
action
:
ActionState
<
[
"Logger"
]
>
)
{
const
num
=
temporaryDiagnostics
.
length
;
if
(
num
>
0
&&
action
.
logger
.
isDebug
(
)
)
{
action
.
logger
.
startGroup
(
"Diagnostics in temporary location"
)
;
action
.
logger
.
debug
(
`
${
num
}
diagnostic(s) were not moved to the database and remain in the temporary directory.`
,
)
;
for
(
const
temporary
of
temporaryDiagnostics
)
{
try
{
action
.
logger
.
debug
(
fs
.
readFileSync
(
temporary
.
path
,
"utf-8"
)
)
;
}
catch
(
err
)
{
action
.
logger
.
debug
(
`Failed to read diagnostics from temporary location at '
${
temporary
.
path
}
':
${
getErrorMessage
(
err
)
}
`
,
)
;
}
}
action
.
logger
.
endGroup
(
)
;
}
}
/** Relocates all temporary diagnostics to the respective databases. */
export
function
flushDiagnostics
(
action
:
ActionState
<
[
"Logger"
]
>
,
config
:
ActionConfig
<
"dbLocation"
|
"languages"
>
,
)
{
const
diagnosticsCount
=
temporaryDiagnostics
.
length
;
action
.
logger
.
debug
(
`Moving
${
diagnosticsCount
}
diagnostic(s) to their respective databases.`
,
)
;
const
failedFlushDiagnostics
:
TemporaryDiagnostic
[
]
=
[
]
;
for
(
const
temporary
of
temporaryDiagnostics
)
{
// If `temporary.language` is `undefined`, then we didn't have a `config` at the time that
// `addNoLanguageDiagnostic` was called. In that case, we arbitrarily choose the first
// configured language here like we would have done in `addNoLanguageDiagnostic`.
const
directory
=
getDatabaseDiagnosticsPath
(
config
,
temporary
.
language
??
config
.
languages
[
0
]
,
)
;
const
filename
=
path
.
basename
(
temporary
.
path
)
;
const
destination
=
path
.
join
(
directory
,
filename
)
;
try
{
fs
.
mkdirSync
(
directory
,
{
recursive
:
true
}
)
;
try
{
fs
.
renameSync
(
temporary
.
path
,
destination
)
;
}
catch
(
renameErr
:
unknown
)
{
// Rethrow the error if it isn't an EXDEV one.
if
(
!
isErrnoException
(
renameErr
)
||
renameErr
.
code
!==
"EXDEV"
)
{
throw
renameErr
;
}
// The error was an EXDEV one: try to copy the file and delete the temporary one.
fs
.
copyFileSync
(
temporary
.
path
,
destination
)
;
fs
.
unlinkSync
(
temporary
.
path
)
;
}
}
catch
(
err
)
{
action
.
logger
.
warning
(
`Failed to relocate '
${
temporary
.
path
}
' to '
${
destination
}
':
${
getErrorMessage
(
err
)
}
`
,
)
;
failedFlushDiagnostics
.
push
(
temporary
)
;
}
}
// Reset the temporary diagnostics arrays.
temporaryDiagnostics
=
failedFlushDiagnostics
;
}
/**
* Creates a telemetry-only diagnostic message. This is a convenience function
* for creating diagnostics that should only be sent to telemetry and not
* displayed on the status page or CLI summary table.
*
*
@param
id An identifier under which it makes sense to group this diagnostic message
*
@param
name Display name
*
@param
attributes Structured metadata
*/
export
function
makeTelemetryDiagnostic
(
id
:
string
,
name
:
string
,
attributes
:
{
[
key
:
string
]
:
any
}
,
tags
?:
DiagnosticTag
[
]
,
)
:
DiagnosticMessage
{
return
makeDiagnostic
(
id
,
name
,
{
attributes
,
visibility
:
{
cliSummaryTable
:
false
,
statusPage
:
false
,
telemetry
:
true
,
}
,
source
:
{
tags
,
}
,
}
)
;
}
Back
|
FazBrowse Home
|
New Git URL