diff options
| author | Peter Murray <681306+peter-murray@users.noreply.github.com> | 2022-09-26 17:34:52 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-26 12:34:52 -0400 |
| commit | e33cfa574212e8722034156757f8383979e76cb9 (patch) | |
| tree | 0c9284b63ea98535c6a10b080a477136102a6856 /src/url-helper.ts | |
| parent | fa9591092fd91f59e3dbebbb92da3e2787a0bc03 (diff) | |
Inject GitHub host to be able to clone from another GitHub instance (#922)
* Adding the ability to specify the GitHub Server URL and allowing for it to differ from the Actions workflow host
* Adding tests for injecting the GitHub URL
* Addressing code review comments for PR #922
Diffstat (limited to 'src/url-helper.ts')
| -rw-r--r-- | src/url-helper.ts | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/src/url-helper.ts b/src/url-helper.ts index 05f1cbd..6807b7f 100644 --- a/src/url-helper.ts +++ b/src/url-helper.ts @@ -1,6 +1,6 @@ import * as assert from 'assert' -import {IGitSourceSettings} from './git-source-settings' import {URL} from 'url' +import {IGitSourceSettings} from './git-source-settings' export function getFetchUrl(settings: IGitSourceSettings): string { assert.ok( @@ -8,7 +8,7 @@ export function getFetchUrl(settings: IGitSourceSettings): string { 'settings.repositoryOwner must be defined' ) assert.ok(settings.repositoryName, 'settings.repositoryName must be defined') - const serviceUrl = getServerUrl() + const serviceUrl = getServerUrl(settings.githubServerUrl) const encodedOwner = encodeURIComponent(settings.repositoryOwner) const encodedName = encodeURIComponent(settings.repositoryName) if (settings.sshKey) { @@ -19,11 +19,27 @@ export function getFetchUrl(settings: IGitSourceSettings): string { return `${serviceUrl.origin}/${encodedOwner}/${encodedName}` } -export function getServerUrl(): URL { - // todo: remove GITHUB_URL after support for GHES Alpha is no longer needed - return new URL( - process.env['GITHUB_SERVER_URL'] || - process.env['GITHUB_URL'] || - 'https://github.com' - ) +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' } |
