mirror of
https://code.forgejo.org/actions/checkout.git
synced 2024-11-05 10:12:53 +01:00
1d96c772d1
* Add a configurable SSH user * Update docs with param * Indentation of readme * formatting woes * Update src/url-helper.ts Co-authored-by: Josh Gross <joshmgross@github.com> * Update action.yml Co-authored-by: Josh Gross <joshmgross@github.com> * Update genfiles --------- Co-authored-by: Josh Gross <joshmgross@github.com>
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
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 {
|
|
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'
|
|
}
|