2020-03-25 15:12:22 -04:00
|
|
|
import * as assert from 'assert'
|
|
|
|
import {URL} from 'url'
|
2022-09-26 12:34:52 -04:00
|
|
|
import {IGitSourceSettings} from './git-source-settings'
|
2020-03-25 15:12:22 -04: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 12:34:52 -04:00
|
|
|
const serviceUrl = getServerUrl(settings.githubServerUrl)
|
2020-03-25 15:12:22 -04:00
|
|
|
const encodedOwner = encodeURIComponent(settings.repositoryOwner)
|
|
|
|
const encodedName = encodeURIComponent(settings.repositoryName)
|
|
|
|
if (settings.sshKey) {
|
|
|
|
return `git@${serviceUrl.hostname}:${encodedOwner}/${encodedName}.git`
|
|
|
|
}
|
|
|
|
|
|
|
|
// "origin" is SCHEME://HOSTNAME[:PORT]
|
|
|
|
return `${serviceUrl.origin}/${encodedOwner}/${encodedName}`
|
|
|
|
}
|
|
|
|
|
2022-09-26 12:34:52 -04: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 15:12:22 -04:00
|
|
|
}
|