Skip to content

Commit

Permalink
Merge pull request #396 from crazy-max/github-isghes
Browse files Browse the repository at this point in the history
github: isGHES func
  • Loading branch information
crazy-max committed Jul 2, 2024
2 parents 931b62d + 83d63d1 commit 846cac2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
22 changes: 22 additions & 0 deletions __tests__/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
11 changes: 9 additions & 2 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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}`;
}
Expand Down Expand Up @@ -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.');
}

Expand Down

0 comments on commit 846cac2

Please sign in to comment.