Skip to content

Commit 9419772

Browse files
authored
Revise isGhes logic (#511)
* Revise `isGhes` logic * ran `npm run format` * added unit test * tweaked unit test * ran `npm run format`
1 parent d60b41a commit 9419772

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

__tests__/cache-utils.test.ts

+38
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,41 @@ describe('isCacheFeatureAvailable', () => {
209209
expect(warningSpy).toHaveBeenCalledWith(warningMessage);
210210
});
211211
});
212+
213+
describe('isGhes', () => {
214+
const pristineEnv = process.env;
215+
216+
beforeEach(() => {
217+
jest.resetModules();
218+
process.env = {...pristineEnv};
219+
});
220+
221+
afterAll(() => {
222+
process.env = pristineEnv;
223+
});
224+
225+
it('returns false when the GITHUB_SERVER_URL environment variable is not defined', async () => {
226+
delete process.env['GITHUB_SERVER_URL'];
227+
expect(cacheUtils.isGhes()).toBeFalsy();
228+
});
229+
230+
it('returns false when the GITHUB_SERVER_URL environment variable is set to github.com', async () => {
231+
process.env['GITHUB_SERVER_URL'] = 'https://github.com';
232+
expect(cacheUtils.isGhes()).toBeFalsy();
233+
});
234+
235+
it('returns false when the GITHUB_SERVER_URL environment variable is set to a GitHub Enterprise Cloud-style URL', async () => {
236+
process.env['GITHUB_SERVER_URL'] = 'https://contoso.ghe.com';
237+
expect(cacheUtils.isGhes()).toBeFalsy();
238+
});
239+
240+
it('returns false when the GITHUB_SERVER_URL environment variable has a .localhost suffix', async () => {
241+
process.env['GITHUB_SERVER_URL'] = 'https://mock-github.localhost';
242+
expect(cacheUtils.isGhes()).toBeFalsy();
243+
});
244+
245+
it('returns true when the GITHUB_SERVER_URL environment variable is set to some other URL', async () => {
246+
process.env['GITHUB_SERVER_URL'] = 'https://src.onpremise.fabrikam.com';
247+
expect(cacheUtils.isGhes()).toBeTruthy();
248+
});
249+
});

dist/cache-save/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -81175,7 +81175,11 @@ const getCacheDirectoryPath = (packageManagerInfo) => __awaiter(void 0, void 0,
8117581175
exports.getCacheDirectoryPath = getCacheDirectoryPath;
8117681176
function isGhes() {
8117781177
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
81178-
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
81178+
const hostname = ghUrl.hostname.trimEnd().toUpperCase();
81179+
const isGitHubHost = hostname === 'GITHUB.COM';
81180+
const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM');
81181+
const isLocalHost = hostname.endsWith('.LOCALHOST');
81182+
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost;
8117981183
}
8118081184
exports.isGhes = isGhes;
8118181185
function isCacheFeatureAvailable() {

dist/setup/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -88165,7 +88165,11 @@ const getCacheDirectoryPath = (packageManagerInfo) => __awaiter(void 0, void 0,
8816588165
exports.getCacheDirectoryPath = getCacheDirectoryPath;
8816688166
function isGhes() {
8816788167
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
88168-
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
88168+
const hostname = ghUrl.hostname.trimEnd().toUpperCase();
88169+
const isGitHubHost = hostname === 'GITHUB.COM';
88170+
const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM');
88171+
const isLocalHost = hostname.endsWith('.LOCALHOST');
88172+
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost;
8816988173
}
8817088174
exports.isGhes = isGhes;
8817188175
function isCacheFeatureAvailable() {

src/cache-utils.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ export function isGhes(): boolean {
6363
const ghUrl = new URL(
6464
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
6565
);
66-
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
66+
67+
const hostname = ghUrl.hostname.trimEnd().toUpperCase();
68+
const isGitHubHost = hostname === 'GITHUB.COM';
69+
const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM');
70+
const isLocalHost = hostname.endsWith('.LOCALHOST');
71+
72+
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost;
6773
}
6874

6975
export function isCacheFeatureAvailable(): boolean {

0 commit comments

Comments
 (0)