From 31ee4493018c6d5189acad47ac8659baa441dac9 Mon Sep 17 00:00:00 2001 From: John Wesley Walker III <81404201+jww3@users.noreply.github.com> Date: Fri, 18 Oct 2024 13:15:47 +0200 Subject: [PATCH 1/5] Refine `isGhes` logic --- src/util.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/util.ts b/src/util.ts index c0166ef16..a1e51b591 100644 --- a/src/util.ts +++ b/src/util.ts @@ -92,7 +92,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 { From 61e3daf6da3c979ec8d8ab62a1780c32f519d1b4 Mon Sep 17 00:00:00 2001 From: John Wesley Walker III <81404201+jww3@users.noreply.github.com> Date: Fri, 18 Oct 2024 13:21:17 +0000 Subject: [PATCH 2/5] ran `npm run format` --- dist/cleanup/index.js | 6 +++++- dist/setup/index.js | 6 +++++- src/util.ts | 10 +++++----- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/dist/cleanup/index.js b/dist/cleanup/index.js index 5a200909c..6c44aa45a 100644 --- a/dist/cleanup/index.js +++ b/dist/cleanup/index.js @@ -88552,7 +88552,11 @@ function isJobStatusSuccess() { exports.isJobStatusSuccess = isJobStatusSuccess; 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/dist/setup/index.js b/dist/setup/index.js index e96bb1256..9dc658d8f 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -126328,7 +126328,11 @@ function isJobStatusSuccess() { exports.isJobStatusSuccess = isJobStatusSuccess; 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/util.ts b/src/util.ts index a1e51b591..af75aaac1 100644 --- a/src/util.ts +++ b/src/util.ts @@ -93,12 +93,12 @@ export function isGhes(): boolean { process.env['GITHUB_SERVER_URL'] || 'https://github.com' ); - const hostname = ghUrl.hostname.trimEnd().toUpperCase() - const isGitHubHost = hostname === 'GITHUB.COM' - const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM') - const isLocalHost = hostname.endsWith('.LOCALHOST') + 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 + return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost; } export function isCacheFeatureAvailable(): boolean { From 12bd4a4f135ec1fe6f5c91436efa4544646f5016 Mon Sep 17 00:00:00 2001 From: John Wesley Walker III <81404201+jww3@users.noreply.github.com> Date: Fri, 18 Oct 2024 16:53:54 +0000 Subject: [PATCH 3/5] add unit test --- __tests__/util.test.ts | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/__tests__/util.test.ts b/__tests__/util.test.ts index bb7560dca..d60c051cc 100644 --- a/__tests__/util.test.ts +++ b/__tests__/util.test.ts @@ -3,7 +3,8 @@ import * as core from '@actions/core'; import { convertVersionToSemver, isVersionSatisfies, - isCacheFeatureAvailable + isCacheFeatureAvailable, + isGhes } from '../src/util'; jest.mock('@actions/cache'); @@ -80,3 +81,42 @@ describe('convertVersionToSemver', () => { expect(actual).toBe(expected); }); }); + +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 () => { + process.env['GITHUB_SERVER_URL'] = undefined; + 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(); + }); +}); + From 0c1694f6f72bafe4ddb98172ecd5dbb5bcb39470 Mon Sep 17 00:00:00 2001 From: John Wesley Walker III <81404201+jww3@users.noreply.github.com> Date: Fri, 18 Oct 2024 19:01:18 +0200 Subject: [PATCH 4/5] tweaked unit test --- __tests__/util.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/__tests__/util.test.ts b/__tests__/util.test.ts index d60c051cc..db5cd10ad 100644 --- a/__tests__/util.test.ts +++ b/__tests__/util.test.ts @@ -95,7 +95,7 @@ describe('isGhes', () => { }); it('returns false when the GITHUB_SERVER_URL environment variable is not defined', async () => { - process.env['GITHUB_SERVER_URL'] = undefined; + delete process.env['GITHUB_SERVER_URL']; expect(isGhes()).toBeFalsy(); }); @@ -119,4 +119,3 @@ describe('isGhes', () => { expect(isGhes()).toBeTruthy(); }); }); - From ceadfc3789b71301d9c786f293642980088541be Mon Sep 17 00:00:00 2001 From: John Wesley Walker III <81404201+jww3@users.noreply.github.com> Date: Fri, 18 Oct 2024 17:23:29 +0000 Subject: [PATCH 5/5] ran `npm run format` --- __tests__/util.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/__tests__/util.test.ts b/__tests__/util.test.ts index db5cd10ad..9c943c2fa 100644 --- a/__tests__/util.test.ts +++ b/__tests__/util.test.ts @@ -87,12 +87,12 @@ describe('isGhes', () => { beforeEach(() => { jest.resetModules(); - process.env = { ...pristineEnv }; + 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'];