Skip to content

Commit f4c5a11

Browse files
authoredOct 21, 2024··
Revise isGhes logic (#963)
* Revise `isGhes` logic * ran `npm run format` * add unit test * ran `npm run format`
1 parent 19dfb7b commit f4c5a11

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed
 

‎__tests__/utils.test.ts

+39
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
getVersionInputFromPlainFile,
1414
getVersionInputFromTomlFile,
1515
getNextPageUrl,
16+
isGhes,
1617
IS_WINDOWS,
1718
getDownloadFileName
1819
} from '../src/utils';
@@ -195,3 +196,41 @@ describe('getDownloadFileName', () => {
195196
}
196197
});
197198
});
199+
200+
describe('isGhes', () => {
201+
const pristineEnv = process.env;
202+
203+
beforeEach(() => {
204+
jest.resetModules();
205+
process.env = {...pristineEnv};
206+
});
207+
208+
afterAll(() => {
209+
process.env = pristineEnv;
210+
});
211+
212+
it('returns false when the GITHUB_SERVER_URL environment variable is not defined', async () => {
213+
delete process.env['GITHUB_SERVER_URL'];
214+
expect(isGhes()).toBeFalsy();
215+
});
216+
217+
it('returns false when the GITHUB_SERVER_URL environment variable is set to github.com', async () => {
218+
process.env['GITHUB_SERVER_URL'] = 'https://github.com';
219+
expect(isGhes()).toBeFalsy();
220+
});
221+
222+
it('returns false when the GITHUB_SERVER_URL environment variable is set to a GitHub Enterprise Cloud-style URL', async () => {
223+
process.env['GITHUB_SERVER_URL'] = 'https://contoso.ghe.com';
224+
expect(isGhes()).toBeFalsy();
225+
});
226+
227+
it('returns false when the GITHUB_SERVER_URL environment variable has a .localhost suffix', async () => {
228+
process.env['GITHUB_SERVER_URL'] = 'https://mock-github.localhost';
229+
expect(isGhes()).toBeFalsy();
230+
});
231+
232+
it('returns true when the GITHUB_SERVER_URL environment variable is set to some other URL', async () => {
233+
process.env['GITHUB_SERVER_URL'] = 'https://src.onpremise.fabrikam.com';
234+
expect(isGhes()).toBeTruthy();
235+
});
236+
});

‎dist/setup/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -92017,7 +92017,11 @@ function validatePythonVersionFormatForPyPy(version) {
9201792017
exports.validatePythonVersionFormatForPyPy = validatePythonVersionFormatForPyPy;
9201892018
function isGhes() {
9201992019
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
92020-
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
92020+
const hostname = ghUrl.hostname.trimEnd().toUpperCase();
92021+
const isGitHubHost = hostname === 'GITHUB.COM';
92022+
const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM');
92023+
const isLocalHost = hostname.endsWith('.LOCALHOST');
92024+
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost;
9202192025
}
9202292026
exports.isGhes = isGhes;
9202392027
function isCacheFeatureAvailable() {

‎src/utils.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,13 @@ export function isGhes(): boolean {
116116
const ghUrl = new URL(
117117
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
118118
);
119-
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
119+
120+
const hostname = ghUrl.hostname.trimEnd().toUpperCase();
121+
const isGitHubHost = hostname === 'GITHUB.COM';
122+
const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM');
123+
const isLocalHost = hostname.endsWith('.LOCALHOST');
124+
125+
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost;
120126
}
121127

122128
export function isCacheFeatureAvailable(): boolean {

0 commit comments

Comments
 (0)
Please sign in to comment.