Merge pull request #396 from crazy-max/github-isghes

github: isGHES func
This commit is contained in:
CrazyMax 2024-07-02 13:29:06 +02:00 committed by GitHub
commit 846cac2aa2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 2 deletions

View File

@ -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');

View File

@ -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<UploadArtifactResponse> {
if (isGhes()) {
if (GitHub.isGHES) {
throw new Error('@actions/artifact v2.0.0+ is currently not supported on GHES.');
}