From d958ca36603d0de43dc79c535fb09419d407d5a7 Mon Sep 17 00:00:00 2001 From: Nogic <24802730+nogic1008@users.noreply.github.com> Date: Thu, 27 Apr 2023 00:36:46 +0000 Subject: [PATCH] refactor: accept changes on cache-utils --- __tests__/cache-utils.test.ts | 29 +++++++++++++++++++++++ dist/cache-save/index.js | 34 +++++++++++++-------------- dist/setup/index.js | 34 +++++++++++++-------------- src/cache-utils.ts | 44 ++++++++++++++++++----------------- 4 files changed, 84 insertions(+), 57 deletions(-) diff --git a/__tests__/cache-utils.test.ts b/__tests__/cache-utils.test.ts index a3899e8..e13309f 100644 --- a/__tests__/cache-utils.test.ts +++ b/__tests__/cache-utils.test.ts @@ -16,6 +16,20 @@ http-cache: /home/codespace/.local/share/NuGet/v3-cache global-packages: /var/nuget temp: /tmp/NuGetScratch plugins-cache: /home/codespace/.local/share/NuGet/plugins-cache +`, + { + 'http-cache': '/home/codespace/.local/share/NuGet/v3-cache', + 'global-packages': '/var/nuget', + temp: '/tmp/NuGetScratch', + 'plugins-cache': '/home/codespace/.local/share/NuGet/plugins-cache' + } + ], + [ + ` + http-cache: /home/codespace/.local/share/NuGet/v3-cache + global-packages: /var/nuget + temp: /tmp/NuGetScratch + plugins-cache: /home/codespace/.local/share/NuGet/plugins-cache `, { 'http-cache': '/home/codespace/.local/share/NuGet/v3-cache', @@ -38,6 +52,21 @@ plugins-cache: C:\\Users\\user\\AppData\\Local\\NuGet\\plugins-cache 'plugins-cache': 'C:\\Users\\user\\AppData\\Local\\NuGet\\plugins-cache' } + ], + [ + ` + http-cache: C:\\Users\\user\\AppData\\Local\\NuGet\\v3-cache + global-packages: C:\\Users\\user\\.nuget\\packages\\ + temp: C:\\Users\\user\\AppData\\Local\\Temp\\NuGetScratch + plugins-cache: C:\\Users\\user\\AppData\\Local\\NuGet\\plugins-cache + `, + { + 'http-cache': 'C:\\Users\\user\\AppData\\Local\\NuGet\\v3-cache', + 'global-packages': 'C:\\Users\\user\\.nuget\\packages\\', + temp: 'C:\\Users\\user\\AppData\\Local\\Temp\\NuGetScratch', + 'plugins-cache': + 'C:\\Users\\user\\AppData\\Local\\NuGet\\plugins-cache' + } ] ])('(stdout: "%s") returns %p', async (stdout, expected) => { jest diff --git a/dist/cache-save/index.js b/dist/cache-save/index.js index feb6c23..39c890f 100644 --- a/dist/cache-save/index.js +++ b/dist/cache-save/index.js @@ -58608,16 +58608,9 @@ const cache = __importStar(__nccwpck_require__(7799)); const core = __importStar(__nccwpck_require__(2186)); const exec = __importStar(__nccwpck_require__(1514)); const constants_1 = __nccwpck_require__(9042); -const folders = new Set([ - 'http-cache', - 'global-packages', - 'temp', - 'plugins-cache' -]); /** * Get NuGet global packages, cache, and temp folders from .NET CLI. * @returns (Folder Name)-(Path) mappings - * @summary This function only works in .NET Core SDK 3.1 and above. * @see https://docs.microsoft.com/nuget/consume-packages/managing-the-global-packages-and-cache-folders * @example * Windows @@ -58653,12 +58646,12 @@ const getNuGetFolderPath = () => __awaiter(void 0, void 0, void 0, function* () temp: '', 'plugins-cache': '' }; - const regex = /^([a-z-]+): (.+[/\\].+)$/gm; - let m; - while ((m = regex.exec(stdout)) !== null) { - const [, key, path] = m; - if (folders.has(key)) { - result[key] = path; + const regex = /(?:^|\s)(?[a-z-]+): (?.+[/\\].+)$/gm; + let match; + while ((match = regex.exec(stdout)) !== null) { + const key = match.groups.key; + if (key in result) { + result[key] = match.groups.path; } } return result; @@ -58669,16 +58662,21 @@ function isCacheFeatureAvailable() { return true; } if (isGhes()) { - throw new Error('Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.'); + core.warning('Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.'); + return false; } core.warning('The runner was not able to contact the cache service. Caching will be skipped'); return false; - function isGhes() { - const url = process.env['GITHUB_SERVER_URL'] || 'https://github.com'; - return new URL(url).hostname.toUpperCase() !== 'GITHUB.COM'; - } } exports.isCacheFeatureAvailable = isCacheFeatureAvailable; +/** + * Returns this action runs on GitHub Enterprise Server or not. + * (port from https://github.com/actions/toolkit/blob/457303960f03375db6f033e214b9f90d79c3fe5c/packages/cache/src/internal/cacheUtils.ts#L134) + */ +function isGhes() { + const url = process.env['GITHUB_SERVER_URL'] || 'https://github.com'; + return new URL(url).hostname.toUpperCase() !== 'GITHUB.COM'; +} /***/ }), diff --git a/dist/setup/index.js b/dist/setup/index.js index bde8fb4..199fa23 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -71019,16 +71019,9 @@ const cache = __importStar(__nccwpck_require__(7799)); const core = __importStar(__nccwpck_require__(2186)); const exec = __importStar(__nccwpck_require__(1514)); const constants_1 = __nccwpck_require__(9042); -const folders = new Set([ - 'http-cache', - 'global-packages', - 'temp', - 'plugins-cache' -]); /** * Get NuGet global packages, cache, and temp folders from .NET CLI. * @returns (Folder Name)-(Path) mappings - * @summary This function only works in .NET Core SDK 3.1 and above. * @see https://docs.microsoft.com/nuget/consume-packages/managing-the-global-packages-and-cache-folders * @example * Windows @@ -71064,12 +71057,12 @@ const getNuGetFolderPath = () => __awaiter(void 0, void 0, void 0, function* () temp: '', 'plugins-cache': '' }; - const regex = /^([a-z-]+): (.+[/\\].+)$/gm; - let m; - while ((m = regex.exec(stdout)) !== null) { - const [, key, path] = m; - if (folders.has(key)) { - result[key] = path; + const regex = /(?:^|\s)(?[a-z-]+): (?.+[/\\].+)$/gm; + let match; + while ((match = regex.exec(stdout)) !== null) { + const key = match.groups.key; + if (key in result) { + result[key] = match.groups.path; } } return result; @@ -71080,16 +71073,21 @@ function isCacheFeatureAvailable() { return true; } if (isGhes()) { - throw new Error('Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.'); + core.warning('Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.'); + return false; } core.warning('The runner was not able to contact the cache service. Caching will be skipped'); return false; - function isGhes() { - const url = process.env['GITHUB_SERVER_URL'] || 'https://github.com'; - return new URL(url).hostname.toUpperCase() !== 'GITHUB.COM'; - } } exports.isCacheFeatureAvailable = isCacheFeatureAvailable; +/** + * Returns this action runs on GitHub Enterprise Server or not. + * (port from https://github.com/actions/toolkit/blob/457303960f03375db6f033e214b9f90d79c3fe5c/packages/cache/src/internal/cacheUtils.ts#L134) + */ +function isGhes() { + const url = process.env['GITHUB_SERVER_URL'] || 'https://github.com'; + return new URL(url).hostname.toUpperCase() !== 'GITHUB.COM'; +} /***/ }), diff --git a/src/cache-utils.ts b/src/cache-utils.ts index 5c25179..34bd735 100644 --- a/src/cache-utils.ts +++ b/src/cache-utils.ts @@ -4,18 +4,15 @@ import * as exec from '@actions/exec'; import {cliCommand} from './constants'; -const folders = new Set([ - 'http-cache', - 'global-packages', - 'temp', - 'plugins-cache' -] as const); -type NuGetFolder = Parameters[0]; +type NuGetFolderName = + | 'http-cache' + | 'global-packages' + | 'temp' + | 'plugins-cache'; /** * Get NuGet global packages, cache, and temp folders from .NET CLI. * @returns (Folder Name)-(Path) mappings - * @summary This function only works in .NET Core SDK 3.1 and above. * @see https://docs.microsoft.com/nuget/consume-packages/managing-the-global-packages-and-cache-folders * @example * Windows @@ -53,20 +50,20 @@ export const getNuGetFolderPath = async () => { ); } - const result: Record = { + const result: Record = { 'http-cache': '', 'global-packages': '', temp: '', 'plugins-cache': '' }; - const regex = /^([a-z-]+): (.+[/\\].+)$/gm; + const regex = /(?:^|\s)(?[a-z-]+): (?.+[/\\].+)$/gm; - let m: RegExpExecArray | null; - while ((m = regex.exec(stdout)) !== null) { - const [, key, path] = m; - if (folders.has(key as NuGetFolder)) { - result[key] = path; + let match: RegExpExecArray | null; + while ((match = regex.exec(stdout)) !== null) { + const key = match.groups!.key; + if ((key as NuGetFolderName) in result) { + result[key] = match.groups!.path; } } @@ -79,18 +76,23 @@ export function isCacheFeatureAvailable(): boolean { } if (isGhes()) { - throw new Error( + core.warning( 'Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.' ); + return false; } core.warning( 'The runner was not able to contact the cache service. Caching will be skipped' ); return false; - - function isGhes(): boolean { - const url = process.env['GITHUB_SERVER_URL'] || 'https://github.com'; - return new URL(url).hostname.toUpperCase() !== 'GITHUB.COM'; - } +} + +/** + * Returns this action runs on GitHub Enterprise Server or not. + * (port from https://github.com/actions/toolkit/blob/457303960f03375db6f033e214b9f90d79c3fe5c/packages/cache/src/internal/cacheUtils.ts#L134) + */ +function isGhes(): boolean { + const url = process.env['GITHUB_SERVER_URL'] || 'https://github.com'; + return new URL(url).hostname.toUpperCase() !== 'GITHUB.COM'; }