@@ -18,6 +18,7 @@ import {
1818 LoggedMessage ,
1919 SAMPLE_DEFAULT_CLI_VERSION ,
2020 SAMPLE_DOTCOM_API_DETAILS ,
21+ SAMPLE_GHES_API_DETAILS ,
2122 checkExpectedLogMessages ,
2223 createFeatures ,
2324 createTestConfig ,
@@ -435,6 +436,47 @@ function mockListCodeQLBundleReleasesWithNightlyFallback(
435436 } as any ) ;
436437}
437438
439+ /**
440+ * As `mockListStableCodeQLBundleReleases`, but for use when `getCodeQLSource` is called with a
441+ * non-dotcom `variant` (for example `GitHubVariant.GHES` or `GitHubVariant.GHEC_DR`). The
442+ * canonical CodeQL Action repository's release history only ever exists on GitHub.com, so in that
443+ * case we must mock the unauthenticated GitHub.com client (`getUnauthenticatedApiClientForDotcom`)
444+ * rather than the current instance's client (`getApiClient`).
445+ *
446+ * The current instance's client is also mocked here, but to return a different set of bogus
447+ * releases and to fail to find the bundle by tag, so that tests using this helper fail if
448+ * `getSortedCliVersions` regresses to (incorrectly) querying the current GitHub instance, or if
449+ * `getCodeQLBundleDownloadURL`'s unrelated, pre-existing attempt to look up the bundle on the
450+ * current instance is not handled gracefully.
451+ */
452+ function mockListStableCodeQLBundleReleasesForNonDotcomVariant (
453+ releases : unknown [ ] = STABLE_BUNDLE_RELEASES_TEST_SET ,
454+ ) {
455+ const dotcomClient = github . getOctokit ( "123" ) ;
456+ const listReleases = sinon . stub ( dotcomClient . rest . repos , "listReleases" ) ;
457+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
458+ listReleases . resolves ( {
459+ data : releases ,
460+ } as any ) ;
461+ sinon
462+ . stub ( api , "getUnauthenticatedApiClientForDotcom" )
463+ . value ( ( ) => dotcomClient ) ;
464+
465+ const currentInstanceClient = github . getOctokit ( "456" ) ;
466+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
467+ sinon . stub ( currentInstanceClient . rest . repos , "listReleases" ) . resolves ( {
468+ data : [
469+ { tag_name : "codeql-bundle-v9.9.9" , prerelease : false , draft : false } ,
470+ ] ,
471+ } as any ) ;
472+ sinon
473+ . stub ( currentInstanceClient . rest . repos , "getReleaseByTag" )
474+ . rejects ( new Error ( "Not Found" ) ) ;
475+ sinon . stub ( api , "getApiClient" ) . value ( ( ) => currentInstanceClient ) ;
476+
477+ return listReleases ;
478+ }
479+
438480const LATEST_OFFSET_TOOLS_INPUT_TEST_CASES = [
439481 { toolsInput : "latest-0" , expectedCliVersion : "2.25.3" } ,
440482 { toolsInput : "latest-1" , expectedCliVersion : "2.25.1" } ,
@@ -924,6 +966,62 @@ for (const {
924966 ) ;
925967}
926968
969+ /**
970+ * `latest-<N>`, SemVer ranges, `latest-prerelease`, and `nightly-until-<version>`/
971+ * `nightly-until-stable-<version>` all need to look up the canonical CodeQL Action repository's
972+ * release history. That repository only ever exists on GitHub.com, so on a non-dotcom `variant`
973+ * (GHES or GHEC with data residency) this lookup must be made directly, and unauthenticated,
974+ * against GitHub.com, rather than against the current GitHub instance as for all other API
975+ * requests. Otherwise, the request would either fail outright (since the canonical repository
976+ * typically does not exist on the current instance), or -- if a same-named repository happens to
977+ * exist there -- could silently resolve to the wrong CodeQL CLI version.
978+ */
979+ const NON_DOTCOM_VARIANT_TOOLS_INPUT_TEST_CASES = [
980+ { toolsInput : "latest-1" , expectedCliVersion : "2.25.1" } ,
981+ { toolsInput : "^2.24.0" , expectedCliVersion : "2.25.3" } ,
982+ { toolsInput : "latest-prerelease" , expectedCliVersion : "2.26.0" } ,
983+ { toolsInput : "nightly-until-2.20.0" , expectedCliVersion : "2.26.0" } ,
984+ ] as const ;
985+
986+ for ( const variant of [ GitHubVariant . GHES , GitHubVariant . GHEC_DR ] ) {
987+ for ( const {
988+ toolsInput,
989+ expectedCliVersion,
990+ } of NON_DOTCOM_VARIANT_TOOLS_INPUT_TEST_CASES ) {
991+ test . serial (
992+ `getCodeQLSource resolves 'tools: ${ toolsInput } ' to CodeQL version ${ expectedCliVersion } ` +
993+ `on ${ variant } using GitHub.com, rather than the current instance` ,
994+ async ( t ) => {
995+ const features = createFeatures ( [ ] ) ;
996+ sinon . stub ( process , "platform" ) . value ( "linux" ) ;
997+ mockListStableCodeQLBundleReleasesForNonDotcomVariant ( ) ;
998+
999+ await withTmpDir ( async ( tmpDir ) => {
1000+ setupActionsVars ( tmpDir , tmpDir ) ;
1001+ const source = await setupCodeql . getCodeQLSource (
1002+ toolsInput ,
1003+ SAMPLE_DEFAULT_CLI_VERSION ,
1004+ undefined , // rawLanguages
1005+ false , // useOverlayAwareDefaultCliVersion
1006+ SAMPLE_GHES_API_DETAILS ,
1007+ variant ,
1008+ false ,
1009+ features ,
1010+ getRunnerLogger ( true ) ,
1011+ ) ;
1012+
1013+ t . is ( source . sourceType , "download" ) ;
1014+ // If resolution incorrectly used the current (non-dotcom) instance's API instead of
1015+ // GitHub.com, this would instead resolve to the bogus "9.9.9" release configured by
1016+ // `mockListStableCodeQLBundleReleasesForNonDotcomVariant`.
1017+ t . is ( source . toolsVersion , expectedCliVersion ) ;
1018+ t . is ( source [ "cliVersion" ] , expectedCliVersion ) ;
1019+ } ) ;
1020+ } ,
1021+ ) ;
1022+ }
1023+ }
1024+
9271025test . serial (
9281026 "getCodeQLSource correctly returns bundled CLI version when tools == latest" ,
9291027 async ( t ) => {
0 commit comments