mirror of
https://github.com/actions/checkout.git
synced 2024-12-26 11:03:04 +07:00
second attempt
This commit is contained in:
parent
e8e821983d
commit
df4b58c789
23
__test__/url-helper.test.ts
Normal file
23
__test__/url-helper.test.ts
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import * as urlHelper from '../src/url-helper'
|
||||||
|
|
||||||
|
describe('isGhes tests', () => {
|
||||||
|
it('basics', async () => {
|
||||||
|
expect(urlHelper.isGhes()).toBeFalsy()
|
||||||
|
expect(urlHelper.isGhes('https://github.com')).toBeFalsy()
|
||||||
|
//expect(urlHelper.isGhes('https://api.github.com')).toBeFalsy()
|
||||||
|
expect(urlHelper.isGhes('https://europe.ghe.com')).toBeFalsy()
|
||||||
|
expect(urlHelper.isGhes('https://test.github.localhost')).toBeFalsy()
|
||||||
|
expect(urlHelper.isGhes('https://src.onpremise.customer.com')).toBeTruthy()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('getServerApiUrl tests', () => {
|
||||||
|
it('basics', async () => {
|
||||||
|
expect(urlHelper.getServerApiUrl()).toBe('https://api.github.com')
|
||||||
|
expect(urlHelper.getServerApiUrl('https://github.com')).toBe('https://api.github.com')
|
||||||
|
expect(urlHelper.getServerApiUrl('https://GitHub.com')).toBe('https://api.github.com')
|
||||||
|
expect(urlHelper.getServerApiUrl('https://europe.ghe.com')).toBe('https://api.europe.ghe.com')
|
||||||
|
expect(urlHelper.getServerApiUrl('https://australia.GHE.COM')).toBe('https://api.australia.ghe.com')
|
||||||
|
expect(urlHelper.getServerApiUrl('https://src.onpremise.customer.com')).toBe('https://src.onpremise.customer.com/api/v3')
|
||||||
|
})
|
||||||
|
})
|
@ -21,20 +21,32 @@ export function getFetchUrl(settings: IGitSourceSettings): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function getServerUrl(url?: string): URL {
|
export function getServerUrl(url?: string): URL {
|
||||||
let urlValue =
|
let resolvedUrl = process.env['GITHUB_SERVER_URL'] || 'https://github.com'
|
||||||
url && url.trim().length > 0
|
if (hasContent(url, false)) {
|
||||||
? url
|
resolvedUrl = url!
|
||||||
: process.env['GITHUB_SERVER_URL'] || 'https://github.com'
|
}
|
||||||
return new URL(urlValue)
|
|
||||||
|
return new URL(resolvedUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getServerApiUrl(): string {
|
export function getServerApiUrl(url?: string): string {
|
||||||
|
if (hasContent(url, false)) {
|
||||||
|
let serverUrl = getServerUrl(url)
|
||||||
|
if (isGhes(url)) {
|
||||||
|
serverUrl.pathname = "api/v3"
|
||||||
|
} else {
|
||||||
|
serverUrl.hostname = "api." + serverUrl.hostname
|
||||||
|
}
|
||||||
|
|
||||||
|
return pruneSuffix(serverUrl.toString(), '/')
|
||||||
|
}
|
||||||
|
|
||||||
return process.env['GITHUB_API_URL'] || 'https://api.github.com'
|
return process.env['GITHUB_API_URL'] || 'https://api.github.com'
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isGhes(): boolean {
|
export function isGhes(url?: string): boolean {
|
||||||
const ghUrl = new URL(
|
const ghUrl = new URL(
|
||||||
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
|
url || process.env['GITHUB_SERVER_URL'] || 'https://github.com'
|
||||||
)
|
)
|
||||||
|
|
||||||
const hostname = ghUrl.hostname.trimEnd().toUpperCase()
|
const hostname = ghUrl.hostname.trimEnd().toUpperCase()
|
||||||
@ -44,3 +56,20 @@ export function isGhes(): boolean {
|
|||||||
|
|
||||||
return !isGitHubHost && !isGheHost && !isLocalHost
|
return !isGitHubHost && !isGheHost && !isLocalHost
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function pruneSuffix(text: string, suffix: string) {
|
||||||
|
if (hasContent(suffix, true) && text?.endsWith(suffix)) {
|
||||||
|
return text.substring(0, text.length - suffix.length)
|
||||||
|
}
|
||||||
|
return text
|
||||||
|
}
|
||||||
|
|
||||||
|
function hasContent(text: string | undefined, allowPureWhitespace: boolean): boolean {
|
||||||
|
let refinedText = text ?? ""
|
||||||
|
if (!allowPureWhitespace) {
|
||||||
|
refinedText = refinedText.trim()
|
||||||
|
}
|
||||||
|
return refinedText.length > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user