Skip to content

Commit 163217d

Browse files
authored
url-helper.ts now leverages well-known environment variables. (#1941)
* `utl-helper.ts` now leverages well-known environment variables. --------- Co-authored-by: Erez Testiler <[email protected]>
1 parent eef6144 commit 163217d

File tree

3 files changed

+141
-23
lines changed

3 files changed

+141
-23
lines changed

__test__/url-helper.test.ts

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import * as urlHelper from '../src/url-helper'
2+
3+
describe('getServerUrl tests', () => {
4+
it('basics', async () => {
5+
// Note that URL::toString will append a trailing / when passed just a domain name ...
6+
expect(urlHelper.getServerUrl().toString()).toBe('https://github.com/')
7+
expect(urlHelper.getServerUrl(' ').toString()).toBe('https://github.com/')
8+
expect(urlHelper.getServerUrl(' ').toString()).toBe('https://github.com/')
9+
expect(urlHelper.getServerUrl('http://contoso.com').toString()).toBe(
10+
'http://contoso.com/'
11+
)
12+
expect(urlHelper.getServerUrl('https://contoso.com').toString()).toBe(
13+
'https://contoso.com/'
14+
)
15+
expect(urlHelper.getServerUrl('https://contoso.com/').toString()).toBe(
16+
'https://contoso.com/'
17+
)
18+
19+
// ... but can't make that same assumption when passed an URL that includes some deeper path.
20+
expect(urlHelper.getServerUrl('https://contoso.com/a/b').toString()).toBe(
21+
'https://contoso.com/a/b'
22+
)
23+
})
24+
})
25+
26+
describe('isGhes tests', () => {
27+
it('basics', async () => {
28+
expect(urlHelper.isGhes()).toBeFalsy()
29+
expect(urlHelper.isGhes('https://github.com')).toBeFalsy()
30+
expect(urlHelper.isGhes('https://contoso.ghe.com')).toBeFalsy()
31+
expect(urlHelper.isGhes('https://test.github.localhost')).toBeFalsy()
32+
expect(urlHelper.isGhes('https://src.onpremise.fabrikam.com')).toBeTruthy()
33+
})
34+
})
35+
36+
describe('getServerApiUrl tests', () => {
37+
it('basics', async () => {
38+
expect(urlHelper.getServerApiUrl()).toBe('https://api.github.com')
39+
expect(urlHelper.getServerApiUrl('https://github.com')).toBe(
40+
'https://api.github.com'
41+
)
42+
expect(urlHelper.getServerApiUrl('https://GitHub.com')).toBe(
43+
'https://api.github.com'
44+
)
45+
expect(urlHelper.getServerApiUrl('https://contoso.ghe.com')).toBe(
46+
'https://api.contoso.ghe.com'
47+
)
48+
expect(urlHelper.getServerApiUrl('https://fabrikam.GHE.COM')).toBe(
49+
'https://api.fabrikam.ghe.com'
50+
)
51+
expect(
52+
urlHelper.getServerApiUrl('https://src.onpremise.fabrikam.com')
53+
).toBe('https://src.onpremise.fabrikam.com/api/v3')
54+
})
55+
})

dist/index.js

+39-11
Original file line numberDiff line numberDiff line change
@@ -2454,22 +2454,50 @@ function getFetchUrl(settings) {
24542454
return `${serviceUrl.origin}/${encodedOwner}/${encodedName}`;
24552455
}
24562456
function getServerUrl(url) {
2457-
let urlValue = url && url.trim().length > 0
2458-
? url
2459-
: process.env['GITHUB_SERVER_URL'] || 'https://github.com';
2460-
return new url_1.URL(urlValue);
2457+
let resolvedUrl = process.env['GITHUB_SERVER_URL'] || 'https://github.com';
2458+
if (hasContent(url, WhitespaceMode.Trim)) {
2459+
resolvedUrl = url;
2460+
}
2461+
return new url_1.URL(resolvedUrl);
24612462
}
24622463
function getServerApiUrl(url) {
2463-
let apiUrl = 'https://api.github.com';
2464-
if (isGhes(url)) {
2465-
const serverUrl = getServerUrl(url);
2466-
apiUrl = new url_1.URL(`${serverUrl.origin}/api/v3`).toString();
2464+
if (hasContent(url, WhitespaceMode.Trim)) {
2465+
let serverUrl = getServerUrl(url);
2466+
if (isGhes(url)) {
2467+
serverUrl.pathname = 'api/v3';
2468+
}
2469+
else {
2470+
serverUrl.hostname = 'api.' + serverUrl.hostname;
2471+
}
2472+
return pruneSuffix(serverUrl.toString(), '/');
24672473
}
2468-
return apiUrl;
2474+
return process.env['GITHUB_API_URL'] || 'https://api.github.com';
24692475
}
24702476
function isGhes(url) {
2471-
const ghUrl = getServerUrl(url);
2472-
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
2477+
const ghUrl = new url_1.URL(url || process.env['GITHUB_SERVER_URL'] || 'https://github.com');
2478+
const hostname = ghUrl.hostname.trimEnd().toUpperCase();
2479+
const isGitHubHost = hostname === 'GITHUB.COM';
2480+
const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM');
2481+
const isLocalHost = hostname.endsWith('.LOCALHOST');
2482+
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost;
2483+
}
2484+
function pruneSuffix(text, suffix) {
2485+
if (hasContent(suffix, WhitespaceMode.Preserve) && (text === null || text === void 0 ? void 0 : text.endsWith(suffix))) {
2486+
return text.substring(0, text.length - suffix.length);
2487+
}
2488+
return text;
2489+
}
2490+
var WhitespaceMode;
2491+
(function (WhitespaceMode) {
2492+
WhitespaceMode[WhitespaceMode["Trim"] = 0] = "Trim";
2493+
WhitespaceMode[WhitespaceMode["Preserve"] = 1] = "Preserve";
2494+
})(WhitespaceMode || (WhitespaceMode = {}));
2495+
function hasContent(text, whitespaceMode) {
2496+
let refinedText = text !== null && text !== void 0 ? text : '';
2497+
if (whitespaceMode == WhitespaceMode.Trim) {
2498+
refinedText = refinedText.trim();
2499+
}
2500+
return refinedText.length > 0;
24732501
}
24742502

24752503

src/url-helper.ts

+47-12
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,61 @@ export function getFetchUrl(settings: IGitSourceSettings): string {
2121
}
2222

2323
export function getServerUrl(url?: string): URL {
24-
let urlValue =
25-
url && url.trim().length > 0
26-
? url
27-
: process.env['GITHUB_SERVER_URL'] || 'https://github.com'
28-
return new URL(urlValue)
24+
let resolvedUrl = process.env['GITHUB_SERVER_URL'] || 'https://github.com'
25+
if (hasContent(url, WhitespaceMode.Trim)) {
26+
resolvedUrl = url!
27+
}
28+
29+
return new URL(resolvedUrl)
2930
}
3031

3132
export function getServerApiUrl(url?: string): string {
32-
let apiUrl = 'https://api.github.com'
33+
if (hasContent(url, WhitespaceMode.Trim)) {
34+
let serverUrl = getServerUrl(url)
35+
if (isGhes(url)) {
36+
serverUrl.pathname = 'api/v3'
37+
} else {
38+
serverUrl.hostname = 'api.' + serverUrl.hostname
39+
}
3340

34-
if (isGhes(url)) {
35-
const serverUrl = getServerUrl(url)
36-
apiUrl = new URL(`${serverUrl.origin}/api/v3`).toString()
41+
return pruneSuffix(serverUrl.toString(), '/')
3742
}
3843

39-
return apiUrl
44+
return process.env['GITHUB_API_URL'] || 'https://api.github.com'
4045
}
4146

4247
export function isGhes(url?: string): boolean {
43-
const ghUrl = getServerUrl(url)
48+
const ghUrl = new URL(
49+
url || process.env['GITHUB_SERVER_URL'] || 'https://github.com'
50+
)
51+
52+
const hostname = ghUrl.hostname.trimEnd().toUpperCase()
53+
const isGitHubHost = hostname === 'GITHUB.COM'
54+
const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM')
55+
const isLocalHost = hostname.endsWith('.LOCALHOST')
56+
57+
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost
58+
}
4459

45-
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM'
60+
function pruneSuffix(text: string, suffix: string) {
61+
if (hasContent(suffix, WhitespaceMode.Preserve) && text?.endsWith(suffix)) {
62+
return text.substring(0, text.length - suffix.length)
63+
}
64+
return text
65+
}
66+
67+
enum WhitespaceMode {
68+
Trim,
69+
Preserve
70+
}
71+
72+
function hasContent(
73+
text: string | undefined,
74+
whitespaceMode: WhitespaceMode
75+
): boolean {
76+
let refinedText = text ?? ''
77+
if (whitespaceMode == WhitespaceMode.Trim) {
78+
refinedText = refinedText.trim()
79+
}
80+
return refinedText.length > 0
4681
}

0 commit comments

Comments
 (0)