Skip to content

Commit 545223a

Browse files
authored
refactor: Use early return pattern to avoid nested conditions (actions#428)
1 parent 1df8dbe commit 545223a

File tree

4 files changed

+28
-31
lines changed

4 files changed

+28
-31
lines changed

__tests__/util.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@ describe('isVersionSatisfies', () => {
2929
describe('isCacheFeatureAvailable', () => {
3030
it('isCacheFeatureAvailable disabled on GHES', () => {
3131
jest.spyOn(cache, 'isFeatureAvailable').mockImplementation(() => false);
32+
const infoMock = jest.spyOn(core, 'warning');
33+
const message =
34+
'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.';
3235
try {
3336
process.env['GITHUB_SERVER_URL'] = 'http://example.com';
34-
isCacheFeatureAvailable();
35-
} catch (error) {
36-
expect(error).toHaveProperty(
37-
'message',
38-
'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.'
39-
);
37+
expect(isCacheFeatureAvailable()).toBeFalsy();
38+
expect(infoMock).toHaveBeenCalledWith(message);
4039
} finally {
4140
delete process.env['GITHUB_SERVER_URL'];
4241
}

dist/cleanup/index.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68708,16 +68708,15 @@ function isGhes() {
6870868708
}
6870968709
exports.isGhes = isGhes;
6871068710
function isCacheFeatureAvailable() {
68711-
if (!cache.isFeatureAvailable()) {
68712-
if (isGhes()) {
68713-
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.');
68714-
}
68715-
else {
68716-
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
68717-
}
68711+
if (cache.isFeatureAvailable()) {
68712+
return true;
68713+
}
68714+
if (isGhes()) {
68715+
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.');
6871868716
return false;
6871968717
}
68720-
return true;
68718+
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
68719+
return false;
6872168720
}
6872268721
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
6872368722
function getVersionFromFileContent(content, distributionName) {

dist/setup/index.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105436,16 +105436,15 @@ function isGhes() {
105436105436
}
105437105437
exports.isGhes = isGhes;
105438105438
function isCacheFeatureAvailable() {
105439-
if (!cache.isFeatureAvailable()) {
105440-
if (isGhes()) {
105441-
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.');
105442-
}
105443-
else {
105444-
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
105445-
}
105439+
if (cache.isFeatureAvailable()) {
105440+
return true;
105441+
}
105442+
if (isGhes()) {
105443+
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.');
105446105444
return false;
105447105445
}
105448-
return true;
105446+
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
105447+
return false;
105449105448
}
105450105449
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
105451105450
function getVersionFromFileContent(content, distributionName) {

src/util.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,19 @@ export function isGhes(): boolean {
8585
}
8686

8787
export function isCacheFeatureAvailable(): boolean {
88-
if (!cache.isFeatureAvailable()) {
89-
if (isGhes()) {
90-
throw new Error(
91-
'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.'
92-
);
93-
} else {
94-
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
95-
}
88+
if (cache.isFeatureAvailable()) {
89+
return true;
90+
}
9691

92+
if (isGhes()) {
93+
core.warning(
94+
'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.'
95+
);
9796
return false;
9897
}
9998

100-
return true;
99+
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
100+
return false;
101101
}
102102

103103
export function getVersionFromFileContent(

0 commit comments

Comments
 (0)