diff --git a/lib/entry-points.js b/lib/entry-points.js index 73417126b..b190966e8 100644 --- a/lib/entry-points.js +++ b/lib/entry-points.js @@ -163654,27 +163654,27 @@ async function getCliVersionFromFeatures(features) { const gitHubVersion = await getGitHubVersion(); return await features.getEnabledDefaultCliVersions(gitHubVersion.type); } -async function getDownloadUrl(logger, features) { - let platform2 = getBundlePlatform(); +async function getDownloadUrl(action) { + let platform2 = getBundlePlatform(action.platform, action.arch); if (platform2 === void 0) { - logger.warning( + action.logger.warning( `Unsupported platform ${process.platform} on architecture ${process.arch}, defaulting to ${"linux64" /* Linux64 */}` ); platform2 = "linux64" /* Linux64 */; } const proxyPackage = getProxyPackage(platform2); try { - const useFeaturesToDetermineCLI = await features.getValue( + const useFeaturesToDetermineCLI = await action.features.getValue( "start_proxy_use_features_release" /* StartProxyUseFeaturesRelease */ ); - const versionInfo = useFeaturesToDetermineCLI ? (await getCliVersionFromFeatures(features)).enabledVersions[0] : { + const versionInfo = useFeaturesToDetermineCLI ? (await getCliVersionFromFeatures(action.features)).enabledVersions[0] : { cliVersion, tagName: bundleVersion }; const cliRelease = await getReleaseByVersion(versionInfo.tagName); for (const asset of cliRelease.data.assets) { if (asset.name === proxyPackage) { - logger.info( + action.logger.info( `Found '${proxyPackage}' in release '${versionInfo.tagName}' at '${asset.url}'` ); return { @@ -163687,11 +163687,11 @@ async function getDownloadUrl(logger, features) { } } } catch (ex) { - logger.warning( + action.logger.warning( `Failed to retrieve information about the linked release: ${getErrorMessage(ex)}` ); } - logger.info( + action.logger.info( `Did not find '${proxyPackage}' in the linked release, falling back to hard-coded version.` ); return { @@ -163740,9 +163740,10 @@ async function cacheProxy(logger, source, filename, version) { function getProxyFilename() { return process.platform === "win32" ? `${UPDATEJOB_PROXY}.exe` : UPDATEJOB_PROXY; } -async function getProxyBinaryPath(logger, features) { +async function getProxyBinaryPath(action) { + const logger = action.logger; const proxyFileName = getProxyFilename(); - const proxyInfo = await getDownloadUrl(logger, features); + const proxyInfo = await getDownloadUrl(action); let proxyBin = toolcache4.find(proxyFileName, proxyInfo.version); if (!proxyBin) { const apiDetails = getApiDetails(); @@ -164122,7 +164123,7 @@ async function run7(action) { all_credentials: credentials, ca }; - const proxyBin = await getProxyBinaryPath(logger, features); + const proxyBin = await getProxyBinaryPath({ ...action, features }); const proxyInfo = await startProxy( proxyBin, proxyConfig, diff --git a/src/start-proxy-action.ts b/src/start-proxy-action.ts index e8b89732f..d965a2dd6 100644 --- a/src/start-proxy-action.ts +++ b/src/start-proxy-action.ts @@ -98,7 +98,7 @@ async function run(action: ActionState<["Base", "Logger", "Env", "Actions"]>) { }; // Start the Proxy - const proxyBin = await getProxyBinaryPath(logger, features); + const proxyBin = await getProxyBinaryPath({ ...action, features }); const proxyInfo = await startProxy( proxyBin, proxyConfig, diff --git a/src/start-proxy.test.ts b/src/start-proxy.test.ts index 6e22aa85b..9b2ad8123 100644 --- a/src/start-proxy.test.ts +++ b/src/start-proxy.test.ts @@ -5,6 +5,7 @@ import * as toolcache from "@actions/tool-cache"; import test, { ExecutionContext } from "ava"; import sinon from "sinon"; +import { ActionState } from "./action-common"; import * as apiClient from "./api-client"; import * as defaults from "./defaults.json"; import { setUpFeatureFlagTests } from "./feature-flags/testing-util"; @@ -19,6 +20,7 @@ import { assertNotLogged, checkExpectedLogMessages, createFeatures, + initAllState, makeMacro, makeTestToken, RecordingLogger, @@ -722,9 +724,12 @@ function mockOfflineFeatures(tempDir: string, logger: Logger) { return setUpFeatureFlagTests(tempDir, logger, gitHubVersion); } -/** The result of `getBundlePlatform` or `Linux64` if `undefined`. */ -const testPlatform: BundlePlatform = - getBundlePlatform() ?? BundlePlatform.Linux64; +/** Gets the `BundlePlatform` based on the `action` state, but defaults to `Linux64` if undefined. */ +function getTestPlatform(action: ActionState<["Base"]>) { + return ( + getBundlePlatform(action.platform, action.arch) ?? BundlePlatform.Linux64 + ); +} test.serial( "getDownloadUrl returns fallback when `getReleaseByVersion` rejects", @@ -734,16 +739,14 @@ test.serial( await withTmpDir(async (tempDir) => { const features = mockOfflineFeatures(tempDir, logger); - const info = await startProxyExports.getDownloadUrl( - getRunnerLogger(true), - features, - ); + const state = initAllState({ logger, features }); + const info = await startProxyExports.getDownloadUrl(state); t.is(info.version, startProxyExports.UPDATEJOB_PROXY_VERSION); t.is( info.url, startProxyExports.getFallbackUrl( - startProxyExports.getProxyPackage(testPlatform), + startProxyExports.getProxyPackage(getTestPlatform(state)), ), ); }); @@ -758,19 +761,17 @@ test.serial( await withTmpDir(async (tempDir) => { const features = mockOfflineFeatures(tempDir, logger); + const state = initAllState({ logger, features }); for (const assets of testAssets) { const stub = mockGetReleaseByTag(assets); - const info = await startProxyExports.getDownloadUrl( - getRunnerLogger(true), - features, - ); + const info = await startProxyExports.getDownloadUrl(state); t.is(info.version, startProxyExports.UPDATEJOB_PROXY_VERSION); t.is( info.url, startProxyExports.getFallbackUrl( - startProxyExports.getProxyPackage(testPlatform), + startProxyExports.getProxyPackage(getTestPlatform(state)), ), ); @@ -782,10 +783,11 @@ test.serial( test.serial("getDownloadUrl returns matching release asset", async (t) => { const logger = new RecordingLogger(); + const state = initAllState({ logger }); const assets = [ { name: "foo", url: "other-url" }, { - name: startProxyExports.getProxyPackage(testPlatform), + name: startProxyExports.getProxyPackage(getTestPlatform(state)), url: "url-we-want", }, ]; @@ -793,10 +795,7 @@ test.serial("getDownloadUrl returns matching release asset", async (t) => { await withTmpDir(async (tempDir) => { const features = mockOfflineFeatures(tempDir, logger); - const info = await startProxyExports.getDownloadUrl( - getRunnerLogger(true), - features, - ); + const info = await startProxyExports.getDownloadUrl({ ...state, features }); t.is(info.version, defaults.cliVersion); t.is(info.url, "url-we-want"); @@ -930,7 +929,9 @@ test.serial( sinon.stub(toolcache, "find").returns(toolcachePath); const features = mockOfflineFeatures(tempDir, logger); - const path = await startProxyExports.getProxyBinaryPath(logger, features); + const path = await startProxyExports.getProxyBinaryPath( + initAllState({ logger, features }), + ); t.assert(path); t.is( @@ -945,10 +946,11 @@ test.serial( "getProxyBinaryPath - downloads proxy if not in cache", async (t) => { const logger = new RecordingLogger(); + const state = initAllState({ logger }); const downloadUrl = "url-we-want"; mockGetReleaseByTag([ { - name: startProxyExports.getProxyPackage(testPlatform), + name: startProxyExports.getProxyPackage(getTestPlatform(state)), url: downloadUrl, }, ]); @@ -973,10 +975,10 @@ test.serial( .resolves(extractedPath); const cacheDir = sinon.stub(toolcache, "cacheDir").resolves(toolcachePath); - const path = await startProxyExports.getProxyBinaryPath( - logger, - createFeatures([]), - ); + const path = await startProxyExports.getProxyBinaryPath({ + ...state, + features: createFeatures([]), + }); t.assert(find.calledOnce); t.assert(getApiDetails.calledOnce); @@ -991,7 +993,7 @@ test.serial( ); checkExpectedLogMessages(t, logger.messages, [ - `Found '${startProxyExports.getProxyPackage(testPlatform)}' in release '${defaults.bundleVersion}' at '${downloadUrl}'`, + `Found '${startProxyExports.getProxyPackage(getTestPlatform(state))}' in release '${defaults.bundleVersion}' at '${downloadUrl}'`, ]); }, ); @@ -1000,6 +1002,7 @@ test.serial( "getProxyBinaryPath - downloads proxy based on features if not in cache", async (t) => { const logger = new RecordingLogger(); + const state = initAllState({ logger }); const expectedTag = "codeql-bundle-v2.20.1"; const expectedParams = { owner: "github", @@ -1009,7 +1012,7 @@ test.serial( const downloadUrl = "url-we-want"; const assets = [ { - name: startProxyExports.getProxyPackage(testPlatform), + name: startProxyExports.getProxyPackage(getTestPlatform(state)), url: downloadUrl, }, ]; @@ -1060,7 +1063,10 @@ test.serial( .resolves({ enabledVersions: [{ cliVersion: "2.20.1", tagName: expectedTag }], }); - const path = await startProxyExports.getProxyBinaryPath(logger, features); + const path = await startProxyExports.getProxyBinaryPath({ + ...state, + features, + }); t.assert(getDefaultCliVersion.calledOnce); sinon.assert.calledOnceWithMatch( @@ -1082,7 +1088,7 @@ test.serial( }); checkExpectedLogMessages(t, logger.messages, [ - `Found '${startProxyExports.getProxyPackage(testPlatform)}' in release '${expectedTag}' at '${downloadUrl}'`, + `Found '${startProxyExports.getProxyPackage(getTestPlatform(state))}' in release '${expectedTag}' at '${downloadUrl}'`, ]); }, ); diff --git a/src/start-proxy.ts b/src/start-proxy.ts index b01f35222..c8f626bad 100644 --- a/src/start-proxy.ts +++ b/src/start-proxy.ts @@ -415,19 +415,17 @@ async function getCliVersionFromFeatures( * Determines the URL of the proxy release asset that we should download if its not * already in the toolcache, and its version. * - * @param logger The logger to use. - * @param features Information about enabled features. + * @param action The action state. * @returns Returns the download URL and version of the proxy package we plan to use. */ export async function getDownloadUrl( - logger: Logger, - features: FeatureEnablement, + action: ActionState<["Base", "Logger", "FeatureFlags"]>, ): Promise<{ url: string; version: string }> { // Default to linux64 if we don't recognise the platform+arch pair. // This maintains the behaviour we had before switching to `getBundlePlatform` here. - let platform = getBundlePlatform(); + let platform = getBundlePlatform(action.platform, action.arch); if (platform === undefined) { - logger.warning( + action.logger.warning( `Unsupported platform ${process.platform} on architecture ${process.arch}, defaulting to ${BundlePlatform.Linux64}`, ); platform = BundlePlatform.Linux64; @@ -436,14 +434,14 @@ export async function getDownloadUrl( const proxyPackage = getProxyPackage(platform); try { - const useFeaturesToDetermineCLI = await features.getValue( + const useFeaturesToDetermineCLI = await action.features.getValue( Feature.StartProxyUseFeaturesRelease, ); // Retrieve information about the CLI version we should use. This will be either the linked // version, or the one enabled by FFs. const versionInfo = useFeaturesToDetermineCLI - ? (await getCliVersionFromFeatures(features)).enabledVersions[0] + ? (await getCliVersionFromFeatures(action.features)).enabledVersions[0] : { cliVersion: defaults.cliVersion, tagName: defaults.bundleVersion, @@ -455,7 +453,7 @@ export async function getDownloadUrl( // Search the release's assets to find the one we are looking for. for (const asset of cliRelease.data.assets) { if (asset.name === proxyPackage) { - logger.info( + action.logger.info( `Found '${proxyPackage}' in release '${versionInfo.tagName}' at '${asset.url}'`, ); return { @@ -468,13 +466,13 @@ export async function getDownloadUrl( } } } catch (ex) { - logger.warning( + action.logger.warning( `Failed to retrieve information about the linked release: ${getErrorMessage(ex)}`, ); } // Fallback to the hard-coded URL. - logger.info( + action.logger.info( `Did not find '${proxyPackage}' in the linked release, falling back to hard-coded version.`, ); return { @@ -567,15 +565,15 @@ export function getProxyFilename() { * runner's tool cache. Otherwise, it downloads and extracts the proxy binary, * and stores it in the tool cache. * - * @param logger The logger to use. + * @param action The action state. * @returns The path to the proxy binary. */ export async function getProxyBinaryPath( - logger: Logger, - features: FeatureEnablement, + action: ActionState<["Base", "Logger", "FeatureFlags"]>, ): Promise { + const logger = action.logger; const proxyFileName = getProxyFilename(); - const proxyInfo = await getDownloadUrl(logger, features); + const proxyInfo = await getDownloadUrl(action); let proxyBin = toolcache.find(proxyFileName, proxyInfo.version); if (!proxyBin) {