From e62c6428e7fceb13121cbf165c1996765bd8393b Mon Sep 17 00:00:00 2001 From: Bassem Dghaidi <568794+Link-@users.noreply.github.com> Date: Tue, 24 Sep 2024 03:29:14 -0700 Subject: [PATCH] Fix service urls --- packages/cache/src/cache.ts | 18 ++++++++++-------- packages/cache/src/internal/cacheHttpClient.ts | 4 ++-- .../cache/src/internal/cacheTwirpClient.ts | 5 ++--- packages/cache/src/internal/config.ts | 16 ++++++++++++++++ packages/cache/src/internal/constants.ts | 6 +----- 5 files changed, 31 insertions(+), 18 deletions(-) diff --git a/packages/cache/src/cache.ts b/packages/cache/src/cache.ts index 0530aaab..321610cd 100644 --- a/packages/cache/src/cache.ts +++ b/packages/cache/src/cache.ts @@ -1,7 +1,7 @@ import * as core from '@actions/core' import * as path from 'path' import * as utils from './internal/cacheUtils' -import { CacheServiceVersion, CacheUrl } from './internal/constants' +import * as config from './internal/config' import * as cacheHttpClient from './internal/cacheHttpClient' import * as cacheTwirpClient from './internal/cacheTwirpClient' import { createTar, extractTar, listTar } from './internal/tar' @@ -67,9 +67,9 @@ function checkKey(key: string): void { * @returns boolean return true if Actions cache service feature is available, otherwise false */ -export function isFeatureAvailable(): boolean { - return !!CacheUrl -} +// export function isFeatureAvailable(): boolean { +// return !!CacheUrl +// } /** * Restores cache from keys @@ -90,8 +90,9 @@ export async function restoreCache( ): Promise { checkPaths(paths) - console.debug(`Cache Service Version: ${CacheServiceVersion}`) - switch (CacheServiceVersion) { + const cacheServiceVersion: string = config.getCacheServiceVersion() + console.debug(`Cache Service Version: ${cacheServiceVersion}`) + switch (cacheServiceVersion) { case "v2": return await restoreCachev2(paths, primaryKey, restoreKeys, options, enableCrossOsArchive) case "v1": @@ -265,8 +266,9 @@ export async function saveCache( checkPaths(paths) checkKey(key) - console.debug(`Cache Service Version: ${CacheServiceVersion}`) - switch (CacheServiceVersion) { + const cacheServiceVersion: string = config.getCacheServiceVersion() + console.debug(`Cache Service Version: ${cacheServiceVersion}`) + switch (cacheServiceVersion) { case "v2": return await saveCachev2(paths, key, options, enableCrossOsArchive) case "v1": diff --git a/packages/cache/src/internal/cacheHttpClient.ts b/packages/cache/src/internal/cacheHttpClient.ts index 8fe76376..98d6a3bb 100644 --- a/packages/cache/src/internal/cacheHttpClient.ts +++ b/packages/cache/src/internal/cacheHttpClient.ts @@ -33,10 +33,10 @@ import { retryHttpClientResponse, retryTypedResponse } from './requestUtils' -import { CacheUrl } from './constants' +import { getCacheServiceURL } from './config' function getCacheApiUrl(resource: string): string { - const baseUrl: string = CacheUrl || '' + const baseUrl: string = getCacheServiceURL() if (!baseUrl) { throw new Error('Cache Service Url not found, unable to restore cache.') } diff --git a/packages/cache/src/internal/cacheTwirpClient.ts b/packages/cache/src/internal/cacheTwirpClient.ts index 6d9826ac..cc365ec6 100644 --- a/packages/cache/src/internal/cacheTwirpClient.ts +++ b/packages/cache/src/internal/cacheTwirpClient.ts @@ -2,8 +2,7 @@ import { HttpClient, HttpClientResponse, HttpCodes } from '@actions/http-client' import { BearerCredentialHandler } from '@actions/http-client/lib/auth' import { info, debug } from '@actions/core' import { CacheServiceClientJSON } from '../generated/results/api/v1/cache.twirp' -import { CacheUrl } from './constants' -import { getRuntimeToken } from './config' +import { getRuntimeToken, getCacheServiceURL } from './config' // import {getUserAgentString} from './user-agent' // import {NetworkError, UsageError} from './errors' @@ -31,7 +30,7 @@ class CacheServiceClient implements Rpc { retryMultiplier?: number ) { const token = getRuntimeToken() - this.baseUrl = CacheUrl + this.baseUrl = getCacheServiceURL() if (maxAttempts) { this.maxAttempts = maxAttempts } diff --git a/packages/cache/src/internal/config.ts b/packages/cache/src/internal/config.ts index 959f3f46..54844396 100644 --- a/packages/cache/src/internal/config.ts +++ b/packages/cache/src/internal/config.ts @@ -5,3 +5,19 @@ export function getRuntimeToken(): string { } return token } + +export function getCacheServiceVersion(): string { + return process.env['ACTIONS_CACHE_SERVICE_VERSION'] || 'v1' +} + +export function getCacheServiceURL(): string { + const version = getCacheServiceVersion() + switch (version) { + case 'v1': + return process.env['ACTIONS_CACHE_URL'] || process.env['ACTIONS_RESULTS_URL'] || "" + case 'v2': + return process.env['ACTIONS_RESULTS_URL'] || "" + default: + throw new Error(`Unsupported cache service version: ${version}`) + } +} \ No newline at end of file diff --git a/packages/cache/src/internal/constants.ts b/packages/cache/src/internal/constants.ts index 143ba06e..b2cddf96 100644 --- a/packages/cache/src/internal/constants.ts +++ b/packages/cache/src/internal/constants.ts @@ -35,8 +35,4 @@ export const SystemTarPathOnWindows = `${process.env['SYSTEMDRIVE']}\\Windows\\S export const TarFilename = 'cache.tar' -export const ManifestFilename = 'manifest.txt' - -// Cache Service Metadata -export const CacheUrl = `${process.env['ACTIONS_CACHE_URL_NEXT']} || ${process.env['ACTIONS_CACHE_URL']}` -export const CacheServiceVersion = `${process.env['ACTIONS_CACHE_URL_NEXT'] ? 'v2' : 'v1'}` \ No newline at end of file +export const ManifestFilename = 'manifest.txt' \ No newline at end of file