2020-03-25 20:12:22 +01:00
|
|
|
import * as assert from 'assert'
|
|
|
|
import {URL} from 'url'
|
2022-09-26 18:34:52 +02:00
|
|
|
import {IGitSourceSettings} from './git-source-settings'
|
2020-03-25 20:12:22 +01:00
|
|
|
|
|
|
|
export function getFetchUrl(settings: IGitSourceSettings): string {
|
|
|
|
assert.ok(
|
|
|
|
settings.repositoryOwner,
|
|
|
|
'settings.repositoryOwner must be defined'
|
|
|
|
)
|
|
|
|
assert.ok(settings.repositoryName, 'settings.repositoryName must be defined')
|
2022-09-26 18:34:52 +02:00
|
|
|
const serviceUrl = getServerUrl(settings.githubServerUrl)
|
2020-03-25 20:12:22 +01:00
|
|
|
const encodedOwner = encodeURIComponent(settings.repositoryOwner)
|
|
|
|
const encodedName = encodeURIComponent(settings.repositoryName)
|
|
|
|
if (settings.sshKey) {
|
2024-04-18 21:29:55 +02:00
|
|
|
const user = settings.sshUser.length > 0 ? settings.sshUser : 'git'
|
|
|
|
return `${user}@${serviceUrl.hostname}:${encodedOwner}/${encodedName}.git`
|
2020-03-25 20:12:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// "origin" is SCHEME://HOSTNAME[:PORT]
|
|
|
|
return `${serviceUrl.origin}/${encodedOwner}/${encodedName}`
|
|
|
|
}
|
|
|
|
|
2022-09-26 18:34:52 +02:00
|
|
|
export function getServerUrl(url?: string): URL {
|
|
|
|
let urlValue =
|
|
|
|
url && url.trim().length > 0
|
|
|
|
? url
|
|
|
|
: process.env['GITHUB_SERVER_URL'] || 'https://github.com'
|
|
|
|
return new URL(urlValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getServerApiUrl(url?: string): string {
|
|
|
|
let apiUrl = 'https://api.github.com'
|
|
|
|
|
|
|
|
if (isGhes(url)) {
|
|
|
|
const serverUrl = getServerUrl(url)
|
|
|
|
apiUrl = new URL(`${serverUrl.origin}/api/v3`).toString()
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiUrl
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isGhes(url?: string): boolean {
|
|
|
|
const ghUrl = getServerUrl(url)
|
|
|
|
|
|
|
|
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM'
|
2020-03-25 20:12:22 +01:00
|
|
|
}
|