diff --git a/__tests__/github.test.ts b/__tests__/github.test.ts index 54c211a..f341844 100644 --- a/__tests__/github.test.ts +++ b/__tests__/github.test.ts @@ -85,6 +85,28 @@ describe('apiURL', () => { }); }); +describe('isGHES', () => { + afterEach(() => { + process.env.GITHUB_SERVER_URL = ''; + }); + it('should return false when the request domain is github.com', () => { + process.env.GITHUB_SERVER_URL = 'https://github.com'; + expect(GitHub.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(GitHub.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(GitHub.isGHES).toBe(false); + }); + it('should return true when the request domain is specific to an enterprise', () => { + process.env.GITHUB_SERVER_URL = 'https://my-enterprise.github.com'; + expect(GitHub.isGHES).toBe(true); + }); +}); + describe('repository', () => { it('returns GitHub repository', async () => { expect(GitHub.repository).toEqual('docker/actions-toolkit'); diff --git a/src/github.ts b/src/github.ts index 446856d..d305ddc 100644 --- a/src/github.ts +++ b/src/github.ts @@ -22,7 +22,6 @@ import os from 'os'; import path from 'path'; import {CreateArtifactRequest, FinalizeArtifactRequest, StringValue} from '@actions/artifact/lib/generated'; import {internalArtifactTwirpClient} from '@actions/artifact/lib/internal/shared/artifact-twirp-client'; -import {isGhes} from '@actions/artifact/lib/internal/shared/config'; import {getBackendIdsFromToken} from '@actions/artifact/lib/internal/shared/util'; import {getExpiration} from '@actions/artifact/lib/internal/upload/retention'; import {InvalidResponseError, NetworkError} from '@actions/artifact'; @@ -67,6 +66,14 @@ export class GitHub { return process.env.GITHUB_API_URL || 'https://api.github.com'; } + static get isGHES(): boolean { + const serverURL = new URL(GitHub.serverURL); + const hostname = serverURL.hostname.trimEnd().toUpperCase(); + const isGitHubHost = hostname === 'GITHUB.COM'; + const isGHESHost = hostname.endsWith('.GHE.COM') || hostname.endsWith('.GHE.LOCALHOST'); + return !isGitHubHost && !isGHESHost; + } + static get repository(): string { return `${github.context.repo.owner}/${github.context.repo.repo}`; } @@ -124,7 +131,7 @@ export class GitHub { } public static async uploadArtifact(opts: UploadArtifactOpts): Promise { - if (isGhes()) { + if (GitHub.isGHES) { throw new Error('@actions/artifact v2.0.0+ is currently not supported on GHES.'); }