diff --git a/lib/start-proxy-action.js b/lib/start-proxy-action.js index f234f866c..1c272cc61 100644 --- a/lib/start-proxy-action.js +++ b/lib/start-proxy-action.js @@ -119796,6 +119796,16 @@ async function extractProxy(logger, archive) { throw new StartProxyError("Failed to extract proxy archive." /* ExtractionFailed */); } } +async function cacheProxy(logger, path2, filename, version) { + try { + return await toolcache.cacheDir(path2, filename, version); + } catch (error3) { + logger.error( + `Failed to add proxy archive from ${path2} to toolcache: ${getErrorMessage(error3)}` + ); + throw new StartProxyError("Failed to add proxy to toolcache" /* CacheFailed */); + } +} // src/start-proxy-action.ts var KEY_SIZE = 2048; @@ -119960,7 +119970,8 @@ async function getProxyBinaryPath(logger) { ); const temp = await downloadProxy(logger, proxyInfo.url, authorization); const extracted = await extractProxy(logger, temp); - proxyBin = await toolcache2.cacheDir( + proxyBin = await cacheProxy( + logger, extracted, proxyFileName, proxyInfo.version diff --git a/src/start-proxy-action.ts b/src/start-proxy-action.ts index fd00953e7..db55a9e05 100644 --- a/src/start-proxy-action.ts +++ b/src/start-proxy-action.ts @@ -10,6 +10,7 @@ import { getApiDetails, getAuthorizationHeaderFor } from "./api-client"; import { KnownLanguage } from "./languages"; import { getActionsLogger, Logger } from "./logging"; import { + cacheProxy, Credential, credentialToStr, downloadProxy, @@ -242,7 +243,8 @@ async function getProxyBinaryPath(logger: Logger): Promise { ); const temp = await downloadProxy(logger, proxyInfo.url, authorization); const extracted = await extractProxy(logger, temp); - proxyBin = await toolcache.cacheDir( + proxyBin = await cacheProxy( + logger, extracted, proxyFileName, proxyInfo.version, diff --git a/src/start-proxy.test.ts b/src/start-proxy.test.ts index ee9fbbd20..a5a089673 100644 --- a/src/start-proxy.test.ts +++ b/src/start-proxy.test.ts @@ -447,3 +447,29 @@ test( await startProxyExports.extractProxy(logger, "path"); }, ); + +test("cacheProxy - returns file path on success", async (t) => { + await withRecordingLoggerAsync(async (logger) => { + const testPath = "/some/path"; + sinon.stub(toolcache, "cacheDir").resolves(testPath); + + const result = await startProxyExports.cacheProxy( + logger, + "/other/path", + "proxy", + "1.0", + ); + t.is(result, testPath); + }); +}); + +test( + "cacheProxy", + wrapFailureTest, + () => { + sinon.stub(toolcache, "cacheDir").throws(); + }, + async (logger) => { + await startProxyExports.cacheProxy(logger, "/other/path", "proxy", "1.0"); + }, +); diff --git a/src/start-proxy.ts b/src/start-proxy.ts index 65ffc5c0e..16bf40367 100644 --- a/src/start-proxy.ts +++ b/src/start-proxy.ts @@ -24,6 +24,7 @@ import { ConfigurationError, getErrorMessage, isDefined } from "./util"; export enum StartProxyErrorType { DownloadFailed = "Failed to download proxy archive.", ExtractionFailed = "Failed to extract proxy archive.", + CacheFailed = "Failed to add proxy to toolcache", } /** @@ -453,3 +454,28 @@ export async function extractProxy(logger: Logger, archive: string) { throw new StartProxyError(StartProxyErrorType.ExtractionFailed); } } + +/** + * Attempts to store the proxy in the toolcache. + * + * @param logger The logger to use. + * @param path The source path to add to the toolcache. + * @param filename The filename of the proxy binary. + * @param version The version of the proxy. + * @returns The path to the directory in the toolcache. + */ +export async function cacheProxy( + logger: Logger, + path: string, + filename: string, + version: string, +) { + try { + return await toolcache.cacheDir(path, filename, version); + } catch (error) { + logger.error( + `Failed to add proxy archive from ${path} to toolcache: ${getErrorMessage(error)}`, + ); + throw new StartProxyError(StartProxyErrorType.CacheFailed); + } +}