2020-04-28 17:46:47 +03:00
"use strict" ;
2021-07-27 19:59:59 +03:00
var _ _createBinding = ( this && this . _ _createBinding ) || ( Object . create ? ( function ( o , m , k , k2 ) {
if ( k2 === undefined ) k2 = k ;
2023-01-18 23:00:33 +03:00
var desc = Object . getOwnPropertyDescriptor ( m , k ) ;
if ( ! desc || ( "get" in desc ? ! m . _ _esModule : desc . writable || desc . configurable ) ) {
desc = { enumerable : true , get : function ( ) { return m [ k ] ; } } ;
}
Object . defineProperty ( o , k2 , desc ) ;
2021-07-27 19:59:59 +03:00
} ) : ( function ( o , m , k , k2 ) {
if ( k2 === undefined ) k2 = k ;
o [ k2 ] = m [ k ] ;
} ) ) ;
var _ _setModuleDefault = ( this && this . _ _setModuleDefault ) || ( Object . create ? ( function ( o , v ) {
Object . defineProperty ( o , "default" , { enumerable : true , value : v } ) ;
} ) : function ( o , v ) {
o [ "default" ] = v ;
} ) ;
2020-04-28 17:46:47 +03:00
var _ _importStar = ( this && this . _ _importStar ) || function ( mod ) {
if ( mod && mod . _ _esModule ) return mod ;
var result = { } ;
2021-07-27 19:59:59 +03:00
if ( mod != null ) for ( var k in mod ) if ( k !== "default" && Object . prototype . hasOwnProperty . call ( mod , k ) ) _ _createBinding ( result , mod , k ) ;
_ _setModuleDefault ( result , mod ) ;
2020-04-28 17:46:47 +03:00
return result ;
} ;
2022-08-31 13:51:08 +03:00
var _ _importDefault = ( this && this . _ _importDefault ) || function ( mod ) {
return ( mod && mod . _ _esModule ) ? mod : { "default" : mod } ;
} ;
2020-04-28 17:46:47 +03:00
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
2023-08-07 18:00:32 +03:00
exports . runPromise = void 0 ;
2022-08-27 01:06:09 +03:00
const fs = _ _importStar ( require ( "fs" ) ) ;
2022-08-31 13:51:08 +03:00
const path _1 = _ _importDefault ( require ( "path" ) ) ;
2022-08-19 13:12:10 +03:00
const perf _hooks _1 = require ( "perf_hooks" ) ;
2020-04-28 17:46:47 +03:00
const core = _ _importStar ( require ( "@actions/core" ) ) ;
2023-09-28 13:46:36 +03:00
const safe _which _1 = require ( "@chrisgavin/safe-which" ) ;
2020-09-15 16:00:25 +03:00
const actionsUtil = _ _importStar ( require ( "./actions-util" ) ) ;
2020-08-25 18:19:15 +03:00
const analyze _1 = require ( "./analyze" ) ;
2022-06-24 13:26:12 +03:00
const api _client _1 = require ( "./api-client" ) ;
2022-08-27 01:06:09 +03:00
const autobuild _1 = require ( "./autobuild" ) ;
2021-09-15 16:49:20 +03:00
const codeql _1 = require ( "./codeql" ) ;
2020-08-27 16:04:09 +03:00
const config _utils _1 = require ( "./config-utils" ) ;
2021-06-22 15:05:12 +03:00
const database _upload _1 = require ( "./database-upload" ) ;
2023-10-05 14:52:06 +03:00
const diagnostics _1 = require ( "./diagnostics" ) ;
2023-07-06 14:24:38 +03:00
const environment _1 = require ( "./environment" ) ;
2022-06-24 13:26:12 +03:00
const feature _flags _1 = require ( "./feature-flags" ) ;
2022-08-27 01:06:09 +03:00
const languages _1 = require ( "./languages" ) ;
2020-08-11 14:43:27 +03:00
const logging _1 = require ( "./logging" ) ;
2021-06-22 15:05:12 +03:00
const repository _1 = require ( "./repository" ) ;
2023-08-07 15:42:43 +03:00
const statusReport = _ _importStar ( require ( "./status-report" ) ) ;
const status _report _1 = require ( "./status-report" ) ;
2022-08-02 19:52:22 +03:00
const trap _caching _1 = require ( "./trap-caching" ) ;
2023-06-30 17:53:13 +03:00
const uploadLib = _ _importStar ( require ( "./upload-lib" ) ) ;
2020-04-28 17:46:47 +03:00
const util = _ _importStar ( require ( "./util" ) ) ;
2022-11-09 20:08:44 +03:00
const util _1 = require ( "./util" ) ;
2022-08-24 13:59:11 +03:00
async function sendStatusReport ( startedAt , config , stats , error , trapCacheUploadTime , dbCreationTimings , didUploadTrapCaches , logger ) {
2023-08-07 15:42:43 +03:00
const status = ( 0 , status _report _1 . getActionsStatus ) ( error , stats ? . analyze _failure _language ) ;
2023-08-07 18:00:32 +03:00
const statusReportBase = await ( 0 , status _report _1 . createStatusReportBase ) ( "finish" , status , startedAt , await util . checkDiskUsage ( ) , error ? . message , error ? . stack ) ;
2023-08-07 15:42:43 +03:00
const report = {
2020-07-20 18:33:37 +03:00
... statusReportBase ,
2020-08-25 18:19:15 +03:00
... ( stats || { } ) ,
2022-08-24 13:59:11 +03:00
... ( dbCreationTimings || { } ) ,
2020-07-20 18:33:37 +03:00
} ;
2022-08-16 15:30:49 +03:00
if ( config && didUploadTrapCaches ) {
const trapCacheUploadStatusReport = {
2023-08-07 15:42:43 +03:00
... report ,
2022-08-17 15:30:17 +03:00
trap _cache _upload _duration _ms : Math . round ( trapCacheUploadTime || 0 ) ,
trap _cache _upload _size _bytes : Math . round ( await ( 0 , trap _caching _1 . getTotalCacheSize ) ( config . trapCaches , logger ) ) ,
2022-08-16 15:30:49 +03:00
} ;
2023-08-07 15:42:43 +03:00
await statusReport . sendStatusReport ( trapCacheUploadStatusReport ) ;
2022-08-16 15:30:49 +03:00
}
else {
2023-08-07 15:42:43 +03:00
await statusReport . sendStatusReport ( report ) ;
2022-08-16 15:30:49 +03:00
}
2020-07-20 18:33:37 +03:00
}
2022-08-17 16:39:30 +03:00
// `expect-error` should only be set to a non-false value by the CodeQL Action PR checks.
2022-08-17 02:27:14 +03:00
function hasBadExpectErrorInput ( ) {
return ( actionsUtil . getOptionalInput ( "expect-error" ) !== "false" &&
2022-08-17 16:39:30 +03:00
! util . isInTestMode ( ) ) ;
2022-08-17 02:27:14 +03:00
}
2022-08-30 19:57:12 +03:00
/ * *
2022-08-31 13:51:08 +03:00
* Returns whether any TRAP files exist under the ` db-go ` folder ,
2022-08-30 19:57:12 +03:00
* indicating whether Go extraction has extracted at least one file .
* /
function doesGoExtractionOutputExist ( config ) {
2022-08-27 01:06:09 +03:00
const golangDbDirectory = util . getCodeQLDatabasePath ( config , languages _1 . Language . go ) ;
2022-08-31 13:51:08 +03:00
const trapDirectory = path _1 . default . join ( golangDbDirectory , "trap" , languages _1 . Language . go ) ;
2022-08-31 15:09:38 +03:00
return ( fs . existsSync ( trapDirectory ) &&
fs
. readdirSync ( trapDirectory )
. some ( ( fileName ) => [
".trap" ,
".trap.gz" ,
".trap.br" ,
".trap.tar.gz" ,
".trap.tar.br" ,
".trap.tar" ,
] . some ( ( ext ) => fileName . endsWith ( ext ) ) ) ) ;
2022-08-27 01:06:09 +03:00
}
2022-08-31 14:24:07 +03:00
/ * *
2022-11-15 00:54:35 +03:00
* We attempt to autobuild Go to preserve compatibility for users who have
* set up Go using a legacy scanning style CodeQL workflow , i . e . one without
* an autobuild step or manual build steps .
2022-08-31 14:24:07 +03:00
*
* - We detect whether an autobuild step is present by checking the
2023-03-21 00:09:04 +03:00
* ` CODEQL_ACTION_DID_AUTOBUILD_GOLANG ` environment variable , which is set
2022-08-31 14:24:07 +03:00
* when the autobuilder is invoked .
2022-12-01 13:29:03 +03:00
* - We detect whether the Go database has already been finalized in case it
* has been manually set in a prior Action step .
2022-08-31 14:24:07 +03:00
* - We approximate whether manual build steps are present by looking at
* whether any extraction output already exists for Go .
* /
2022-11-15 00:54:35 +03:00
async function runAutobuildIfLegacyGoWorkflow ( config , logger ) {
2022-09-01 14:56:03 +03:00
if ( ! config . languages . includes ( languages _1 . Language . go ) ) {
2022-08-31 14:24:07 +03:00
return ;
}
2023-07-06 14:24:38 +03:00
if ( process . env [ environment _1 . EnvVar . DID _AUTOBUILD _GOLANG ] === "true" ) {
2022-11-15 00:54:35 +03:00
logger . debug ( "Won't run Go autobuild since it has already been run." ) ;
2022-08-31 14:24:07 +03:00
return ;
}
2022-12-01 13:29:03 +03:00
if ( ( 0 , analyze _1 . dbIsFinalized ) ( config , languages _1 . Language . go , logger ) ) {
logger . debug ( "Won't run Go autobuild since there is already a finalized database for Go." ) ;
return ;
}
2022-08-31 14:24:07 +03:00
// This captures whether a user has added manual build steps for Go
if ( doesGoExtractionOutputExist ( config ) ) {
2022-11-15 00:54:35 +03:00
logger . debug ( "Won't run Go autobuild since at least one file of Go code has already been extracted." ) ;
// If the user has run the manual build step, and has set the `CODEQL_EXTRACTOR_GO_BUILD_TRACING`
// variable, we suggest they remove it from their workflow.
if ( "CODEQL_EXTRACTOR_GO_BUILD_TRACING" in process . env ) {
logger . warning ( ` The CODEQL_EXTRACTOR_GO_BUILD_TRACING environment variable has no effect on workflows with manual build steps, so we recommend that you remove it from your workflow. ` ) ;
}
2022-08-31 14:24:07 +03:00
return ;
}
2023-08-31 14:39:07 +03:00
logger . debug ( "Running Go autobuild because extraction output (TRAP files) for Go code has not been found." ) ;
2022-08-31 14:24:07 +03:00
await ( 0 , autobuild _1 . runAutobuild ) ( languages _1 . Language . go , config , logger ) ;
}
2020-04-28 17:46:47 +03:00
async function run ( ) {
2020-07-20 18:33:37 +03:00
const startedAt = new Date ( ) ;
2021-11-17 16:20:36 +03:00
let uploadResult = undefined ;
2021-06-28 12:07:51 +03:00
let runStats = undefined ;
2021-01-25 17:58:25 +03:00
let config = undefined ;
2022-08-15 16:44:43 +03:00
let trapCacheUploadTime = undefined ;
2022-08-24 13:59:11 +03:00
let dbCreationTimings = undefined ;
2022-08-15 16:44:43 +03:00
let didUploadTrapCaches = false ;
2023-01-20 15:03:17 +03:00
util . initializeEnvironment ( actionsUtil . getActionVersion ( ) ) ;
2022-08-16 15:30:49 +03:00
const logger = ( 0 , logging _1 . getActionsLogger ) ( ) ;
2020-04-28 17:46:47 +03:00
try {
2023-08-07 18:00:32 +03:00
if ( ! ( await statusReport . sendStatusReport ( await ( 0 , status _report _1 . createStatusReportBase ) ( "finish" , "starting" , startedAt , await util . checkDiskUsage ( logger ) ) ) ) ) {
2020-04-28 17:46:47 +03:00
return ;
}
2021-09-10 23:53:13 +03:00
config = await ( 0 , config _utils _1 . getConfig ) ( actionsUtil . getTemporaryDirectory ( ) , logger ) ;
2020-08-28 11:43:25 +03:00
if ( config === undefined ) {
throw new Error ( "Config file could not be found at expected location. Has the 'init' action been called?" ) ;
}
2022-08-17 02:27:14 +03:00
if ( hasBadExpectErrorInput ( ) ) {
2023-09-06 20:14:30 +03:00
throw new util . UserError ( "`expect-error` input parameter is for internal use only. It should only be set by codeql-action or a fork." ) ;
2022-08-17 02:27:14 +03:00
}
2022-09-02 21:59:18 +03:00
const apiDetails = ( 0 , api _client _1 . getApiDetails ) ( ) ;
2020-11-27 15:23:06 +03:00
const outputDir = actionsUtil . getRequiredInput ( "output" ) ;
2021-10-29 01:09:59 +03:00
const threads = util . getThreadsFlag ( actionsUtil . getOptionalInput ( "threads" ) || process . env [ "CODEQL_THREADS" ] , logger ) ;
2021-12-15 20:03:43 +03:00
const repositoryNwo = ( 0 , repository _1 . parseRepositoryNwo ) ( util . getRequiredEnvParam ( "GITHUB_REPOSITORY" ) ) ;
2022-11-14 19:37:48 +03:00
const gitHubVersion = await ( 0 , api _client _1 . getGitHubVersion ) ( ) ;
2022-11-21 22:14:38 +03:00
const features = new feature _flags _1 . Features ( gitHubVersion , repositoryNwo , actionsUtil . getTemporaryDirectory ( ) , logger ) ;
2023-09-18 14:43:52 +03:00
const memory = util . getMemoryFlag ( actionsUtil . getOptionalInput ( "ram" ) || process . env [ "CODEQL_RAM" ] , logger ) ;
2023-10-13 12:14:22 +03:00
// Check that `which go` still points at the same path it did when the `init` Action ran to ensure that no steps
// in-between performed any setup. We encourage users to perform all setup tasks before initializing CodeQL so that
// the setup tasks do not interfere with our analysis.
// Furthermore, if we installed a wrapper script in the `init` Action, we need to ensure that there isn't a step
// in the workflow after the `init` step which installs a different version of Go and takes precedence in the PATH,
// thus potentially circumventing our workaround that allows tracing to work.
const goInitPath = process . env [ environment _1 . EnvVar . GO _BINARY _LOCATION ] ;
2023-10-06 17:11:31 +03:00
if ( process . env [ environment _1 . EnvVar . DID _AUTOBUILD _GOLANG ] !== "true" &&
2023-10-13 12:14:22 +03:00
goInitPath !== undefined ) {
2023-09-28 13:46:36 +03:00
const goBinaryPath = await ( 0 , safe _which _1 . safeWhich ) ( "go" ) ;
2023-10-13 12:14:22 +03:00
if ( goInitPath !== goBinaryPath ) {
core . warning ( ` Expected \` which go \` to return ${ goInitPath } , but got ${ goBinaryPath } : please ensure that the correct version of Go is installed before the \` codeql-action/init \` Action is used. ` ) ;
2023-10-05 15:39:58 +03:00
( 0 , diagnostics _1 . addDiagnostic ) ( config , languages _1 . Language . go , ( 0 , diagnostics _1 . makeDiagnostic ) ( "go/workflow/go-installed-after-codeql-init" , "Go was installed after the `codeql-action/init` Action was run" , {
2023-10-05 16:27:34 +03:00
markdownMessage : "To avoid interfering with the CodeQL analysis, perform all installation steps before calling the `github/codeql-action/init` Action." ,
2023-10-05 14:52:06 +03:00
visibility : {
statusPage : true ,
telemetry : true ,
cliSummaryTable : true ,
} ,
severity : "warning" ,
} ) ) ;
2023-09-28 13:46:36 +03:00
}
}
2022-11-15 00:54:35 +03:00
await runAutobuildIfLegacyGoWorkflow ( config , logger ) ;
2023-05-12 11:00:31 +03:00
dbCreationTimings = await ( 0 , analyze _1 . runFinalize ) ( outputDir , threads , memory , config , logger , features ) ;
2021-06-28 12:07:51 +03:00
if ( actionsUtil . getRequiredInput ( "skip-queries" ) !== "true" ) {
2022-10-11 20:39:40 +03:00
runStats = await ( 0 , analyze _1 . runQueries ) ( outputDir , memory , util . getAddSnippetsFlag ( actionsUtil . getRequiredInput ( "add-snippets" ) ) , threads , actionsUtil . getOptionalInput ( "category" ) , config , logger , features ) ;
2021-06-28 12:07:51 +03:00
}
2021-05-24 19:26:13 +03:00
if ( actionsUtil . getOptionalInput ( "cleanup-level" ) !== "none" ) {
2021-09-10 23:53:13 +03:00
await ( 0 , analyze _1 . runCleanup ) ( config , actionsUtil . getOptionalInput ( "cleanup-level" ) || "brutal" , logger ) ;
2021-05-24 19:26:13 +03:00
}
const dbLocations = { } ;
for ( const language of config . languages ) {
dbLocations [ language ] = util . getCodeQLDatabasePath ( config , language ) ;
}
core . setOutput ( "db-locations" , dbLocations ) ;
2023-07-25 19:40:23 +03:00
core . setOutput ( "sarif-output" , path _1 . default . resolve ( outputDir ) ) ;
2023-03-23 20:23:25 +03:00
const uploadInput = actionsUtil . getOptionalInput ( "upload" ) ;
if ( runStats && actionsUtil . getUploadValue ( uploadInput ) === "always" ) {
2023-09-07 22:44:15 +03:00
uploadResult = await uploadLib . uploadFromActions ( outputDir , actionsUtil . getRequiredInput ( "checkout_path" ) , actionsUtil . getOptionalInput ( "category" ) , logger , { considerInvalidRequestUserError : false } ) ;
2022-01-26 16:17:00 +03:00
core . setOutput ( "sarif-id" , uploadResult . sarifID ) ;
2021-01-06 13:50:35 +03:00
}
else {
2020-11-27 15:23:06 +03:00
logger . info ( "Not uploading results" ) ;
}
2021-12-14 23:48:32 +03:00
// Possibly upload the database bundles for remote queries
2022-02-15 17:10:33 +03:00
await ( 0 , database _upload _1 . uploadDatabases ) ( repositoryNwo , config , apiDetails , logger ) ;
2022-08-02 19:52:22 +03:00
// Possibly upload the TRAP caches for later re-use
2022-08-19 13:12:10 +03:00
const trapCacheUploadStartTime = perf _hooks _1 . performance . now ( ) ;
2022-08-10 16:08:35 +03:00
const codeql = await ( 0 , codeql _1 . getCodeQL ) ( config . codeQLCmd ) ;
2022-08-15 16:44:43 +03:00
didUploadTrapCaches = await ( 0 , trap _caching _1 . uploadTrapCaches ) ( codeql , config , logger ) ;
2022-08-19 13:12:10 +03:00
trapCacheUploadTime = perf _hooks _1 . performance . now ( ) - trapCacheUploadStartTime ;
2022-04-28 21:10:42 +03:00
// We don't upload results in test mode, so don't wait for processing
if ( util . isInTestMode ( ) ) {
core . debug ( "In test mode. Waiting for processing is disabled." ) ;
}
else if ( uploadResult !== undefined &&
2021-11-17 16:20:36 +03:00
actionsUtil . getRequiredInput ( "wait-for-processing" ) === "true" ) {
2023-06-30 17:53:13 +03:00
await uploadLib . waitForProcessing ( ( 0 , repository _1 . parseRepositoryNwo ) ( util . getRequiredEnvParam ( "GITHUB_REPOSITORY" ) ) , uploadResult . sarifID , ( 0 , logging _1 . getActionsLogger ) ( ) ) ;
2021-11-17 16:20:36 +03:00
}
2022-08-17 02:27:14 +03:00
// If we did not throw an error yet here, but we expect one, throw it.
if ( actionsUtil . getOptionalInput ( "expect-error" ) === "true" ) {
core . setFailed ( ` expect-error input was set to true but no error was thrown. ` ) ;
}
2023-07-06 14:24:38 +03:00
core . exportVariable ( environment _1 . EnvVar . ANALYZE _DID _COMPLETE _SUCCESSFULLY , "true" ) ;
2020-04-28 17:46:47 +03:00
}
2023-04-06 19:04:21 +03:00
catch ( unwrappedError ) {
const error = ( 0 , util _1 . wrapError ) ( unwrappedError ) ;
2022-08-17 02:27:14 +03:00
if ( actionsUtil . getOptionalInput ( "expect-error" ) !== "true" ||
hasBadExpectErrorInput ( ) ) {
core . setFailed ( error . message ) ;
}
2020-10-22 11:22:27 +03:00
if ( error instanceof analyze _1 . CodeQLAnalysisError ) {
2021-06-28 12:07:51 +03:00
const stats = { ... error . queriesStatusReport } ;
2022-08-24 13:59:11 +03:00
await sendStatusReport ( startedAt , config , stats , error , trapCacheUploadTime , dbCreationTimings , didUploadTrapCaches , logger ) ;
2021-06-28 12:07:51 +03:00
}
else {
2022-08-24 13:59:11 +03:00
await sendStatusReport ( startedAt , config , undefined , error , trapCacheUploadTime , dbCreationTimings , didUploadTrapCaches , logger ) ;
2020-10-22 11:22:27 +03:00
}
2020-04-28 17:46:47 +03:00
return ;
}
2021-11-17 16:20:36 +03:00
if ( runStats && uploadResult ) {
2022-02-15 17:42:55 +03:00
await sendStatusReport ( startedAt , config , {
2021-11-17 16:20:36 +03:00
... runStats ,
... uploadResult . statusReport ,
2022-08-24 13:59:11 +03:00
} , undefined , trapCacheUploadTime , dbCreationTimings , didUploadTrapCaches , logger ) ;
2021-06-28 12:07:51 +03:00
}
else if ( runStats ) {
2022-08-24 13:59:11 +03:00
await sendStatusReport ( startedAt , config , { ... runStats } , undefined , trapCacheUploadTime , dbCreationTimings , didUploadTrapCaches , logger ) ;
2021-06-28 12:07:51 +03:00
}
else {
2022-08-24 13:59:11 +03:00
await sendStatusReport ( startedAt , config , undefined , undefined , trapCacheUploadTime , dbCreationTimings , didUploadTrapCaches , logger ) ;
2021-06-28 12:07:51 +03:00
}
2020-04-28 17:46:47 +03:00
}
2021-10-29 01:09:59 +03:00
exports . runPromise = run ( ) ;
2020-11-26 00:46:30 +03:00
async function runWrapper ( ) {
try {
2021-10-29 01:09:59 +03:00
await exports . runPromise ;
2020-11-26 00:46:30 +03:00
}
catch ( error ) {
2023-04-06 19:04:21 +03:00
core . setFailed ( ` analyze action failed: ${ ( 0 , util _1 . wrapError ) ( error ) . message } ` ) ;
2020-11-26 00:46:30 +03:00
}
2022-11-09 20:08:44 +03:00
await ( 0 , util _1 . checkForTimeout ) ( ) ;
2020-11-26 00:46:30 +03:00
}
void runWrapper ( ) ;
2020-08-24 17:09:02 +03:00
//# sourceMappingURL=analyze-action.js.map