Skip to content

Commit 206e984

Browse files
authored
refactor: Use early return pattern to avoid nested conditions (#566)
* 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]>
1 parent 2c3dd9e commit 206e984

File tree

3 files changed

+23
-25
lines changed

3 files changed

+23
-25
lines changed

__tests__/utils.test.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@ describe('validateVersion', () => {
4242
describe('isCacheFeatureAvailable', () => {
4343
it('isCacheFeatureAvailable disabled on GHES', () => {
4444
jest.spyOn(cache, 'isFeatureAvailable').mockImplementation(() => false);
45+
const infoMock = jest.spyOn(core, 'warning');
46+
const message =
47+
'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.';
4548
try {
4649
process.env['GITHUB_SERVER_URL'] = 'http://example.com';
47-
isCacheFeatureAvailable();
48-
} catch (error) {
49-
expect(error).toHaveProperty(
50-
'message',
51-
'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.'
52-
);
50+
expect(isCacheFeatureAvailable()).toBeFalsy();
51+
expect(infoMock).toHaveBeenCalledWith(message);
5352
} finally {
5453
delete process.env['GITHUB_SERVER_URL'];
5554
}

dist/setup/index.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -67057,16 +67057,15 @@ function isGhes() {
6705767057
}
6705867058
exports.isGhes = isGhes;
6705967059
function isCacheFeatureAvailable() {
67060-
if (!cache.isFeatureAvailable()) {
67061-
if (isGhes()) {
67062-
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.');
67063-
}
67064-
else {
67065-
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
67066-
}
67060+
if (cache.isFeatureAvailable()) {
67061+
return true;
67062+
}
67063+
if (isGhes()) {
67064+
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.');
6706767065
return false;
6706867066
}
67069-
return true;
67067+
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
67068+
return false;
6707067069
}
6707167070
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
6707267071
function logWarning(message) {

src/utils.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,21 @@ export function isGhes(): boolean {
105105
}
106106

107107
export function isCacheFeatureAvailable(): boolean {
108-
if (!cache.isFeatureAvailable()) {
109-
if (isGhes()) {
110-
throw new Error(
111-
'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.'
112-
);
113-
} else {
114-
core.warning(
115-
'The runner was not able to contact the cache service. Caching will be skipped'
116-
);
117-
}
108+
if (cache.isFeatureAvailable()) {
109+
return true;
110+
}
118111

112+
if (isGhes()) {
113+
core.warning(
114+
'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.'
115+
);
119116
return false;
120117
}
121118

122-
return true;
119+
core.warning(
120+
'The runner was not able to contact the cache service. Caching will be skipped'
121+
);
122+
return false;
123123
}
124124

125125
export function logWarning(message: string): void {

0 commit comments

Comments
 (0)