From f4c5a1183d69690d31b6304b7af403a5b56a88d6 Mon Sep 17 00:00:00 2001 From: John Wesley Walker III <81404201+jww3@users.noreply.github.com> Date: Mon, 21 Oct 2024 18:42:17 +0200 Subject: [PATCH] Revise `isGhes` logic (#963) * Revise `isGhes` logic * ran `npm run format` * add unit test * ran `npm run format` --- __tests__/utils.test.ts | 39 +++++++++++++++++++++++++++++++++++++++ dist/setup/index.js | 6 +++++- src/utils.ts | 8 +++++++- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index c2c6bca..eac39ab 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -13,6 +13,7 @@ import { getVersionInputFromPlainFile, getVersionInputFromTomlFile, getNextPageUrl, + isGhes, IS_WINDOWS, getDownloadFileName } from '../src/utils'; @@ -195,3 +196,41 @@ describe('getDownloadFileName', () => { } }); }); + +describe('isGhes', () => { + const pristineEnv = process.env; + + beforeEach(() => { + jest.resetModules(); + process.env = {...pristineEnv}; + }); + + afterAll(() => { + process.env = pristineEnv; + }); + + it('returns false when the GITHUB_SERVER_URL environment variable is not defined', async () => { + delete process.env['GITHUB_SERVER_URL']; + expect(isGhes()).toBeFalsy(); + }); + + it('returns false when the GITHUB_SERVER_URL environment variable is set to github.com', async () => { + process.env['GITHUB_SERVER_URL'] = 'https://github.com'; + expect(isGhes()).toBeFalsy(); + }); + + it('returns false when the GITHUB_SERVER_URL environment variable is set to a GitHub Enterprise Cloud-style URL', async () => { + process.env['GITHUB_SERVER_URL'] = 'https://contoso.ghe.com'; + expect(isGhes()).toBeFalsy(); + }); + + it('returns false when the GITHUB_SERVER_URL environment variable has a .localhost suffix', async () => { + process.env['GITHUB_SERVER_URL'] = 'https://mock-github.localhost'; + expect(isGhes()).toBeFalsy(); + }); + + it('returns true when the GITHUB_SERVER_URL environment variable is set to some other URL', async () => { + process.env['GITHUB_SERVER_URL'] = 'https://src.onpremise.fabrikam.com'; + expect(isGhes()).toBeTruthy(); + }); +}); diff --git a/dist/setup/index.js b/dist/setup/index.js index f1e3e29..e56f966 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -92017,7 +92017,11 @@ function validatePythonVersionFormatForPyPy(version) { exports.validatePythonVersionFormatForPyPy = validatePythonVersionFormatForPyPy; function isGhes() { const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com'); - return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM'; + const hostname = ghUrl.hostname.trimEnd().toUpperCase(); + const isGitHubHost = hostname === 'GITHUB.COM'; + const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM'); + const isLocalHost = hostname.endsWith('.LOCALHOST'); + return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost; } exports.isGhes = isGhes; function isCacheFeatureAvailable() { diff --git a/src/utils.ts b/src/utils.ts index f3aa1ef..a6dab63 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -116,7 +116,13 @@ export function isGhes(): boolean { const ghUrl = new URL( process.env['GITHUB_SERVER_URL'] || 'https://github.com' ); - return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM'; + + const hostname = ghUrl.hostname.trimEnd().toUpperCase(); + const isGitHubHost = hostname === 'GITHUB.COM'; + const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM'); + const isLocalHost = hostname.endsWith('.LOCALHOST'); + + return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost; } export function isCacheFeatureAvailable(): boolean {