Compare commits

..

4 Commits

Author SHA1 Message Date
John Wesley Walker III
55f79d95df Use concise WhitespaceMode names 2024-10-17 23:01:47 +00:00
John Wesley Walker III
7fd13ec418 Address hasContent readability 2024-10-17 22:54:12 +00:00
John Wesley Walker III
7695871fe0 ran npm run build 2024-10-17 17:50:52 +00:00
John Wesley Walker III
914445f9e7 Responded to PR feedback. 2024-10-17 17:46:35 +00:00
3 changed files with 47 additions and 14 deletions

View File

@ -1,5 +1,28 @@
import * as urlHelper from '../src/url-helper'
describe('getServerUrl tests', () => {
it('basics', async () => {
// Note that URL::toString will append a trailing / when passed just a domain name ...
expect(urlHelper.getServerUrl().toString()).toBe('https://github.com/')
expect(urlHelper.getServerUrl(' ').toString()).toBe('https://github.com/')
expect(urlHelper.getServerUrl(' ').toString()).toBe('https://github.com/')
expect(urlHelper.getServerUrl('http://contoso.com').toString()).toBe(
'http://contoso.com/'
)
expect(urlHelper.getServerUrl('https://contoso.com').toString()).toBe(
'https://contoso.com/'
)
expect(urlHelper.getServerUrl('https://contoso.com/').toString()).toBe(
'https://contoso.com/'
)
// ... but can't make that same assumption when passed an URL that includes some deeper path.
expect(urlHelper.getServerUrl('https://contoso.com/a/b').toString()).toBe(
'https://contoso.com/a/b'
)
})
})
describe('isGhes tests', () => {
it('basics', async () => {
expect(urlHelper.isGhes()).toBeFalsy()

19
dist/index.js vendored
View File

@ -2455,13 +2455,13 @@ function getFetchUrl(settings) {
}
function getServerUrl(url) {
let resolvedUrl = process.env['GITHUB_SERVER_URL'] || 'https://github.com';
if (hasContent(url, false)) {
if (hasContent(url, WhitespaceMode.Trim)) {
resolvedUrl = url;
}
return new url_1.URL(resolvedUrl);
}
function getServerApiUrl(url) {
if (hasContent(url, false)) {
if (hasContent(url, WhitespaceMode.Trim)) {
let serverUrl = getServerUrl(url);
if (isGhes(url)) {
serverUrl.pathname = 'api/v3';
@ -2477,19 +2477,24 @@ function isGhes(url) {
const ghUrl = new url_1.URL(url || process.env['GITHUB_SERVER_URL'] || 'https://github.com');
const hostname = ghUrl.hostname.trimEnd().toUpperCase();
const isGitHubHost = hostname === 'GITHUB.COM';
const isGheHost = hostname.endsWith('.GHE.COM');
const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM');
const isLocalHost = hostname.endsWith('.LOCALHOST');
return !isGitHubHost && !isGheHost && !isLocalHost;
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost;
}
function pruneSuffix(text, suffix) {
if (hasContent(suffix, true) && (text === null || text === void 0 ? void 0 : text.endsWith(suffix))) {
if (hasContent(suffix, WhitespaceMode.Preserve) && (text === null || text === void 0 ? void 0 : text.endsWith(suffix))) {
return text.substring(0, text.length - suffix.length);
}
return text;
}
function hasContent(text, allowPureWhitespace) {
var WhitespaceMode;
(function (WhitespaceMode) {
WhitespaceMode[WhitespaceMode["Trim"] = 0] = "Trim";
WhitespaceMode[WhitespaceMode["Preserve"] = 1] = "Preserve";
})(WhitespaceMode || (WhitespaceMode = {}));
function hasContent(text, whitespaceMode) {
let refinedText = text !== null && text !== void 0 ? text : '';
if (!allowPureWhitespace) {
if (whitespaceMode == WhitespaceMode.Trim) {
refinedText = refinedText.trim();
}
return refinedText.length > 0;

View File

@ -22,7 +22,7 @@ export function getFetchUrl(settings: IGitSourceSettings): string {
export function getServerUrl(url?: string): URL {
let resolvedUrl = process.env['GITHUB_SERVER_URL'] || 'https://github.com'
if (hasContent(url, false)) {
if (hasContent(url, WhitespaceMode.Trim)) {
resolvedUrl = url!
}
@ -30,7 +30,7 @@ export function getServerUrl(url?: string): URL {
}
export function getServerApiUrl(url?: string): string {
if (hasContent(url, false)) {
if (hasContent(url, WhitespaceMode.Trim)) {
let serverUrl = getServerUrl(url)
if (isGhes(url)) {
serverUrl.pathname = 'api/v3'
@ -51,25 +51,30 @@ export function isGhes(url?: string): boolean {
const hostname = ghUrl.hostname.trimEnd().toUpperCase()
const isGitHubHost = hostname === 'GITHUB.COM'
const isGheHost = hostname.endsWith('.GHE.COM')
const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM')
const isLocalHost = hostname.endsWith('.LOCALHOST')
return !isGitHubHost && !isGheHost && !isLocalHost
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost
}
function pruneSuffix(text: string, suffix: string) {
if (hasContent(suffix, true) && text?.endsWith(suffix)) {
if (hasContent(suffix, WhitespaceMode.Preserve) && text?.endsWith(suffix)) {
return text.substring(0, text.length - suffix.length)
}
return text
}
enum WhitespaceMode {
Trim,
Preserve
}
function hasContent(
text: string | undefined,
allowPureWhitespace: boolean
whitespaceMode: WhitespaceMode
): boolean {
let refinedText = text ?? ''
if (!allowPureWhitespace) {
if (whitespaceMode == WhitespaceMode.Trim) {
refinedText = refinedText.trim()
}
return refinedText.length > 0