FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
codeql-action/src/init.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
/
init.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
414 lines (390 loc) · 13 KB
Breadcrumbs
codeql-action
/
src
/
init.ts
Copy path
File metadata and controls
414 lines (390 loc) · 13 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
import
*
as
fs
from
"fs"
;
import
*
as
path
from
"path"
;
import
*
as
toolrunner
from
"@actions/exec/lib/toolrunner"
;
import
*
as
github
from
"@actions/github"
;
import
*
as
io
from
"@actions/io"
;
import
*
as
yaml
from
"js-yaml"
;
import
{
ActionState
}
from
"./action-common"
;
import
{
getOptionalInput
,
isAnalyzingPullRequest
,
isDefaultSetup
,
isSelfHostedRunner
,
}
from
"./actions-util"
;
import
{
GitHubApiDetails
}
from
"./api-client"
;
import
{
CodeQL
,
setupCodeQL
}
from
"./codeql"
;
import
*
as
configUtils
from
"./config-utils"
;
import
{
EnvVar
,
exportEnvVar
}
from
"./environment"
;
import
{
CodeQLDefaultVersionInfo
,
Feature
,
FeatureEnablement
,
}
from
"./feature-flags"
;
import
{
RepositoryProperties
,
RepositoryPropertyName
,
}
from
"./feature-flags/properties"
;
import
{
BuiltInLanguage
,
Language
}
from
"./languages"
;
import
{
Logger
,
withGroupAsync
}
from
"./logging"
;
import
{
ToolsSource
}
from
"./setup-codeql"
;
import
{
ToolsDownloadStatusReport
}
from
"./tools-download"
;
import
*
as
util
from
"./util"
;
export
async
function
initCodeQL
(
toolsInput
:
string
|
undefined
,
apiDetails
:
GitHubApiDetails
,
tempDir
:
string
,
variant
:
util
.
GitHubVariant
,
defaultCliVersion
:
CodeQLDefaultVersionInfo
,
rawLanguages
:
string
[
]
|
undefined
,
otherLanguagePacksReason
:
string
|
undefined
,
useOverlayAwareDefaultCliVersion
:
boolean
,
features
:
FeatureEnablement
,
logger
:
Logger
,
)
:
Promise
<
{
codeql
:
CodeQL
;
toolsDownloadStatusReport
?:
ToolsDownloadStatusReport
;
toolsSource
:
ToolsSource
;
toolsVersion
:
string
;
}
>
{
logger
.
startGroup
(
"Setup CodeQL tools"
)
;
const
{
codeql
,
toolsDownloadStatusReport
,
toolsSource
,
toolsVersion
}
=
await
setupCodeQL
(
toolsInput
,
apiDetails
,
tempDir
,
variant
,
defaultCliVersion
,
rawLanguages
,
otherLanguagePacksReason
,
useOverlayAwareDefaultCliVersion
,
features
,
logger
,
true
,
)
;
await
codeql
.
printVersion
(
)
;
logger
.
endGroup
(
)
;
return
{
codeql
,
toolsDownloadStatusReport
,
toolsSource
,
toolsVersion
,
}
;
}
export
async
function
initConfig
(
actionState
:
ActionState
<
[
"Logger"
,
"Env"
,
"FeatureFlags"
]
>
,
inputs
:
configUtils
.
InitConfigInputs
,
)
:
Promise
<
configUtils
.
Config
>
{
return
await
withGroupAsync
(
"Load language configuration"
,
async
(
)
=>
{
return
await
configUtils
.
initConfig
(
actionState
,
inputs
)
;
}
)
;
}
export
async
function
runDatabaseInitCluster
(
databaseInitEnvironment
:
Record
<
string
,
string
|
undefined
>
,
codeql
:
CodeQL
,
config
:
configUtils
.
Config
,
sourceRoot
:
string
,
processName
:
string
|
undefined
,
qlconfigFile
:
string
|
undefined
,
)
:
Promise
<
void
>
{
fs
.
mkdirSync
(
config
.
dbLocation
,
{
recursive
:
true
}
)
;
await
configUtils
.
wrapEnvironment
(
databaseInitEnvironment
,
async
(
)
=>
await
codeql
.
databaseInitCluster
(
config
,
sourceRoot
,
processName
,
qlconfigFile
,
)
,
)
;
}
/**
* Check whether all query packs are compatible with the overlay analysis
* support in the CodeQL CLI. If the check fails, this function will log a
* warning and returns false.
*
*
@param
codeql A CodeQL instance.
*
@param
logger A logger.
*
@returns
`true` if all query packs are compatible with overlay analysis,
* `false` otherwise.
*/
export
async
function
checkPacksForOverlayCompatibility
(
codeql
:
CodeQL
,
config
:
configUtils
.
Config
,
logger
:
Logger
,
)
:
Promise
<
boolean
>
{
const
codeQlOverlayVersion
=
(
await
codeql
.
getVersion
(
)
)
.
overlayVersion
;
if
(
codeQlOverlayVersion
===
undefined
)
{
logger
.
warning
(
"The CodeQL CLI does not support overlay analysis."
)
;
return
false
;
}
for
(
const
language
of
config
.
languages
)
{
const
suitePath
=
util
.
getGeneratedSuitePath
(
config
,
language
)
;
const
packDirs
=
await
codeql
.
resolveQueriesStartingPacks
(
[
suitePath
]
)
;
if
(
packDirs
.
some
(
(
packDir
)
=>
!
checkPackForOverlayCompatibility
(
packDir
,
codeQlOverlayVersion
,
logger
,
)
,
)
)
{
return
false
;
}
}
return
true
;
}
/** Interface for `qlpack.yml` file contents. */
interface
QlPack
{
buildMetadata
?:
string
;
}
/**
* Check a single pack for its overlay compatibility. If the check fails, this
* function will log a warning and returns false.
*
*
@param
packDir Path to the directory containing the pack.
*
@param
codeQlOverlayVersion The overlay version of the CodeQL CLI.
*
@param
logger A logger.
*
@returns
`true` if the pack is compatible with overlay analysis, `false`
* otherwise.
*/
function
checkPackForOverlayCompatibility
(
packDir
:
string
,
codeQlOverlayVersion
:
number
,
logger
:
Logger
,
)
:
boolean
{
try
{
let
qlpackPath
=
path
.
join
(
packDir
,
"qlpack.yml"
)
;
if
(
!
fs
.
existsSync
(
qlpackPath
)
)
{
qlpackPath
=
path
.
join
(
packDir
,
"codeql-pack.yml"
)
;
}
const
qlpackContents
=
yaml
.
load
(
fs
.
readFileSync
(
qlpackPath
,
"utf8"
)
,
)
as
QlPack
;
if
(
!
qlpackContents
.
buildMetadata
)
{
// This is a source-only pack, and overlay compatibility checks apply only
// to precompiled packs.
return
true
;
}
const
packInfoPath
=
path
.
join
(
packDir
,
".packinfo"
)
;
if
(
!
fs
.
existsSync
(
packInfoPath
)
)
{
logger
.
warning
(
`The query pack at
${
packDir
}
does not have a .packinfo file, `
+
"so it cannot support overlay analysis. Recompiling the query pack "
+
"with the latest CodeQL CLI should solve this problem."
,
)
;
return
false
;
}
const
packInfoFileContents
=
JSON
.
parse
(
fs
.
readFileSync
(
packInfoPath
,
"utf8"
)
,
)
;
const
packOverlayVersion
=
packInfoFileContents
.
overlayVersion
;
if
(
typeof
packOverlayVersion
!==
"number"
)
{
logger
.
warning
(
`The .packinfo file for the query pack at
${
packDir
}
`
+
"does not have the overlayVersion field, which indicates that "
+
"the pack is not compatible with overlay analysis."
,
)
;
return
false
;
}
if
(
packOverlayVersion
!==
codeQlOverlayVersion
)
{
logger
.
warning
(
`The query pack at
${
packDir
}
was compiled with `
+
`overlay version
${
packOverlayVersion
}
, but the CodeQL CLI `
+
`supports overlay version
${
codeQlOverlayVersion
}
. The `
+
"query pack needs to be recompiled to support overlay analysis."
,
)
;
return
false
;
}
}
catch
(
e
)
{
logger
.
warning
(
`Error while checking pack at
${
packDir
}
`
+
`for overlay compatibility:
${
util
.
getErrorMessage
(
e
)
}
`
,
)
;
return
false
;
}
return
true
;
}
/**
* If we are running python 3.12+ on windows, we need to switch to python 3.11.
* This check happens in a powershell script.
*/
export
async
function
checkInstallPython311
(
languages
:
Language
[
]
,
codeql
:
CodeQL
,
)
{
if
(
languages
.
includes
(
BuiltInLanguage
.
python
)
&&
process
.
platform
===
"win32"
&&
!
(
await
codeql
.
getVersion
(
)
)
.
features
?.
supportsPython312
)
{
const
script
=
path
.
resolve
(
__dirname
,
"../python-setup"
,
"check_python12.ps1"
,
)
;
await
new
toolrunner
.
ToolRunner
(
await
io
.
which
(
"powershell"
,
true
)
,
[
script
,
]
)
.
exec
(
)
;
}
}
export
function
cleanupDatabaseClusterDirectory
(
config
:
configUtils
.
Config
,
logger
:
Logger
,
options
:
{
disableExistingDirectoryWarning
?:
boolean
}
=
{
}
,
// We can't stub the fs module in tests, so we allow the caller to override the rmSync function
// for testing.
rmSync
=
fs
.
rmSync
,
)
:
void
{
if
(
fs
.
existsSync
(
config
.
dbLocation
)
&&
(
fs
.
statSync
(
config
.
dbLocation
)
.
isFile
(
)
||
fs
.
readdirSync
(
config
.
dbLocation
)
.
length
>
0
)
)
{
if
(
!
options
.
disableExistingDirectoryWarning
)
{
logger
.
warning
(
`The database cluster directory
${
config
.
dbLocation
}
must be empty. Attempting to clean it up.`
,
)
;
}
try
{
rmSync
(
config
.
dbLocation
,
{
force
:
true
,
maxRetries
:
3
,
recursive
:
true
,
}
)
;
logger
.
info
(
`Cleaned up database cluster directory
${
config
.
dbLocation
}
.`
,
)
;
}
catch
(
e
)
{
const
blurb
=
`The CodeQL Action requires an empty database cluster directory.
${
getOptionalInput
(
"db-location"
)
?
`This is currently configured to be
${
config
.
dbLocation
}
. `
:
`By default, this is located at
${
config
.
dbLocation
}
. `
+
"You can customize it using the 'db-location' input to the init Action. "
}
An attempt was made to clean up the directory, but this failed.`
;
// Hosted runners are automatically cleaned up, so this error should not occur for hosted runners.
if
(
isSelfHostedRunner
(
)
)
{
throw
new
util
.
ConfigurationError
(
`
${
blurb
}
This can happen if another process is using the directory or the directory is owned by a different user. `
+
`Please clean up the directory manually and rerun the job. Details:
${
util
.
getErrorMessage
(
e
,
)
}
`
,
)
;
}
else
{
throw
new
Error
(
`
${
blurb
}
This shouldn't typically happen on hosted runners. `
+
"If you are using an advanced setup, please check your workflow, otherwise we "
+
`recommend rerunning the job. Details:
${
util
.
getErrorMessage
(
e
)
}
`
,
)
;
}
}
}
}
export
async
function
getFileCoverageInformationEnabled
(
debugMode
:
boolean
,
codeql
:
CodeQL
,
features
:
FeatureEnablement
,
repositoryProperties
:
RepositoryProperties
,
)
:
Promise
<
{
enabled
:
boolean
;
enabledByRepositoryProperty
:
boolean
;
showDeprecationWarning
:
boolean
;
}
>
{
// Always enable file coverage information in debug mode
if
(
debugMode
)
{
return
{
enabled
:
true
,
enabledByRepositoryProperty
:
false
,
showDeprecationWarning
:
false
,
}
;
}
// We're most interested in speeding up PRs, and we want to keep
// submitting file coverage information for the default branch since
// it is used to populate the status page.
if
(
!
isAnalyzingPullRequest
(
)
)
{
return
{
enabled
:
true
,
enabledByRepositoryProperty
:
false
,
showDeprecationWarning
:
false
,
}
;
}
// If the user has explicitly opted out via an environment variable, don't
// show the deprecation warning.
if
(
(
process
.
env
[
EnvVar
.
FILE_COVERAGE_ON_PRS
]
||
""
)
.
toLocaleLowerCase
(
)
===
"true"
)
{
return
{
enabled
:
true
,
enabledByRepositoryProperty
:
false
,
showDeprecationWarning
:
false
,
}
;
}
// Allow repositories to opt in to file coverage information on PRs
// using a repository property. In this case, don't show the deprecation
// warning since the repository has explicitly opted in.
if
(
repositoryProperties
[
RepositoryPropertyName
.
FILE_COVERAGE_ON_PRS
]
===
true
)
{
return
{
enabled
:
true
,
enabledByRepositoryProperty
:
true
,
showDeprecationWarning
:
false
,
}
;
}
// If the feature is disabled, then maintain the previous behavior of
// unconditionally computing file coverage information, but warn that
// file coverage on PRs will be disabled in a future release.
if
(
!
(
await
features
.
getValue
(
Feature
.
SkipFileCoverageOnPrs
,
codeql
)
)
)
{
return
{
enabled
:
true
,
enabledByRepositoryProperty
:
false
,
showDeprecationWarning
:
true
,
}
;
}
// Otherwise, disable file coverage information on PRs to speed up analysis.
return
{
enabled
:
false
,
enabledByRepositoryProperty
:
false
,
showDeprecationWarning
:
false
,
}
;
}
/**
* Log a warning about the deprecation of file coverage information on PRs, including how to opt
* back in via an environment variable or repository property.
*/
export
function
logFileCoverageOnPrsDeprecationWarning
(
logger
:
Logger
)
:
void
{
if
(
process
.
env
[
EnvVar
.
DID_LOG_FILE_COVERAGE_ON_PRS_DEPRECATION
]
)
{
return
;
}
const
repositoryOwnerType
:
string
|
undefined
=
github
.
context
.
payload
.
repository
?.
owner
.
type
;
let
message
=
"Starting April 2026, the CodeQL Action will skip computing file coverage information on pull requests "
+
"to improve analysis performance. File coverage information will still be computed on non-PR analyses."
;
const
envVarOptOut
=
"set the `CODEQL_ACTION_FILE_COVERAGE_ON_PRS` environment variable to `true`."
;
const
repoPropertyOptOut
=
"create a custom repository property with the name "
+
'`github-codeql-file-coverage-on-prs` and the type "True/false", then set this property to '
+
"`true` in the repository's settings."
;
if
(
repositoryOwnerType
===
"Organization"
)
{
// Org-owned repo: can use the repository property
if
(
isDefaultSetup
(
)
)
{
message
+=
`\n\nTo opt out of this change,
${
repoPropertyOptOut
}
`
;
}
else
{
message
+=
`\n\nTo opt out of this change,
${
envVarOptOut
}
Alternatively,
${
repoPropertyOptOut
}
`
;
}
}
else
if
(
isDefaultSetup
(
)
)
{
// User-owned repo on default setup: no repo property available and
// no way to set env vars, so need to switch to advanced setup.
message
+=
`\n\nTo opt out of this change, switch to an advanced setup workflow and
${
envVarOptOut
}
`
;
}
else
{
// User-owned repo on advanced setup: can set the env var
message
+=
`\n\nTo opt out of this change,
${
envVarOptOut
}
`
;
}
logger
.
warning
(
message
)
;
exportEnvVar
(
EnvVar
.
DID_LOG_FILE_COVERAGE_ON_PRS_DEPRECATION
,
"true"
)
;
}
Back
|
FazBrowse Home
|
New Git URL