Skip to content

Commit 8df1039

Browse files
authored
Refine isGhes logic (#697)
1 parent 870c199 commit 8df1039

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

__tests__/util.test.ts

+40-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import * as core from '@actions/core';
33
import {
44
convertVersionToSemver,
55
isVersionSatisfies,
6-
isCacheFeatureAvailable
6+
isCacheFeatureAvailable,
7+
isGhes
78
} from '../src/util';
89

910
jest.mock('@actions/cache');
@@ -80,3 +81,41 @@ describe('convertVersionToSemver', () => {
8081
expect(actual).toBe(expected);
8182
});
8283
});
84+
85+
describe('isGhes', () => {
86+
const pristineEnv = process.env;
87+
88+
beforeEach(() => {
89+
jest.resetModules();
90+
process.env = {...pristineEnv};
91+
});
92+
93+
afterAll(() => {
94+
process.env = pristineEnv;
95+
});
96+
97+
it('returns false when the GITHUB_SERVER_URL environment variable is not defined', async () => {
98+
delete process.env['GITHUB_SERVER_URL'];
99+
expect(isGhes()).toBeFalsy();
100+
});
101+
102+
it('returns false when the GITHUB_SERVER_URL environment variable is set to github.com', async () => {
103+
process.env['GITHUB_SERVER_URL'] = 'https://github.com';
104+
expect(isGhes()).toBeFalsy();
105+
});
106+
107+
it('returns false when the GITHUB_SERVER_URL environment variable is set to a GitHub Enterprise Cloud-style URL', async () => {
108+
process.env['GITHUB_SERVER_URL'] = 'https://contoso.ghe.com';
109+
expect(isGhes()).toBeFalsy();
110+
});
111+
112+
it('returns false when the GITHUB_SERVER_URL environment variable has a .localhost suffix', async () => {
113+
process.env['GITHUB_SERVER_URL'] = 'https://mock-github.localhost';
114+
expect(isGhes()).toBeFalsy();
115+
});
116+
117+
it('returns true when the GITHUB_SERVER_URL environment variable is set to some other URL', async () => {
118+
process.env['GITHUB_SERVER_URL'] = 'https://src.onpremise.fabrikam.com';
119+
expect(isGhes()).toBeTruthy();
120+
});
121+
});

dist/cleanup/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -88552,7 +88552,11 @@ function isJobStatusSuccess() {
8855288552
exports.isJobStatusSuccess = isJobStatusSuccess;
8855388553
function isGhes() {
8855488554
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
88555-
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
88555+
const hostname = ghUrl.hostname.trimEnd().toUpperCase();
88556+
const isGitHubHost = hostname === 'GITHUB.COM';
88557+
const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM');
88558+
const isLocalHost = hostname.endsWith('.LOCALHOST');
88559+
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost;
8855688560
}
8855788561
exports.isGhes = isGhes;
8855888562
function isCacheFeatureAvailable() {

dist/setup/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -126328,7 +126328,11 @@ function isJobStatusSuccess() {
126328126328
exports.isJobStatusSuccess = isJobStatusSuccess;
126329126329
function isGhes() {
126330126330
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
126331-
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
126331+
const hostname = ghUrl.hostname.trimEnd().toUpperCase();
126332+
const isGitHubHost = hostname === 'GITHUB.COM';
126333+
const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM');
126334+
const isLocalHost = hostname.endsWith('.LOCALHOST');
126335+
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost;
126332126336
}
126333126337
exports.isGhes = isGhes;
126334126338
function isCacheFeatureAvailable() {

src/util.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,13 @@ export function isGhes(): boolean {
9292
const ghUrl = new URL(
9393
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
9494
);
95-
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
95+
96+
const hostname = ghUrl.hostname.trimEnd().toUpperCase();
97+
const isGitHubHost = hostname === 'GITHUB.COM';
98+
const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM');
99+
const isLocalHost = hostname.endsWith('.LOCALHOST');
100+
101+
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost;
96102
}
97103

98104
export function isCacheFeatureAvailable(): boolean {

0 commit comments

Comments
 (0)