From 3b02a6fdc5624c115b50a5e41d9f596e4a7a3f60 Mon Sep 17 00:00:00 2001 From: eggyhead <28715808+eggyhead@users.noreply.github.com> Date: Tue, 30 Jan 2024 21:55:50 +0000 Subject: [PATCH 1/2] updating alowed hosts in isGhes check updating alowed hosts in artifact ghes check using dot prepend ghe host --- packages/artifact/__tests__/config.test.ts | 29 +++++++++++++++++++ .../artifact/src/internal/shared/config.ts | 7 ++++- packages/cache/__tests__/cacheUtils.test.ts | 24 +++++++++++++++ packages/cache/src/internal/cacheUtils.ts | 7 ++++- 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 packages/artifact/__tests__/config.test.ts diff --git a/packages/artifact/__tests__/config.test.ts b/packages/artifact/__tests__/config.test.ts new file mode 100644 index 0000000000..d1d860493a --- /dev/null +++ b/packages/artifact/__tests__/config.test.ts @@ -0,0 +1,29 @@ +import * as config from '../src/internal/shared/config' + + +beforeEach(() => { + jest.resetModules() + }); + + +describe('isGhes', () => { + it('should return false when the request domain is github.com', () => { + process.env.GITHUB_SERVER_URL = 'https://github.com' + expect(config.isGhes()).toBe(false) + }) + + it('should return false when the request domain ends with ghe.com', () => { + process.env.GITHUB_SERVER_URL = 'https://my.domain.ghe.com' + expect(config.isGhes()).toBe(false) + }) + + it('should return false when the request domain ends with ghe.localhost', () => { + process.env.GITHUB_SERVER_URL = 'https://my.domain.ghe.localhost' + expect(config.isGhes()).toBe(false) + }) + + it('should return false when the request domain is specific to an enterprise', () => { + process.env.GITHUB_SERVER_URL = 'https://my-enterprise.github.com' + expect(config.isGhes()).toBe(true) + }) +}) \ No newline at end of file diff --git a/packages/artifact/src/internal/shared/config.ts b/packages/artifact/src/internal/shared/config.ts index a5631bfc70..fc39a1c970 100644 --- a/packages/artifact/src/internal/shared/config.ts +++ b/packages/artifact/src/internal/shared/config.ts @@ -27,7 +27,12 @@ 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 isGheHost = (hostname.endsWith('.GHE.COM') || hostname.endsWith('.GHE.LOCALHOST')) + + return !isGitHubHost && !isGheHost } export function getGitHubWorkspaceDir(): string { diff --git a/packages/cache/__tests__/cacheUtils.test.ts b/packages/cache/__tests__/cacheUtils.test.ts index 25124b469c..9a4166cbca 100644 --- a/packages/cache/__tests__/cacheUtils.test.ts +++ b/packages/cache/__tests__/cacheUtils.test.ts @@ -2,6 +2,10 @@ import {promises as fs} from 'fs' import * as path from 'path' import * as cacheUtils from '../src/internal/cacheUtils' +beforeEach(() => { + jest.resetModules() +}); + test('getArchiveFileSizeInBytes returns file size', () => { const filePath = path.join(__dirname, '__fixtures__', 'helloWorld.txt') @@ -38,3 +42,23 @@ test('resolvePaths works on github workspace directory', async () => { const paths = await cacheUtils.resolvePaths([workspace]) expect(paths.length).toBeGreaterThan(0) }) + +test('isGhes returns false for github.com', async () => { + process.env.GITHUB_SERVER_URL = 'https://github.com' + expect(cacheUtils.isGhes()).toBe(false) +}) + +test('isGhes returns false for ghe.com', async () => { + process.env.GITHUB_SERVER_URL = 'https://somedomain.ghe.com' + expect(cacheUtils.isGhes()).toBe(false) +}) + +test('isGhes returns true for enterprise URL', async () => { + process.env.GITHUB_SERVER_URL = 'https://my-enterprise.github.com' + expect(cacheUtils.isGhes()).toBe(true) +}) + +test('isGhes returns false for ghe.localhost', () => { + process.env.GITHUB_SERVER_URL = 'https://my.domain.ghe.localhost' + expect(cacheUtils.isGhes()).toBe(false) +}) \ No newline at end of file diff --git a/packages/cache/src/internal/cacheUtils.ts b/packages/cache/src/internal/cacheUtils.ts index 650653adf0..8bc3ce235d 100644 --- a/packages/cache/src/internal/cacheUtils.ts +++ b/packages/cache/src/internal/cacheUtils.ts @@ -135,5 +135,10 @@ 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 isGheHost = (hostname.endsWith('.GHE.COM') || hostname.endsWith('.GHE.LOCALHOST')) + + return !isGitHubHost && !isGheHost } From d134334a38ef2fe178400557e6ff979534e32ebc Mon Sep 17 00:00:00 2001 From: eggyhead <28715808+eggyhead@users.noreply.github.com> Date: Wed, 31 Jan 2024 16:51:04 +0000 Subject: [PATCH 2/2] lint fixes --- packages/artifact/__tests__/config.test.ts | 40 +++++++++---------- .../artifact/src/internal/shared/config.ts | 5 ++- packages/cache/__tests__/cacheUtils.test.ts | 6 +-- packages/cache/src/internal/cacheUtils.ts | 5 ++- 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/packages/artifact/__tests__/config.test.ts b/packages/artifact/__tests__/config.test.ts index d1d860493a..5afed94dad 100644 --- a/packages/artifact/__tests__/config.test.ts +++ b/packages/artifact/__tests__/config.test.ts @@ -1,29 +1,27 @@ import * as config from '../src/internal/shared/config' - beforeEach(() => { - jest.resetModules() - }); - + jest.resetModules() +}) describe('isGhes', () => { - it('should return false when the request domain is github.com', () => { - process.env.GITHUB_SERVER_URL = 'https://github.com' - expect(config.isGhes()).toBe(false) - }) + it('should return false when the request domain is github.com', () => { + process.env.GITHUB_SERVER_URL = 'https://github.com' + expect(config.isGhes()).toBe(false) + }) - it('should return false when the request domain ends with ghe.com', () => { - process.env.GITHUB_SERVER_URL = 'https://my.domain.ghe.com' - expect(config.isGhes()).toBe(false) - }) + it('should return false when the request domain ends with ghe.com', () => { + process.env.GITHUB_SERVER_URL = 'https://my.domain.ghe.com' + expect(config.isGhes()).toBe(false) + }) - it('should return false when the request domain ends with ghe.localhost', () => { - process.env.GITHUB_SERVER_URL = 'https://my.domain.ghe.localhost' - expect(config.isGhes()).toBe(false) - }) + it('should return false when the request domain ends with ghe.localhost', () => { + process.env.GITHUB_SERVER_URL = 'https://my.domain.ghe.localhost' + expect(config.isGhes()).toBe(false) + }) - it('should return false when the request domain is specific to an enterprise', () => { - process.env.GITHUB_SERVER_URL = 'https://my-enterprise.github.com' - expect(config.isGhes()).toBe(true) - }) -}) \ No newline at end of file + it('should return false when the request domain is specific to an enterprise', () => { + process.env.GITHUB_SERVER_URL = 'https://my-enterprise.github.com' + expect(config.isGhes()).toBe(true) + }) +}) diff --git a/packages/artifact/src/internal/shared/config.ts b/packages/artifact/src/internal/shared/config.ts index fc39a1c970..089fae14df 100644 --- a/packages/artifact/src/internal/shared/config.ts +++ b/packages/artifact/src/internal/shared/config.ts @@ -29,8 +29,9 @@ export function isGhes(): boolean { ) const hostname = ghUrl.hostname.trimEnd().toUpperCase() - const isGitHubHost = (hostname == 'GITHUB.COM') - const isGheHost = (hostname.endsWith('.GHE.COM') || hostname.endsWith('.GHE.LOCALHOST')) + const isGitHubHost = hostname === 'GITHUB.COM' + const isGheHost = + hostname.endsWith('.GHE.COM') || hostname.endsWith('.GHE.LOCALHOST') return !isGitHubHost && !isGheHost } diff --git a/packages/cache/__tests__/cacheUtils.test.ts b/packages/cache/__tests__/cacheUtils.test.ts index 9a4166cbca..4388026ae2 100644 --- a/packages/cache/__tests__/cacheUtils.test.ts +++ b/packages/cache/__tests__/cacheUtils.test.ts @@ -3,8 +3,8 @@ import * as path from 'path' import * as cacheUtils from '../src/internal/cacheUtils' beforeEach(() => { - jest.resetModules() -}); + jest.resetModules() +}) test('getArchiveFileSizeInBytes returns file size', () => { const filePath = path.join(__dirname, '__fixtures__', 'helloWorld.txt') @@ -61,4 +61,4 @@ test('isGhes returns true for enterprise URL', async () => { test('isGhes returns false for ghe.localhost', () => { process.env.GITHUB_SERVER_URL = 'https://my.domain.ghe.localhost' expect(cacheUtils.isGhes()).toBe(false) -}) \ No newline at end of file +}) diff --git a/packages/cache/src/internal/cacheUtils.ts b/packages/cache/src/internal/cacheUtils.ts index 8bc3ce235d..91bae9a8db 100644 --- a/packages/cache/src/internal/cacheUtils.ts +++ b/packages/cache/src/internal/cacheUtils.ts @@ -137,8 +137,9 @@ export function isGhes(): boolean { ) const hostname = ghUrl.hostname.trimEnd().toUpperCase() - const isGitHubHost = (hostname == 'GITHUB.COM') - const isGheHost = (hostname.endsWith('.GHE.COM') || hostname.endsWith('.GHE.LOCALHOST')) + const isGitHubHost = hostname === 'GITHUB.COM' + const isGheHost = + hostname.endsWith('.GHE.COM') || hostname.endsWith('.GHE.LOCALHOST') return !isGitHubHost && !isGheHost }