checkout/src/url-helper.ts

85 lines
2.5 KiB
TypeScript
Raw Normal View History

import * as assert from 'assert'
import {URL} from 'url'
import {IGitSourceSettings} from './git-source-settings'
export function getFetchUrl(settings: IGitSourceSettings): string {
assert.ok(
settings.repositoryOwner,
'settings.repositoryOwner must be defined'
)
assert.ok(settings.repositoryName, 'settings.repositoryName must be defined')
const serviceUrl = getServerUrl(settings.githubServerUrl)
const encodedOwner = encodeURIComponent(settings.repositoryOwner)
const encodedName = encodeURIComponent(settings.repositoryName)
if (settings.sshKey) {
const user = settings.sshUser.length > 0 ? settings.sshUser : 'git'
return `${user}@${serviceUrl.hostname}:${encodedOwner}/${encodedName}.git`
}
// "origin" is SCHEME://HOSTNAME[:PORT]
return `${serviceUrl.origin}/${encodedOwner}/${encodedName}`
}
export function getServerUrl(url?: string): URL {
2024-10-17 01:29:39 +08:00
let resolvedUrl = process.env['GITHUB_SERVER_URL'] || 'https://github.com'
2024-10-18 06:54:12 +08:00
if (hasContent(url, WhitespaceMode.IgnorePureWhitespace)) {
2024-10-17 01:29:39 +08:00
resolvedUrl = url!
}
return new URL(resolvedUrl)
}
2024-10-17 01:29:39 +08:00
export function getServerApiUrl(url?: string): string {
2024-10-18 06:54:12 +08:00
if (hasContent(url, WhitespaceMode.IgnorePureWhitespace)) {
2024-10-17 01:29:39 +08:00
let serverUrl = getServerUrl(url)
if (isGhes(url)) {
2024-10-17 02:15:26 +08:00
serverUrl.pathname = 'api/v3'
2024-10-17 01:29:39 +08:00
} else {
2024-10-17 02:15:26 +08:00
serverUrl.hostname = 'api.' + serverUrl.hostname
2024-10-17 01:29:39 +08:00
}
return pruneSuffix(serverUrl.toString(), '/')
}
return process.env['GITHUB_API_URL'] || 'https://api.github.com'
}
2024-10-17 01:29:39 +08:00
export function isGhes(url?: string): boolean {
const ghUrl = new URL(
2024-10-17 01:29:39 +08:00
url || process.env['GITHUB_SERVER_URL'] || 'https://github.com'
)
const hostname = ghUrl.hostname.trimEnd().toUpperCase()
const isGitHubHost = hostname === 'GITHUB.COM'
2024-10-18 01:46:35 +08:00
const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM')
const isLocalHost = hostname.endsWith('.LOCALHOST')
2024-10-18 01:46:35 +08:00
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost
}
2024-10-17 01:29:39 +08:00
function pruneSuffix(text: string, suffix: string) {
2024-10-18 06:54:12 +08:00
if (
hasContent(suffix, WhitespaceMode.AllowPureWhitespace) &&
text?.endsWith(suffix)
) {
2024-10-17 01:29:39 +08:00
return text.substring(0, text.length - suffix.length)
}
return text
}
2024-10-18 06:54:12 +08:00
enum WhitespaceMode {
IgnorePureWhitespace,
AllowPureWhitespace
}
2024-10-17 02:15:26 +08:00
function hasContent(
text: string | undefined,
2024-10-18 06:54:12 +08:00
whitespaceMode: WhitespaceMode
2024-10-17 02:15:26 +08:00
): boolean {
let refinedText = text ?? ''
2024-10-18 06:54:12 +08:00
if (whitespaceMode == WhitespaceMode.IgnorePureWhitespace) {
2024-10-17 01:29:39 +08:00
refinedText = refinedText.trim()
}
return refinedText.length > 0
}