2020-06-26 17:22:19 +01:00
"use strict" ;
2021-07-27 17:59:59 +01:00
var __createBinding = ( this && this . __createBinding ) || ( Object . create ? ( function ( o , m , k , k2 ) {
if ( k2 === undefined ) k2 = k ;
Object . defineProperty ( o , k2 , { enumerable : true , get : function () { return m [ k ]; } });
}) : ( 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-06-26 17:22:19 +01:00
var __importStar = ( this && this . __importStar ) || function ( mod ) {
if ( mod && mod . __esModule ) return mod ;
var result = {};
2021-07-27 17:59:59 +01: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-06-26 17:22:19 +01:00
return result ;
};
2021-01-26 15:50:22 +00:00
var __importDefault = ( this && this . __importDefault ) || function ( mod ) {
return ( mod && mod . __esModule ) ? mod : { "default" : mod };
};
2020-06-26 17:22:19 +01:00
Object . defineProperty ( exports , "__esModule" , { value : true });
2021-07-27 17:59:59 +01:00
exports . getExtraOptions = exports . getCachedCodeQL = exports . setCodeQL = exports . getCodeQL = exports . convertToSemVer = exports . getCodeQLURLVersion = exports . setupCodeQL = exports . getCodeQLActionRepository = exports . CommandInvocationError = void 0 ;
2020-10-01 11:03:30 +01:00
const fs = __importStar ( require ( "fs" ));
const path = __importStar ( require ( "path" ));
2020-11-20 11:35:59 +01:00
const toolrunner = __importStar ( require ( "@actions/exec/lib/toolrunner" ));
2021-01-26 15:50:22 +00:00
const fast_deep_equal_1 = __importDefault ( require ( "fast-deep-equal" ));
2021-02-11 15:13:22 +00:00
const query_string_1 = __importDefault ( require ( "query-string" ));
2020-10-01 11:03:30 +01:00
const semver = __importStar ( require ( "semver" ));
const actions_util_1 = require ( "./actions-util" );
2020-09-29 14:43:37 +01:00
const api = __importStar ( require ( "./api-client" ));
const defaults = __importStar ( require ( "./defaults.json" )); // Referenced from codeql-action-sync-tool!
const error_matcher_1 = require ( "./error-matcher" );
2021-04-22 15:04:59 +01:00
const toolcache = __importStar ( require ( "./toolcache" ));
2020-09-29 14:43:37 +01:00
const toolrunner_error_catcher_1 = require ( "./toolrunner-error-catcher" );
2020-10-01 11:03:30 +01:00
const util = __importStar ( require ( "./util" ));
2021-06-24 17:30:42 +01:00
class CommandInvocationError extends Error {
constructor ( cmd , args , exitCode , error ) {
super ( `Failure invoking ${ cmd } with arguments ${ args } .\n
Exit code ${ exitCode } and error was:\n
${ error } ` );
}
}
exports . CommandInvocationError = CommandInvocationError ;
2020-06-26 15:33:59 +01:00
/**
2020-07-15 17:36:49 +01:00
* Stores the CodeQL object, and is populated by `setupCodeQL` or `getCodeQL`.
2020-06-26 15:33:59 +01:00
* Can be overridden in tests using `setCodeQL`.
*/
let cachedCodeQL = undefined ;
2020-08-10 12:34:11 +01:00
const CODEQL_BUNDLE_VERSION = defaults . bundleVersion ;
2020-07-10 11:54:26 +01:00
const CODEQL_DEFAULT_ACTION_REPOSITORY = "github/codeql-action" ;
2020-09-30 16:17:07 +02:00
function getCodeQLBundleName () {
let platform ;
if ( process . platform === "win32" ) {
platform = "win64" ;
}
else if ( process . platform === "linux" ) {
platform = "linux64" ;
}
else if ( process . platform === "darwin" ) {
platform = "osx64" ;
}
else {
return "codeql-bundle.tar.gz" ;
}
return `codeql-bundle- ${ platform } .tar.gz` ;
}
2021-05-20 15:20:32 -07:00
function getCodeQLActionRepository ( logger ) {
2021-06-24 17:51:49 +01:00
if ( ! util . isActions ()) {
2020-07-10 11:54:26 +01:00
return CODEQL_DEFAULT_ACTION_REPOSITORY ;
}
2021-03-16 11:59:10 +00:00
else {
return getActionsCodeQLActionRepository ( logger );
}
}
2021-05-20 15:20:32 -07:00
exports . getCodeQLActionRepository = getCodeQLActionRepository ;
2021-03-16 11:59:10 +00:00
function getActionsCodeQLActionRepository ( logger ) {
2020-11-10 12:35:14 +00:00
if ( process . env [ "GITHUB_ACTION_REPOSITORY" ] !== undefined ) {
return process . env [ "GITHUB_ACTION_REPOSITORY" ];
}
// The Actions Runner used with GitHub Enterprise Server 2.22 did not set the GITHUB_ACTION_REPOSITORY variable.
// This fallback logic can be removed after the end-of-support for 2.22 on 2021-09-23.
2020-11-12 14:18:58 +00:00
if ( actions_util_1 . isRunningLocalAction ()) {
// This handles the case where the Action does not come from an Action repository,
// e.g. our integration tests which use the Action code from the current checkout.
2020-11-10 12:35:14 +00:00
logger . info ( "The CodeQL Action is checked out locally. Using the default CodeQL Action repository." );
2020-07-10 11:54:26 +01:00
return CODEQL_DEFAULT_ACTION_REPOSITORY ;
}
2020-11-10 12:35:14 +00:00
logger . info ( "GITHUB_ACTION_REPOSITORY environment variable was not set. Falling back to legacy method of finding the GitHub Action." );
2020-11-12 14:18:58 +00:00
const relativeScriptPathParts = actions_util_1 . getRelativeScriptPath (). split ( path . sep );
2020-09-14 10:44:43 +01:00
return ` ${ relativeScriptPathParts [ 0 ] } / ${ relativeScriptPathParts [ 1 ] } ` ;
2020-07-10 11:54:26 +01:00
}
2021-05-20 15:20:32 -07:00
async function getCodeQLBundleDownloadURL ( apiDetails , variant , logger ) {
const codeQLActionRepository = getCodeQLActionRepository ( logger );
2020-07-10 11:54:26 +01:00
const potentialDownloadSources = [
// This GitHub instance, and this Action.
2020-11-23 14:18:05 +00:00
[ apiDetails . url , codeQLActionRepository ],
2020-07-10 11:54:26 +01:00
// This GitHub instance, and the canonical Action.
2020-11-23 14:18:05 +00:00
[ apiDetails . url , CODEQL_DEFAULT_ACTION_REPOSITORY ],
2020-07-10 11:54:26 +01:00
// GitHub.com, and the canonical Action.
2020-08-25 16:19:15 +01:00
[ util . GITHUB_DOTCOM_URL , CODEQL_DEFAULT_ACTION_REPOSITORY ],
2020-07-10 11:54:26 +01:00
];
// We now filter out any duplicates.
// Duplicates will happen either because the GitHub instance is GitHub.com, or because the Action is not a fork.
2021-01-26 15:50:22 +00:00
const uniqueDownloadSources = potentialDownloadSources . filter (( source , index , self ) => {
return ! self . slice ( 0 , index ). some (( other ) => fast_deep_equal_1 . default ( source , other ));
});
2020-09-30 16:17:07 +02:00
const codeQLBundleName = getCodeQLBundleName ();
2021-03-07 09:27:19 +00:00
if ( variant === util . GitHubVariant . GHAE ) {
try {
const release = await api
. getApiClient ( apiDetails )
. request ( "GET /enterprise/code-scanning/codeql-bundle/find/{tag}" , {
tag : CODEQL_BUNDLE_VERSION ,
});
const assetID = release . data . assets [ codeQLBundleName ];
if ( assetID !== undefined ) {
const download = await api
. getApiClient ( apiDetails )
. request ( "GET /enterprise/code-scanning/codeql-bundle/download/{asset_id}" , { asset_id : assetID });
const downloadURL = download . data . url ;
logger . info ( `Found CodeQL bundle at GitHub AE endpoint with URL ${ downloadURL } .` );
return downloadURL ;
}
else {
logger . info ( `Attempted to fetch bundle from GitHub AE endpoint but the bundle ${ codeQLBundleName } was not found in the assets ${ JSON . stringify ( release . data . assets ) } .` );
}
}
catch ( e ) {
logger . info ( `Attempted to fetch bundle from GitHub AE endpoint but got error ${ e } .` );
}
}
2020-09-14 10:44:43 +01:00
for ( const downloadSource of uniqueDownloadSources ) {
const [ apiURL , repository ] = downloadSource ;
2020-07-10 11:54:26 +01:00
// If we've reached the final case, short-circuit the API check since we know the bundle exists and is public.
2020-09-14 10:44:43 +01:00
if ( apiURL === util . GITHUB_DOTCOM_URL &&
repository === CODEQL_DEFAULT_ACTION_REPOSITORY ) {
2020-07-27 10:47:45 +01:00
break ;
2020-07-10 11:54:26 +01:00
}
2020-09-14 10:44:43 +01:00
const [ repositoryOwner , repositoryName ] = repository . split ( "/" );
2020-07-10 11:54:26 +01:00
try {
2020-11-30 16:33:38 +00:00
const release = await api . getApiClient ( apiDetails ). repos . getReleaseByTag ({
2020-07-10 11:54:26 +01:00
owner : repositoryOwner ,
repo : repositoryName ,
2020-09-14 10:44:43 +01:00
tag : CODEQL_BUNDLE_VERSION ,
2020-07-10 11:54:26 +01:00
});
2020-09-14 10:44:43 +01:00
for ( const asset of release . data . assets ) {
2020-09-30 16:17:07 +02:00
if ( asset . name === codeQLBundleName ) {
2020-08-25 16:19:15 +01:00
logger . info ( `Found CodeQL bundle in ${ downloadSource [ 1 ] } on ${ downloadSource [ 0 ] } with URL ${ asset . url } .` );
2020-07-10 11:54:26 +01:00
return asset . url ;
}
}
}
catch ( e ) {
2020-08-25 16:19:15 +01:00
logger . info ( `Looked for CodeQL bundle in ${ downloadSource [ 1 ] } on ${ downloadSource [ 0 ] } but got error ${ e } .` );
2020-07-10 11:54:26 +01:00
}
}
2020-09-30 16:17:07 +02:00
return `https://github.com/ ${ CODEQL_DEFAULT_ACTION_REPOSITORY } /releases/download/ ${ CODEQL_BUNDLE_VERSION } / ${ codeQLBundleName } ` ;
2020-07-10 11:54:26 +01:00
}
2021-05-20 15:20:32 -07:00
async function setupCodeQL ( codeqlURL , apiDetails , tempDir , toolCacheDir , variant , logger ) {
2020-06-26 17:22:19 +01:00
try {
2021-06-28 23:33:27 +01:00
// We use the special value of 'latest' to prioritize the version in the
// defaults over any pinned cached version.
const forceLatest = codeqlURL === "latest" ;
if ( forceLatest ) {
codeqlURL = undefined ;
}
2021-06-28 15:44:13 +01:00
let codeqlFolder ;
let codeqlURLVersion ;
2021-06-28 23:33:27 +01:00
if ( codeqlURL && ! codeqlURL . startsWith ( "http" )) {
codeqlFolder = await toolcache . extractTar ( codeqlURL , tempDir , logger );
2021-06-28 15:44:13 +01:00
codeqlURLVersion = "local" ;
2020-06-26 17:22:19 +01:00
}
else {
2021-06-28 15:44:13 +01:00
codeqlURLVersion = getCodeQLURLVersion ( codeqlURL || `/ ${ CODEQL_BUNDLE_VERSION } /` );
const codeqlURLSemVer = convertToSemVer ( codeqlURLVersion , logger );
// If we find the specified version, we always use that.
codeqlFolder = toolcache . find ( "CodeQL" , codeqlURLSemVer , toolCacheDir , logger );
// If we don't find the requested version, in some cases we may allow a
// different version to save download time if the version hasn't been
// specified explicitly (in which case we always honor it).
if ( ! codeqlFolder && ! codeqlURL && ! forceLatest ) {
const codeqlVersions = toolcache . findAllVersions ( "CodeQL" , toolCacheDir , logger );
if ( codeqlVersions . length === 1 ) {
const tmpCodeqlFolder = toolcache . find ( "CodeQL" , codeqlVersions [ 0 ], toolCacheDir , logger );
if ( fs . existsSync ( path . join ( tmpCodeqlFolder , "pinned-version" ))) {
logger . debug ( `CodeQL in cache overriding the default ${ CODEQL_BUNDLE_VERSION } ` );
codeqlFolder = tmpCodeqlFolder ;
}
}
}
if ( codeqlFolder ) {
logger . debug ( `CodeQL found in cache ${ codeqlFolder } ` );
2020-07-10 11:54:26 +01:00
}
else {
2021-06-28 15:44:13 +01:00
if ( ! codeqlURL ) {
codeqlURL = await getCodeQLBundleDownloadURL ( apiDetails , variant , logger );
}
const parsedCodeQLURL = new URL ( codeqlURL );
const parsedQueryString = query_string_1 . default . parse ( parsedCodeQLURL . search );
const headers = { accept : "application/octet-stream" };
// We only want to provide an authorization header if we are downloading
// from the same GitHub instance the Action is running on.
// This avoids leaking Enterprise tokens to dotcom.
// We also don't want to send an authorization header if there's already a token provided in the URL.
if ( codeqlURL . startsWith ( ` ${ apiDetails . url } /` ) &&
parsedQueryString [ "token" ] === undefined ) {
logger . debug ( "Downloading CodeQL bundle with token." );
headers . authorization = `token ${ apiDetails . auth } ` ;
}
else {
logger . debug ( "Downloading CodeQL bundle without token." );
}
logger . info ( `Downloading CodeQL tools from ${ codeqlURL } . This may take a while.` );
const codeqlPath = await toolcache . downloadTool ( codeqlURL , tempDir , headers );
logger . debug ( `CodeQL bundle download to ${ codeqlPath } complete.` );
const codeqlExtracted = await toolcache . extractTar ( codeqlPath , tempDir , logger );
codeqlFolder = await toolcache . cacheDir ( codeqlExtracted , "CodeQL" , codeqlURLSemVer , toolCacheDir , logger );
2020-07-10 11:54:26 +01:00
}
2020-06-26 17:22:19 +01:00
}
2020-09-14 10:44:43 +01:00
let codeqlCmd = path . join ( codeqlFolder , "codeql" , "codeql" );
if ( process . platform === "win32" ) {
2020-06-26 17:22:19 +01:00
codeqlCmd += ".exe" ;
}
2020-09-14 10:44:43 +01:00
else if ( process . platform !== "linux" && process . platform !== "darwin" ) {
throw new Error ( `Unsupported platform: ${ process . platform } ` );
2020-06-26 17:22:19 +01:00
}
2020-06-26 15:33:59 +01:00
cachedCodeQL = getCodeQLForCmd ( codeqlCmd );
2020-11-11 18:22:12 +00:00
return { codeql : cachedCodeQL , toolsVersion : codeqlURLVersion };
2020-06-26 17:22:19 +01:00
}
catch ( e ) {
2020-08-25 16:19:15 +01:00
logger . error ( e );
2020-06-26 17:22:19 +01:00
throw new Error ( "Unable to download and extract CodeQL CLI" );
}
}
exports . setupCodeQL = setupCodeQL ;
2020-11-11 18:22:12 +00:00
function getCodeQLURLVersion ( url ) {
2020-06-26 17:22:19 +01:00
const match = url . match ( /\/codeql-bundle-(.*)\// );
if ( match === null || match . length < 2 ) {
throw new Error ( `Malformed tools url: ${ url } . Version could not be inferred` );
}
2020-11-11 18:22:12 +00:00
return match [ 1 ];
}
exports . getCodeQLURLVersion = getCodeQLURLVersion ;
function convertToSemVer ( version , logger ) {
2020-06-26 17:22:19 +01:00
if ( ! semver . valid ( version )) {
2020-08-25 16:19:15 +01:00
logger . debug ( `Bundle version ${ version } is not in SemVer format. Will treat it as pre-release 0.0.0- ${ version } .` );
2020-09-14 10:44:43 +01:00
version = `0.0.0- ${ version } ` ;
2020-06-26 17:22:19 +01:00
}
const s = semver . clean ( version );
if ( ! s ) {
2020-11-11 18:22:12 +00:00
throw new Error ( `Bundle version ${ version } is not in SemVer format.` );
2020-06-26 17:22:19 +01:00
}
return s ;
}
2020-11-11 18:22:12 +00:00
exports . convertToSemVer = convertToSemVer ;
2020-08-19 15:54:23 +01:00
/**
* Use the CodeQL executable located at the given path.
*/
function getCodeQL ( cmd ) {
2020-06-26 15:33:59 +01:00
if ( cachedCodeQL === undefined ) {
2020-08-19 15:54:23 +01:00
cachedCodeQL = getCodeQLForCmd ( cmd );
2020-06-26 15:33:59 +01:00
}
return cachedCodeQL ;
2020-06-26 17:22:19 +01:00
}
exports . getCodeQL = getCodeQL ;
2020-08-19 15:54:23 +01:00
function resolveFunction ( partialCodeql , methodName , defaultImplementation ) {
2020-09-14 10:44:43 +01:00
if ( typeof partialCodeql [ methodName ] !== "function" ) {
2020-08-19 15:54:23 +01:00
if ( defaultImplementation !== undefined ) {
return defaultImplementation ;
}
2020-06-26 15:33:59 +01:00
const dummyMethod = () => {
2020-09-14 10:44:43 +01:00
throw new Error ( `CodeQL ${ methodName } method not correctly defined` );
2020-06-26 15:33:59 +01:00
};
return dummyMethod ;
}
return partialCodeql [ methodName ];
}
2020-07-15 17:36:49 +01:00
/**
* Set the functionality for CodeQL methods. Only for use in tests.
*
* Accepts a partial object and any undefined methods will be implemented
* to immediately throw an exception indicating which method is missing.
*/
2020-06-26 15:33:59 +01:00
function setCodeQL ( partialCodeql ) {
cachedCodeQL = {
2020-09-14 10:44:43 +01:00
getPath : resolveFunction ( partialCodeql , "getPath" , () => "/tmp/dummy-path" ),
printVersion : resolveFunction ( partialCodeql , "printVersion" ),
getTracerEnv : resolveFunction ( partialCodeql , "getTracerEnv" ),
databaseInit : resolveFunction ( partialCodeql , "databaseInit" ),
runAutobuild : resolveFunction ( partialCodeql , "runAutobuild" ),
extractScannedLanguage : resolveFunction ( partialCodeql , "extractScannedLanguage" ),
finalizeDatabase : resolveFunction ( partialCodeql , "finalizeDatabase" ),
2021-05-23 16:27:46 +02:00
resolveLanguages : resolveFunction ( partialCodeql , "resolveLanguages" ),
2020-09-14 10:44:43 +01:00
resolveQueries : resolveFunction ( partialCodeql , "resolveQueries" ),
2021-06-03 09:32:44 -07:00
packDownload : resolveFunction ( partialCodeql , "packDownload" ),
2021-05-24 17:26:13 +01:00
databaseCleanup : resolveFunction ( partialCodeql , "databaseCleanup" ),
2021-06-22 13:05:12 +01:00
databaseBundle : resolveFunction ( partialCodeql , "databaseBundle" ),
2021-06-04 14:28:55 +01:00
databaseRunQueries : resolveFunction ( partialCodeql , "databaseRunQueries" ),
databaseInterpretResults : resolveFunction ( partialCodeql , "databaseInterpretResults" ),
2020-06-26 15:33:59 +01:00
};
2020-08-19 15:54:23 +01:00
return cachedCodeQL ;
2020-06-26 15:33:59 +01:00
}
exports . setCodeQL = setCodeQL ;
2020-08-19 15:54:23 +01:00
/**
* Get the cached CodeQL object. Should only be used from tests.
*
* TODO: Work out a good way for tests to get this from the test context
* instead of having to have this method.
*/
function getCachedCodeQL () {
if ( cachedCodeQL === undefined ) {
// Should never happen as setCodeQL is called by testing-utils.setupTests
2020-09-14 10:44:43 +01:00
throw new Error ( "cachedCodeQL undefined" );
2020-08-19 15:54:23 +01:00
}
return cachedCodeQL ;
}
exports . getCachedCodeQL = getCachedCodeQL ;
2020-06-26 17:22:19 +01:00
function getCodeQLForCmd ( cmd ) {
return {
2020-09-14 10:44:43 +01:00
getPath () {
2020-08-19 15:54:23 +01:00
return cmd ;
2020-06-26 17:22:19 +01:00
},
2020-09-14 10:44:43 +01:00
async printVersion () {
2021-06-09 13:17:25 -07:00
await runTool ( cmd , [ "version" , "--format=json" ]);
2020-06-26 17:22:19 +01:00
},
2020-09-14 10:44:43 +01:00
async getTracerEnv ( databasePath ) {
2020-08-27 16:34:09 +01:00
// Write tracer-env.js to a temp location.
2021-05-13 22:01:26 +02:00
// BEWARE: The name and location of this file is recognized by `codeql database
// trace-command` in order to enable special support for concatenable tracer
// configurations. Consequently the name must not be changed.
// (This warning can be removed once a different way to recognize the
// action/runner has been implemented in `codeql database trace-command`
// _and_ is present in the latest supported CLI release.)
2020-09-14 10:44:43 +01:00
const tracerEnvJs = path . resolve ( databasePath , "working" , "tracer-env.js" );
2020-08-26 16:18:53 +01:00
fs . mkdirSync ( path . dirname ( tracerEnvJs ), { recursive : true });
fs . writeFileSync ( tracerEnvJs , `
const fs = require('fs');
const env = {};
for (let entry of Object.entries(process.env)) {
const key = entry[0];
const value = entry[1];
if (typeof value !== 'undefined' && key !== '_' && !key.startsWith('JAVA_MAIN_CLASS_')) {
env[key] = value;
}
}
process.stdout.write(process.argv[2]);
fs.writeFileSync(process.argv[2], JSON.stringify(env), 'utf-8');` );
2021-05-13 22:01:26 +02:00
// BEWARE: The name and location of this file is recognized by `codeql database
// trace-command` in order to enable special support for concatenable tracer
// configurations. Consequently the name must not be changed.
// (This warning can be removed once a different way to recognize the
// action/runner has been implemented in `codeql database trace-command`
// _and_ is present in the latest supported CLI release.)
2020-09-14 10:44:43 +01:00
const envFile = path . resolve ( databasePath , "working" , "env.tmp" );
2021-06-09 13:17:25 -07:00
await runTool ( cmd , [
2020-09-14 10:44:43 +01:00
"database" ,
"trace-command" ,
2020-07-02 14:45:14 +01:00
databasePath ,
2020-09-14 10:44:43 +01:00
... getExtraOptionsFromEnv ([ "database" , "trace-command" ]),
2020-06-26 17:22:19 +01:00
process . execPath ,
2020-08-26 16:18:53 +01:00
tracerEnvJs ,
2020-09-14 10:44:43 +01:00
envFile ,
2021-06-09 13:17:25 -07:00
]);
2020-09-14 10:44:43 +01:00
return JSON . parse ( fs . readFileSync ( envFile , "utf-8" ));
2020-06-26 17:22:19 +01:00
},
2020-09-14 10:44:43 +01:00
async databaseInit ( databasePath , language , sourceRoot ) {
2021-06-09 13:17:25 -07:00
await runTool ( cmd , [
2020-09-14 10:44:43 +01:00
"database" ,
"init" ,
2020-07-02 14:45:14 +01:00
databasePath ,
2020-09-14 10:44:43 +01:00
`--language= ${ language } ` ,
`--source-root= ${ sourceRoot } ` ,
... getExtraOptionsFromEnv ([ "database" , "init" ]),
2021-06-09 13:17:25 -07:00
]);
2020-06-26 17:22:19 +01:00
},
2020-09-14 10:44:43 +01:00
async runAutobuild ( language ) {
const cmdName = process . platform === "win32" ? "autobuild.cmd" : "autobuild.sh" ;
const autobuildCmd = path . join ( path . dirname ( cmd ), language , "tools" , cmdName );
2020-07-02 14:45:14 +01:00
// Update JAVA_TOOL_OPTIONS to contain '-Dhttp.keepAlive=false'
// This is because of an issue with Azure pipelines timing out connections after 4 minutes
// and Maven not properly handling closed connections
// Otherwise long build processes will timeout when pulling down Java packages
// https://developercommunity.visualstudio.com/content/problem/292284/maven-hosted-agent-connection-timeout.html
2020-09-14 10:44:43 +01:00
const javaToolOptions = process . env [ "JAVA_TOOL_OPTIONS" ] || "" ;
process . env [ "JAVA_TOOL_OPTIONS" ] = [
... javaToolOptions . split ( /\s+/ ),
"-Dhttp.keepAlive=false" ,
"-Dmaven.wagon.http.pool=false" ,
]. join ( " " );
2021-06-09 13:17:25 -07:00
await runTool ( autobuildCmd );
2020-07-02 14:45:14 +01:00
},
2020-09-14 10:44:43 +01:00
async extractScannedLanguage ( databasePath , language ) {
2020-06-26 17:22:19 +01:00
// Get extractor location
2020-09-14 10:44:43 +01:00
let extractorPath = "" ;
2020-11-20 11:35:59 +01:00
await new toolrunner . ToolRunner ( cmd , [
2020-09-14 10:44:43 +01:00
"resolve" ,
"extractor" ,
"--format=json" ,
`--language= ${ language } ` ,
... getExtraOptionsFromEnv ([ "resolve" , "extractor" ]),
2020-09-03 17:01:51 +01:00
], {
2020-06-26 17:22:19 +01:00
silent : true ,
listeners : {
2020-09-14 10:44:43 +01:00
stdout : ( data ) => {
extractorPath += data . toString ();
},
stderr : ( data ) => {
process . stderr . write ( data );
},
},
2020-08-28 16:37:14 +01:00
}). exec ();
2020-06-26 17:22:19 +01:00
// Set trace command
2020-09-14 10:44:43 +01:00
const ext = process . platform === "win32" ? ".cmd" : ".sh" ;
const traceCommand = path . resolve ( JSON . parse ( extractorPath ), "tools" , `autobuild ${ ext } ` );
2020-06-26 17:22:19 +01:00
// Run trace command
2020-09-08 00:01:04 +01:00
await toolrunner_error_catcher_1 . toolrunnerErrorCatcher ( cmd , [
2020-09-14 10:44:43 +01:00
"database" ,
"trace-command" ,
... getExtraOptionsFromEnv ([ "database" , "trace-command" ]),
2020-07-02 14:45:14 +01:00
databasePath ,
2020-09-14 10:44:43 +01:00
"--" ,
traceCommand ,
2020-09-07 22:50:37 +01:00
], error_matcher_1 . errorMatchers );
2020-06-26 17:22:19 +01:00
},
2020-11-03 08:25:40 -08:00
async finalizeDatabase ( databasePath , threadsFlag ) {
2020-09-08 00:01:04 +01:00
await toolrunner_error_catcher_1 . toolrunnerErrorCatcher ( cmd , [
2020-09-14 10:44:43 +01:00
"database" ,
"finalize" ,
2021-06-09 15:00:42 +01:00
"--finalize-dataset" ,
2020-11-03 08:25:40 -08:00
threadsFlag ,
2020-09-14 10:44:43 +01:00
... getExtraOptionsFromEnv ([ "database" , "finalize" ]),
databasePath ,
2020-09-07 22:50:37 +01:00
], error_matcher_1 . errorMatchers );
2020-06-26 17:22:19 +01:00
},
2021-05-23 16:27:46 +02:00
async resolveLanguages () {
const codeqlArgs = [ "resolve" , "languages" , "--format=json" ];
2021-06-09 13:17:25 -07:00
const output = await runTool ( cmd , codeqlArgs );
2021-05-24 11:00:02 +02:00
try {
return JSON . parse ( output );
}
catch ( e ) {
throw new Error ( `Unexpected output from codeql resolve languages: ${ e } ` );
}
2021-05-23 16:27:46 +02:00
},
2020-09-14 10:44:43 +01:00
async resolveQueries ( queries , extraSearchPath ) {
2020-06-26 15:33:59 +01:00
const codeqlArgs = [
2020-09-14 10:44:43 +01:00
"resolve" ,
"queries" ,
2020-06-26 17:22:19 +01:00
... queries ,
2020-09-14 10:44:43 +01:00
"--format=bylanguage" ,
... getExtraOptionsFromEnv ([ "resolve" , "queries" ]),
2020-06-26 15:33:59 +01:00
];
if ( extraSearchPath !== undefined ) {
2021-05-20 19:03:50 +00:00
codeqlArgs . push ( "--additional-packs" , extraSearchPath );
2020-06-26 15:33:59 +01:00
}
2021-06-09 13:17:25 -07:00
const output = await runTool ( cmd , codeqlArgs );
2021-05-24 11:00:02 +02:00
try {
return JSON . parse ( output );
}
catch ( e ) {
throw new Error ( `Unexpected output from codeql resolve queries: ${ e } ` );
}
2020-06-26 17:22:19 +01:00
},
2021-06-04 14:28:55 +01:00
async databaseRunQueries ( databasePath , extraSearchPath , querySuitePath , memoryFlag , threadsFlag ) {
2021-06-09 13:17:25 -07:00
const codeqlArgs = [
2020-09-14 10:44:43 +01:00
"database" ,
2021-06-04 14:28:55 +01:00
"run-queries" ,
2020-09-01 14:13:10 +01:00
memoryFlag ,
threadsFlag ,
2020-07-02 14:45:14 +01:00
databasePath ,
2020-09-24 11:42:21 +02:00
"--min-disk-free=1024" ,
2021-05-05 18:09:10 +01:00
"-v" ,
2021-06-04 14:28:55 +01:00
... getExtraOptionsFromEnv ([ "database" , "run-queries" ]),
2021-04-01 12:38:13 +01:00
];
if ( extraSearchPath !== undefined ) {
2021-06-09 13:17:25 -07:00
codeqlArgs . push ( "--additional-packs" , extraSearchPath );
2021-04-01 12:38:13 +01:00
}
2021-06-09 13:17:25 -07:00
codeqlArgs . push ( querySuitePath );
await runTool ( cmd , codeqlArgs );
2021-06-04 14:28:55 +01:00
},
async databaseInterpretResults ( databasePath , querySuitePaths , sarifFile , addSnippetsFlag , threadsFlag , automationDetailsId ) {
2021-06-09 13:17:25 -07:00
const codeqlArgs = [
2021-06-04 14:28:55 +01:00
"database" ,
"interpret-results" ,
threadsFlag ,
"--format=sarif-latest" ,
2021-08-02 17:06:09 +01:00
"--print-diagnostics-summary" ,
2021-06-04 14:28:55 +01:00
"--print-metrics-summary" ,
"--sarif-group-rules-by-pack" ,
"-v" ,
`--output= ${ sarifFile } ` ,
addSnippetsFlag ,
... getExtraOptionsFromEnv ([ "database" , "interpret-results" ]),
];
2021-05-03 19:41:53 +02:00
if ( automationDetailsId !== undefined ) {
2021-06-09 13:17:25 -07:00
codeqlArgs . push ( "--sarif-category" , automationDetailsId );
2021-05-03 19:41:53 +02:00
}
2021-06-09 13:17:25 -07:00
codeqlArgs . push ( databasePath , ... querySuitePaths );
2021-05-19 16:01:06 -07:00
// capture stdout, which contains analysis summaries
2021-06-09 13:17:25 -07:00
return await runTool ( cmd , codeqlArgs );
2020-09-14 10:44:43 +01:00
},
2021-06-04 10:18:24 -07:00
/**
* Download specified packs into the package cache. If the specified
* package and version already exists (e.g., from a previous analysis run),
* then it is not downloaded again (unless the extra option `--force` is
* specified).
*
* If no version is specified, then the latest version is
* downloaded. The check to determine what the latest version is is done
* each time this package is requested.
*/
2021-06-03 09:32:44 -07:00
async packDownload ( packs ) {
2021-06-09 13:17:25 -07:00
const codeqlArgs = [
2021-06-03 09:32:44 -07:00
"pack" ,
"download" ,
2021-06-04 13:44:24 -07:00
"--format=json" ,
2021-06-03 09:32:44 -07:00
... getExtraOptionsFromEnv ([ "pack" , "download" ]),
... packs . map ( packWithVersionToString ),
];
2021-06-09 13:17:25 -07:00
const output = await runTool ( cmd , codeqlArgs );
2021-06-03 09:32:44 -07:00
try {
2021-06-07 16:05:32 -07:00
const parsedOutput = JSON . parse ( output );
if ( Array . isArray ( parsedOutput . packs ) &&
2021-06-08 10:24:08 -07:00
// TODO PackDownloadOutput will not include the version if it is not specified
// in the input. The version is always the latest version available.
// It should be added to the output, but this requires a CLI change
parsedOutput . packs . every (( p ) => p . name /* && p.version */ )) {
2021-06-07 16:05:32 -07:00
return parsedOutput ;
}
else {
throw new Error ( "Unexpected output from pack download" );
}
2021-06-03 09:32:44 -07:00
}
catch ( e ) {
2021-06-07 16:05:32 -07:00
throw new Error ( `Attempted to download specified packs but got an error:\n ${ output } \n ${ e } ` );
2021-06-03 09:32:44 -07:00
}
2020-09-14 10:44:43 +01:00
},
2021-05-24 17:26:13 +01:00
async databaseCleanup ( databasePath , cleanupLevel ) {
2021-06-09 13:17:25 -07:00
const codeqlArgs = [
2021-05-24 17:26:13 +01:00
"database" ,
"cleanup" ,
databasePath ,
`--mode= ${ cleanupLevel } ` ,
];
2021-06-09 13:17:25 -07:00
await runTool ( cmd , codeqlArgs );
2021-05-24 17:26:13 +01:00
},
2021-06-22 13:05:12 +01:00
async databaseBundle ( databasePath , outputFilePath ) {
const args = [
"database" ,
"bundle" ,
databasePath ,
`--output= ${ outputFilePath } ` ,
];
await new toolrunner . ToolRunner ( cmd , args ). exec ();
},
2020-06-26 17:22:19 +01:00
};
}
2021-06-03 09:32:44 -07:00
function packWithVersionToString ( pack ) {
return pack . version ? ` ${ pack . packName } @ ${ pack . version } ` : pack . packName ;
}
2020-08-10 09:25:14 +02:00
/**
* Gets the options for `path` of `options` as an array of extra option strings.
*/
2020-11-19 23:03:45 +01:00
function getExtraOptionsFromEnv ( paths ) {
2020-09-14 10:44:43 +01:00
const options = util . getExtraOptionsEnvParam ();
2020-11-19 23:03:45 +01:00
return getExtraOptions ( options , paths , []);
}
/**
* Gets `options` as an array of extra option strings.
*
* - throws an exception mentioning `pathInfo` if this conversion is impossible.
*/
function asExtraOptions ( options , pathInfo ) {
if ( options === undefined ) {
return [];
}
if ( ! Array . isArray ( options )) {
const msg = `The extra options for ' ${ pathInfo . join ( "." ) } ' (' ${ JSON . stringify ( options ) } ') are not in an array.` ;
throw new Error ( msg );
}
return options . map (( o ) => {
const t = typeof o ;
if ( t !== "string" && t !== "number" && t !== "boolean" ) {
const msg = `The extra option for ' ${ pathInfo . join ( "." ) } ' (' ${ JSON . stringify ( o ) } ') is not a primitive value.` ;
throw new Error ( msg );
}
return ` ${ o } ` ;
});
2020-08-10 09:25:14 +02:00
}
/**
* Gets the options for `path` of `options` as an array of extra option strings.
*
* - the special terminal step name '*' in `options` matches all path steps
* - throws an exception if this conversion is impossible.
2020-09-23 20:25:45 +08:00
*
* Exported for testing.
2020-08-10 09:25:14 +02:00
*/
2020-11-19 23:03:45 +01:00
function getExtraOptions ( options , paths , pathInfo ) {
2021-07-27 17:59:59 +01:00
const all = asExtraOptions ( options === null || options === void 0 ? void 0 : options [ "*" ], pathInfo . concat ( "*" ));
2020-11-19 23:03:45 +01:00
const specific = paths . length === 0
2020-09-14 10:44:43 +01:00
? asExtraOptions ( options , pathInfo )
2021-07-27 17:59:59 +01:00
: getExtraOptions ( options === null || options === void 0 ? void 0 : options [ paths [ 0 ]], paths === null || paths === void 0 ? void 0 : paths . slice ( 1 ), pathInfo . concat ( paths [ 0 ]));
2020-08-10 09:25:14 +02:00
return all . concat ( specific );
}
exports . getExtraOptions = getExtraOptions ;
2021-06-24 17:30:42 +01:00
/*
* A constant defining the maximum number of characters we will keep from
* the programs stderr for logging. This serves two purposes:
* (1) It avoids an OOM if a program fails in a way that results it
* printing many log lines.
* (2) It avoids us hitting the limit of how much data we can send in our
* status reports on GitHub.com.
*/
const maxErrorSize = 20000 ;
2021-06-09 13:17:25 -07:00
async function runTool ( cmd , args = []) {
let output = "" ;
2021-06-24 17:30:42 +01:00
let error = "" ;
const exitCode = await new toolrunner . ToolRunner ( cmd , args , {
2021-06-09 13:17:25 -07:00
listeners : {
stdout : ( data ) => {
output += data . toString ();
},
2021-06-24 17:30:42 +01:00
stderr : ( data ) => {
const toRead = Math . min ( maxErrorSize - error . length , data . length );
error += data . toString ( "utf8" , 0 , toRead );
},
2021-06-09 13:17:25 -07:00
},
2021-06-24 17:30:42 +01:00
ignoreReturnCode : true ,
2021-06-09 13:17:25 -07:00
}). exec ();
2021-06-24 17:30:42 +01:00
if ( exitCode !== 0 )
throw new CommandInvocationError ( cmd , args , exitCode , error );
2021-06-09 13:17:25 -07:00
return output ;
}
2020-06-26 17:22:19 +01:00
//# sourceMappingURL=codeql.js.map