Skip to content

Commit

Permalink
refactor: Use early return pattern to avoid nested conditions (#566)
Browse files Browse the repository at this point in the history
* refactor: Use early return pattern

Signed-off-by: jongwooo <[email protected]>

* fix: Replace throw with warn

Signed-off-by: jongwooo <[email protected]>

Signed-off-by: jongwooo <[email protected]>
  • Loading branch information
jongwooo authored Dec 19, 2022
1 parent 2c3dd9e commit 206e984
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
11 changes: 5 additions & 6 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
Expand Down
15 changes: 7 additions & 8 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67057,16 +67057,15 @@ 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');
}
if (cache.isFeatureAvailable()) {
return true;
}
if (isGhes()) {
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;
}
return true;
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
return false;
}
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
function logWarning(message) {
Expand Down
22 changes: 11 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,21 @@ 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;
}

if (isGhes()) {
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;
}

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 {
Expand Down

0 comments on commit 206e984

Please sign in to comment.