From d73025053bdece7c4128177b8679e7de171b3de7 Mon Sep 17 00:00:00 2001 From: John Wesley Walker III <81404201+jww3@users.noreply.github.com> Date: Fri, 18 Oct 2024 13:23:42 +0200 Subject: [PATCH 1/4] Revise `isGhes` logic --- src/utils/actionUtils.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/utils/actionUtils.ts b/src/utils/actionUtils.ts index dc18fa4..5796702 100644 --- a/src/utils/actionUtils.ts +++ b/src/utils/actionUtils.ts @@ -4,10 +4,16 @@ import * as core from "@actions/core"; import { RefKey } from "../constants"; export function isGhes(): boolean { - const ghUrl = new URL( - process.env["GITHUB_SERVER_URL"] || "https://github.com" - ); - return ghUrl.hostname.toUpperCase() !== "GITHUB.COM"; + const ghUrl = new URL( + 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') + + return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost } export function isExactKeyMatch(key: string, cacheKey?: string): boolean { From 25942a73ac3e07f428f6787a5b11c9d84dd7c80d Mon Sep 17 00:00:00 2001 From: John Wesley Walker III <81404201+jww3@users.noreply.github.com> Date: Fri, 18 Oct 2024 13:25:07 +0000 Subject: [PATCH 2/4] ran `npm run build` --- dist/restore-only/index.js | 8 ++++++-- dist/restore/index.js | 8 ++++++-- dist/save-only/index.js | 8 ++++++-- dist/save/index.js | 8 ++++++-- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/dist/restore-only/index.js b/dist/restore-only/index.js index 14e97b1..b7f7a4e 100644 --- a/dist/restore-only/index.js +++ b/dist/restore-only/index.js @@ -59585,8 +59585,12 @@ const cache = __importStar(__nccwpck_require__(7799)); const core = __importStar(__nccwpck_require__(2186)); const constants_1 = __nccwpck_require__(9042); function isGhes() { - const ghUrl = new URL(process.env["GITHUB_SERVER_URL"] || "https://github.com"); - return ghUrl.hostname.toUpperCase() !== "GITHUB.COM"; + const ghUrl = new URL(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'); + return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost; } exports.isGhes = isGhes; function isExactKeyMatch(key, cacheKey) { diff --git a/dist/restore/index.js b/dist/restore/index.js index 29b70b2..70ff2ca 100644 --- a/dist/restore/index.js +++ b/dist/restore/index.js @@ -59585,8 +59585,12 @@ const cache = __importStar(__nccwpck_require__(7799)); const core = __importStar(__nccwpck_require__(2186)); const constants_1 = __nccwpck_require__(9042); function isGhes() { - const ghUrl = new URL(process.env["GITHUB_SERVER_URL"] || "https://github.com"); - return ghUrl.hostname.toUpperCase() !== "GITHUB.COM"; + const ghUrl = new URL(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'); + return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost; } exports.isGhes = isGhes; function isExactKeyMatch(key, cacheKey) { diff --git a/dist/save-only/index.js b/dist/save-only/index.js index f542b2c..04f0fa0 100644 --- a/dist/save-only/index.js +++ b/dist/save-only/index.js @@ -59598,8 +59598,12 @@ const cache = __importStar(__nccwpck_require__(7799)); const core = __importStar(__nccwpck_require__(2186)); const constants_1 = __nccwpck_require__(9042); function isGhes() { - const ghUrl = new URL(process.env["GITHUB_SERVER_URL"] || "https://github.com"); - return ghUrl.hostname.toUpperCase() !== "GITHUB.COM"; + const ghUrl = new URL(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'); + return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost; } exports.isGhes = isGhes; function isExactKeyMatch(key, cacheKey) { diff --git a/dist/save/index.js b/dist/save/index.js index f137655..3b9658a 100644 --- a/dist/save/index.js +++ b/dist/save/index.js @@ -59598,8 +59598,12 @@ const cache = __importStar(__nccwpck_require__(7799)); const core = __importStar(__nccwpck_require__(2186)); const constants_1 = __nccwpck_require__(9042); function isGhes() { - const ghUrl = new URL(process.env["GITHUB_SERVER_URL"] || "https://github.com"); - return ghUrl.hostname.toUpperCase() !== "GITHUB.COM"; + const ghUrl = new URL(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'); + return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost; } exports.isGhes = isGhes; function isExactKeyMatch(key, cacheKey) { From 71d826cc3307623babdbd7bef45bf9d6f1207d39 Mon Sep 17 00:00:00 2001 From: John Wesley Walker III <81404201+jww3@users.noreply.github.com> Date: Fri, 18 Oct 2024 13:52:16 +0000 Subject: [PATCH 3/4] appease the linter --- dist/restore-only/index.js | 8 ++++---- dist/restore/index.js | 8 ++++---- dist/save-only/index.js | 8 ++++---- dist/save/index.js | 8 ++++---- src/utils/actionUtils.ts | 16 ++++++++-------- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/dist/restore-only/index.js b/dist/restore-only/index.js index b7f7a4e..828dbd1 100644 --- a/dist/restore-only/index.js +++ b/dist/restore-only/index.js @@ -59585,11 +59585,11 @@ const cache = __importStar(__nccwpck_require__(7799)); const core = __importStar(__nccwpck_require__(2186)); const constants_1 = __nccwpck_require__(9042); function isGhes() { - const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com'); + const ghUrl = new URL(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 isGitHubHost = hostname === "GITHUB.COM"; + const isGitHubEnterpriseCloudHost = hostname.endsWith(".GHE.COM"); + const isLocalHost = hostname.endsWith(".LOCALHOST"); return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost; } exports.isGhes = isGhes; diff --git a/dist/restore/index.js b/dist/restore/index.js index 70ff2ca..dbf9432 100644 --- a/dist/restore/index.js +++ b/dist/restore/index.js @@ -59585,11 +59585,11 @@ const cache = __importStar(__nccwpck_require__(7799)); const core = __importStar(__nccwpck_require__(2186)); const constants_1 = __nccwpck_require__(9042); function isGhes() { - const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com'); + const ghUrl = new URL(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 isGitHubHost = hostname === "GITHUB.COM"; + const isGitHubEnterpriseCloudHost = hostname.endsWith(".GHE.COM"); + const isLocalHost = hostname.endsWith(".LOCALHOST"); return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost; } exports.isGhes = isGhes; diff --git a/dist/save-only/index.js b/dist/save-only/index.js index 04f0fa0..434fac7 100644 --- a/dist/save-only/index.js +++ b/dist/save-only/index.js @@ -59598,11 +59598,11 @@ const cache = __importStar(__nccwpck_require__(7799)); const core = __importStar(__nccwpck_require__(2186)); const constants_1 = __nccwpck_require__(9042); function isGhes() { - const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com'); + const ghUrl = new URL(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 isGitHubHost = hostname === "GITHUB.COM"; + const isGitHubEnterpriseCloudHost = hostname.endsWith(".GHE.COM"); + const isLocalHost = hostname.endsWith(".LOCALHOST"); return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost; } exports.isGhes = isGhes; diff --git a/dist/save/index.js b/dist/save/index.js index 3b9658a..b8828f3 100644 --- a/dist/save/index.js +++ b/dist/save/index.js @@ -59598,11 +59598,11 @@ const cache = __importStar(__nccwpck_require__(7799)); const core = __importStar(__nccwpck_require__(2186)); const constants_1 = __nccwpck_require__(9042); function isGhes() { - const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com'); + const ghUrl = new URL(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 isGitHubHost = hostname === "GITHUB.COM"; + const isGitHubEnterpriseCloudHost = hostname.endsWith(".GHE.COM"); + const isLocalHost = hostname.endsWith(".LOCALHOST"); return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost; } exports.isGhes = isGhes; diff --git a/src/utils/actionUtils.ts b/src/utils/actionUtils.ts index 5796702..260d4fd 100644 --- a/src/utils/actionUtils.ts +++ b/src/utils/actionUtils.ts @@ -4,16 +4,16 @@ import * as core from "@actions/core"; import { RefKey } from "../constants"; export function isGhes(): boolean { - const ghUrl = new URL( - process.env['GITHUB_SERVER_URL'] || 'https://github.com' - ); + const ghUrl = new URL( + 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 isExactKeyMatch(key: string, cacheKey?: string): boolean { From e7e2547a8838154c29f0db50c6dc7866b71a8ac2 Mon Sep 17 00:00:00 2001 From: John Wesley Walker III <81404201+jww3@users.noreply.github.com> Date: Fri, 18 Oct 2024 17:22:08 +0000 Subject: [PATCH 4/4] added unit tests --- __tests__/actionUtils.test.ts | 36 ++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/__tests__/actionUtils.test.ts b/__tests__/actionUtils.test.ts index f98a76e..c2e6823 100644 --- a/__tests__/actionUtils.test.ts +++ b/__tests__/actionUtils.test.ts @@ -8,17 +8,26 @@ import * as testUtils from "../src/utils/testUtils"; jest.mock("@actions/core"); jest.mock("@actions/cache"); +let pristineEnv: NodeJS.ProcessEnv; + beforeAll(() => { + pristineEnv = process.env; jest.spyOn(core, "getInput").mockImplementation((name, options) => { return jest.requireActual("@actions/core").getInput(name, options); }); }); -afterEach(() => { +beforeEach(() => { + jest.resetModules(); + process.env = pristineEnv; delete process.env[Events.Key]; delete process.env[RefKey]; }); +afterAll(() => { + process.env = pristineEnv; +}); + test("isGhes returns true if server url is not github.com", () => { try { process.env["GITHUB_SERVER_URL"] = "http://example.com"; @@ -231,3 +240,28 @@ test("isCacheFeatureAvailable for ac disabled on dotcom", () => { delete process.env["GITHUB_SERVER_URL"]; } }); + +test("isGhes returns false when the GITHUB_SERVER_URL environment variable is not defined", async () => { + delete process.env["GITHUB_SERVER_URL"]; + expect(actionUtils.isGhes()).toBeFalsy(); +}); + +test("isGhes returns false when the GITHUB_SERVER_URL environment variable is set to github.com", async () => { + process.env["GITHUB_SERVER_URL"] = "https://github.com"; + expect(actionUtils.isGhes()).toBeFalsy(); +}); + +test("isGhes 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(actionUtils.isGhes()).toBeFalsy(); +}); + +test("isGhes returns false when the GITHUB_SERVER_URL environment variable has a .localhost suffix", async () => { + process.env["GITHUB_SERVER_URL"] = "https://mock-github.localhost"; + expect(actionUtils.isGhes()).toBeFalsy(); +}); + +test("isGhes 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(actionUtils.isGhes()).toBeTruthy(); +});