From 8606abc738c60a07c5ad11ef8f9f4208f274c66b Mon Sep 17 00:00:00 2001 From: Jongwoo Han Date: Mon, 12 Dec 2022 20:47:46 +0900 Subject: [PATCH 1/2] refactor: Use early return pattern Signed-off-by: jongwooo --- dist/setup/index.js | 16 +++++++--------- src/utils.ts | 23 +++++++++++------------ 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/dist/setup/index.js b/dist/setup/index.js index 6cf4871db..1901d1a1e 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -67057,16 +67057,14 @@ function isGhes() { } exports.isGhes = isGhes; function isCacheFeatureAvailable() { - if (!cache.isFeatureAvailable()) { - if (isGhes()) { - throw new Error('Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the 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; + if (cache.isFeatureAvailable()) { + return true; } - return true; + if (isGhes()) { + throw new Error('Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the 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; function logWarning(message) { diff --git a/src/utils.ts b/src/utils.ts index 37059cb66..bc2e87989 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -105,21 +105,20 @@ export function isGhes(): boolean { } export function isCacheFeatureAvailable(): boolean { - if (!cache.isFeatureAvailable()) { - if (isGhes()) { - throw new Error( - 'Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the 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( + 'Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the 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; } export function logWarning(message: string): void { From 13964fee2c5a16462bf431af042d82e71d1d040e Mon Sep 17 00:00:00 2001 From: jongwooo Date: Thu, 15 Dec 2022 23:03:42 +0900 Subject: [PATCH 2/2] fix: Replace throw with warn Signed-off-by: jongwooo --- __tests__/utils.test.ts | 11 +++++------ dist/setup/index.js | 3 ++- src/utils.ts | 3 ++- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index 04ec7c51f..30fc61cb4 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -42,14 +42,13 @@ describe('validateVersion', () => { describe('isCacheFeatureAvailable', () => { it('isCacheFeatureAvailable disabled on GHES', () => { jest.spyOn(cache, 'isFeatureAvailable').mockImplementation(() => false); + const infoMock = jest.spyOn(core, 'warning'); + const message = + 'Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the Actions cache service is enabled or not.'; try { process.env['GITHUB_SERVER_URL'] = 'http://example.com'; - isCacheFeatureAvailable(); - } catch (error) { - expect(error).toHaveProperty( - 'message', - 'Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the Actions cache service is enabled or not.' - ); + expect(isCacheFeatureAvailable()).toBeFalsy(); + expect(infoMock).toHaveBeenCalledWith(message); } finally { delete process.env['GITHUB_SERVER_URL']; } diff --git a/dist/setup/index.js b/dist/setup/index.js index 1901d1a1e..2d155710c 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -67061,7 +67061,8 @@ function isCacheFeatureAvailable() { return true; } if (isGhes()) { - throw new Error('Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the Actions cache service is enabled or not.'); + core.warning('Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the 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/utils.ts b/src/utils.ts index bc2e87989..fe32eda94 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -110,9 +110,10 @@ export function isCacheFeatureAvailable(): boolean { } if (isGhes()) { - throw new Error( + core.warning( 'Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the Actions cache service is enabled or not.' ); + return false; } core.warning(