2020-06-23 19:08:30 +01:00
"use strict" ;
var _ _importStar = ( this && this . _ _importStar ) || function ( mod ) {
if ( mod && mod . _ _esModule ) return mod ;
var result = { } ;
if ( mod != null ) for ( var k in mod ) if ( Object . hasOwnProperty . call ( mod , k ) ) result [ k ] = mod [ k ] ;
result [ "default" ] = mod ;
return result ;
} ;
var _ _importDefault = ( this && this . _ _importDefault ) || function ( mod ) {
return ( mod && mod . _ _esModule ) ? mod : { "default" : mod } ;
} ;
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
2020-09-29 14:43:37 +01:00
const path = _ _importStar ( require ( "path" ) ) ;
2020-10-01 11:03:30 +01:00
const githubUtils = _ _importStar ( require ( "@actions/github/lib/utils" ) ) ;
2020-09-21 11:06:21 +01:00
const retry = _ _importStar ( require ( "@octokit/plugin-retry" ) ) ;
2020-06-23 19:08:30 +01:00
const console _log _level _1 = _ _importDefault ( require ( "console-log-level" ) ) ;
2020-10-26 22:58:05 +00:00
const semver = _ _importStar ( require ( "semver" ) ) ;
2020-09-15 14:00:25 +01:00
const actions _util _1 = require ( "./actions-util" ) ;
2020-10-26 22:58:05 +00:00
const apiCompatibility = _ _importStar ( require ( "./api-compatibility.json" ) ) ;
const logging = _ _importStar ( require ( "./logging" ) ) ;
2020-08-04 14:35:20 -07:00
const util _1 = require ( "./util" ) ;
2020-10-26 22:58:05 +00:00
var DisallowedAPIVersionReason ;
( function ( DisallowedAPIVersionReason ) {
DisallowedAPIVersionReason [ DisallowedAPIVersionReason [ "ACTION_TOO_OLD" ] = 0 ] = "ACTION_TOO_OLD" ;
DisallowedAPIVersionReason [ DisallowedAPIVersionReason [ "ACTION_TOO_NEW" ] = 1 ] = "ACTION_TOO_NEW" ;
} ) ( DisallowedAPIVersionReason = exports . DisallowedAPIVersionReason || ( exports . DisallowedAPIVersionReason = { } ) ) ;
const GITHUB _ENTERPRISE _VERSION _HEADER = "x-github-enterprise-version" ;
let hasBeenWarnedAboutVersion = false ;
exports . getApiClient = function ( githubAuth , githubUrl , mode , allowLocalRun = false ) {
2020-08-04 14:35:20 -07:00
if ( util _1 . isLocalRun ( ) && ! allowLocalRun ) {
2020-09-14 10:44:43 +01:00
throw new Error ( "Invalid API call in local run" ) ;
2020-08-04 14:35:20 -07:00
}
2020-10-26 22:58:05 +00:00
const customOctokit = githubUtils . GitHub . plugin ( retry . retry , ( octokit , _ ) => {
octokit . hook . after ( "request" , ( response , _ ) => {
if ( ! hasBeenWarnedAboutVersion &&
Object . prototype . hasOwnProperty . call ( response . headers , GITHUB _ENTERPRISE _VERSION _HEADER ) ) {
const installedVersion = response . headers [ GITHUB _ENTERPRISE _VERSION _HEADER ] ;
const disallowedAPIVersionReason = apiVersionInRange ( installedVersion , apiCompatibility . minimumVersion , apiCompatibility . maximumVersion ) ;
const logger = mode === "actions"
? logging . getActionsLogger ( )
: logging . getRunnerLogger ( false ) ;
const toolName = mode === "actions" ? "Action" : "Runner" ;
if ( disallowedAPIVersionReason ===
DisallowedAPIVersionReason . ACTION _TOO _OLD ) {
logger . warning ( ` The CodeQL ${ toolName } version you are using is too old to be compatible with GitHub Enterprise ${ installedVersion } . If you experience issues, please upgrade to a more recent version of the CodeQL ${ toolName } . ` ) ;
}
if ( disallowedAPIVersionReason ===
DisallowedAPIVersionReason . ACTION _TOO _NEW ) {
logger . warning ( ` GitHub Enterprise ${ installedVersion } is too old to be compatible with this version of the CodeQL ${ toolName } . If you experience issues, please upgrade to a more recent version of GitHub Enterprise or use an older version of the CodeQL ${ toolName } . ` ) ;
}
hasBeenWarnedAboutVersion = true ;
}
} ) ;
} ) ;
return new customOctokit ( githubUtils . getOctokitOptions ( githubAuth , {
2020-08-25 16:19:15 +01:00
baseUrl : getApiUrl ( githubUrl ) ,
2020-07-06 16:04:02 +01:00
userAgent : "CodeQL Action" ,
2020-09-14 10:44:43 +01:00
log : console _log _level _1 . default ( { level : "debug" } ) ,
2020-09-21 11:06:21 +01:00
} ) ) ;
2020-07-06 16:04:02 +01:00
} ;
2020-08-25 16:19:15 +01:00
function getApiUrl ( githubUrl ) {
const url = new URL ( githubUrl ) ;
// If we detect this is trying to be to github.com
// then return with a fixed canonical URL.
2020-09-14 10:44:43 +01:00
if ( url . hostname === "github.com" || url . hostname === "api.github.com" ) {
return "https://api.github.com" ;
2020-08-25 16:19:15 +01:00
}
// Add the /api/v3 API prefix
2020-09-14 10:44:43 +01:00
url . pathname = path . join ( url . pathname , "api" , "v3" ) ;
2020-08-25 16:19:15 +01:00
return url . toString ( ) ;
}
2020-08-11 12:43:27 +01:00
// Temporary function to aid in the transition to running on and off of github actions.
// Once all code has been coverted this function should be removed or made canonical
// and called only from the action entrypoints.
function getActionsApiClient ( allowLocalRun = false ) {
2020-10-26 22:58:05 +00:00
return exports . getApiClient ( actions _util _1 . getRequiredInput ( "token" ) , actions _util _1 . getRequiredEnvParam ( "GITHUB_SERVER_URL" ) , "actions" , allowLocalRun ) ;
2020-08-11 12:43:27 +01:00
}
exports . getActionsApiClient = getActionsApiClient ;
2020-10-26 22:58:05 +00:00
function apiVersionInRange ( version , minimumVersion , maximumVersion ) {
if ( ! semver . satisfies ( version , ` >= ${ minimumVersion } ` ) ) {
return DisallowedAPIVersionReason . ACTION _TOO _NEW ;
}
if ( ! semver . satisfies ( version , ` <= ${ maximumVersion } ` ) ) {
return DisallowedAPIVersionReason . ACTION _TOO _OLD ;
}
return undefined ;
}
exports . apiVersionInRange = apiVersionInRange ;
2020-06-23 19:08:30 +01:00
//# sourceMappingURL=api-client.js.map