From 676975d9aaccb55b3621a6d481497820a9942577 Mon Sep 17 00:00:00 2001 From: Sergey Dolin Date: Fri, 9 Dec 2022 11:41:54 +0100 Subject: [PATCH 1/2] Use early return pattern --- dist/cache-save/index.js | 16 ++++++---------- dist/setup/index.js | 16 ++++++---------- src/cache-utils.ts | 23 ++++++++++------------- 3 files changed, 22 insertions(+), 33 deletions(-) diff --git a/dist/cache-save/index.js b/dist/cache-save/index.js index 876629ef..5f499403 100644 --- a/dist/cache-save/index.js +++ b/dist/cache-save/index.js @@ -61185,16 +61185,12 @@ function isGhes() { } exports.isGhes = isGhes; function isCacheFeatureAvailable() { - if (!cache.isFeatureAvailable()) { - 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.'); - } - else { - core.warning('The runner was not able to contact the cache service. Caching will be skipped'); - } - return false; - } - return true; + if (cache.isFeatureAvailable()) + 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('The runner was not able to contact the cache service. Caching will be skipped'); + return false; } exports.isCacheFeatureAvailable = isCacheFeatureAvailable; diff --git a/dist/setup/index.js b/dist/setup/index.js index b6c3e23f..af122147 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -73139,16 +73139,12 @@ function isGhes() { } exports.isGhes = isGhes; function isCacheFeatureAvailable() { - if (!cache.isFeatureAvailable()) { - 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.'); - } - else { - core.warning('The runner was not able to contact the cache service. Caching will be skipped'); - } - return false; - } - return true; + if (cache.isFeatureAvailable()) + 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('The runner was not able to contact the cache service. Caching will be skipped'); + return false; } exports.isCacheFeatureAvailable = isCacheFeatureAvailable; diff --git a/src/cache-utils.ts b/src/cache-utils.ts index ccd4e987..065d347e 100644 --- a/src/cache-utils.ts +++ b/src/cache-utils.ts @@ -105,19 +105,16 @@ export function isGhes(): boolean { } export function isCacheFeatureAvailable(): boolean { - if (!cache.isFeatureAvailable()) { - 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.' - ); - } else { - core.warning( - 'The runner was not able to contact the cache service. Caching will be skipped' - ); - } + if (cache.isFeatureAvailable()) return true; - return false; - } + 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.' + ); - return true; + core.warning( + 'The runner was not able to contact the cache service. Caching will be skipped' + ); + + return false; } From b28830cbe242f8218618bacd97758c82c162a981 Mon Sep 17 00:00:00 2001 From: Sergey Dolin Date: Fri, 9 Dec 2022 12:05:59 +0100 Subject: [PATCH 2/2] replace throw with warn --- __tests__/cache-utils.test.ts | 3 ++- __tests__/installer.test.ts | 5 +++-- dist/cache-save/index.js | 6 ++++-- dist/setup/index.js | 6 ++++-- src/cache-utils.ts | 6 ++++-- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/__tests__/cache-utils.test.ts b/__tests__/cache-utils.test.ts index d8934eba..9e8d653d 100644 --- a/__tests__/cache-utils.test.ts +++ b/__tests__/cache-utils.test.ts @@ -46,7 +46,8 @@ describe('cache-utils', () => { isFeatureAvailable.mockImplementation(() => false); process.env['GITHUB_SERVER_URL'] = 'https://www.test.com'; - expect(() => isCacheFeatureAvailable()).toThrowError( + expect(isCacheFeatureAvailable()).toBeFalsy(); + expect(warningSpy).toHaveBeenCalledWith( '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.' ); }); diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index 3c3105e2..2a74f78f 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -728,8 +728,9 @@ describe('setup-node', () => { await main.run(); - expect(cnSpy).toHaveBeenCalledWith( - `::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.${osm.EOL}` + expect(warningSpy).toHaveBeenCalledWith( + // `::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.${osm.EOL}` + '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.' ); }); diff --git a/dist/cache-save/index.js b/dist/cache-save/index.js index 5f499403..d099eecb 100644 --- a/dist/cache-save/index.js +++ b/dist/cache-save/index.js @@ -61187,8 +61187,10 @@ exports.isGhes = isGhes; function isCacheFeatureAvailable() { if (cache.isFeatureAvailable()) 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.'); + if (isGhes()) { + 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; } diff --git a/dist/setup/index.js b/dist/setup/index.js index af122147..01a4548b 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -73141,8 +73141,10 @@ exports.isGhes = isGhes; function isCacheFeatureAvailable() { if (cache.isFeatureAvailable()) 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.'); + if (isGhes()) { + 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; } diff --git a/src/cache-utils.ts b/src/cache-utils.ts index 065d347e..5df3e718 100644 --- a/src/cache-utils.ts +++ b/src/cache-utils.ts @@ -107,10 +107,12 @@ export function isGhes(): boolean { export function isCacheFeatureAvailable(): boolean { if (cache.isFeatureAvailable()) return true; - if (isGhes()) - throw new Error( + if (isGhes()) { + 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'