diff --git a/README.md b/README.md index 9b6176d..b1c8b2c 100644 --- a/README.md +++ b/README.md @@ -123,8 +123,9 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/ # The base URL for the GitHub instance that you are trying to clone from, will use # environment defaults to fetch from the same instance that the workflow is - # running from unless specified. Example URLs are https://github.com or - # https://my-ghes-server.example.com + # running from unless specified. Also support URL pathname except SSH (`ssh-key` + # not specified). Example URLs are https://github.com or + # https://my-ghes-server.example.com or https://my-ghes-server.example.com/git/ github-server-url: '' ``` diff --git a/action.yml b/action.yml index 75d5ae2..3a6ba95 100644 --- a/action.yml +++ b/action.yml @@ -96,7 +96,7 @@ inputs: description: Add repository path as safe.directory for Git global config by running `git config --global --add safe.directory ` default: true github-server-url: - description: The base URL for the GitHub instance that you are trying to clone from, will use environment defaults to fetch from the same instance that the workflow is running from unless specified. Example URLs are https://github.com or https://my-ghes-server.example.com + description: The base URL for the GitHub instance that you are trying to clone from, will use environment defaults to fetch from the same instance that the workflow is running from unless specified. Also support URL pathname except SSH (`ssh-key` not specified). Example URLs are https://github.com or https://my-ghes-server.example.com or https://my-ghes-server.example.com/git/ required: false runs: using: node20 diff --git a/dist/index.js b/dist/index.js index 9d959a9..8728adf 100644 --- a/dist/index.js +++ b/dist/index.js @@ -168,13 +168,14 @@ class GitAuthHelper { this.settings = gitSourceSettings || {}; // Token auth header const serverUrl = urlHelper.getServerUrl(this.settings.githubServerUrl); - this.tokenConfigKey = `http.${serverUrl.origin}/.extraheader`; // "origin" is SCHEME://HOSTNAME[:PORT] + const baseURL = urlHelper.getBaseUrl(serverUrl.href); + this.tokenConfigKey = `http.${baseURL}/.extraheader`; // "origin" is SCHEME://HOSTNAME[:PORT] const basicCredential = Buffer.from(`x-access-token:${this.settings.authToken}`, 'utf8').toString('base64'); core.setSecret(basicCredential); this.tokenPlaceholderConfigValue = `AUTHORIZATION: basic ***`; this.tokenConfigValue = `AUTHORIZATION: basic ${basicCredential}`; // Instead of SSH URL - this.insteadOfKey = `url.${serverUrl.origin}/.insteadOf`; // "origin" is SCHEME://HOSTNAME[:PORT] + this.insteadOfKey = `url.${baseURL}/.insteadOf`; // "origin" is SCHEME://HOSTNAME[:PORT] this.insteadOfValues.push(`git@${serverUrl.hostname}:`); if (this.settings.workflowOrganizationId) { this.insteadOfValues.push(`org-${this.settings.workflowOrganizationId}@github.com:`); @@ -2439,7 +2440,7 @@ var __importStar = (this && this.__importStar) || function (mod) { return result; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.isGhes = exports.getServerApiUrl = exports.getServerUrl = exports.getFetchUrl = void 0; +exports.isGhes = exports.getServerApiUrl = exports.getBaseUrl = exports.getServerUrl = exports.getFetchUrl = void 0; const assert = __importStar(__nccwpck_require__(9491)); const url_1 = __nccwpck_require__(7310); function getFetchUrl(settings) { @@ -2453,16 +2454,22 @@ function getFetchUrl(settings) { return `${user}@${serviceUrl.hostname}:${encodedOwner}/${encodedName}.git`; } // "origin" is SCHEME://HOSTNAME[:PORT] - return `${serviceUrl.origin}/${encodedOwner}/${encodedName}`; + const baseURL = getBaseUrl(serviceUrl.href); + return `${baseURL}/${encodedOwner}/${encodedName}`; } exports.getFetchUrl = getFetchUrl; function getServerUrl(url) { - let urlValue = url && url.trim().length > 0 + const urlValue = url && url.trim().length > 0 ? url : process.env['GITHUB_SERVER_URL'] || 'https://github.com'; return new url_1.URL(urlValue); } exports.getServerUrl = getServerUrl; +function getBaseUrl(url) { + const matcher = url.match(/^[^?]+/); + return (matcher && matcher[0].replace(/\/+$/g, '')) || ''; +} +exports.getBaseUrl = getBaseUrl; function getServerApiUrl(url) { let apiUrl = 'https://api.github.com'; if (isGhes(url)) { diff --git a/src/git-auth-helper.ts b/src/git-auth-helper.ts index 126e8e5..1cf42b0 100644 --- a/src/git-auth-helper.ts +++ b/src/git-auth-helper.ts @@ -53,7 +53,8 @@ class GitAuthHelper { // Token auth header const serverUrl = urlHelper.getServerUrl(this.settings.githubServerUrl) - this.tokenConfigKey = `http.${serverUrl.origin}/.extraheader` // "origin" is SCHEME://HOSTNAME[:PORT] + const baseURL = urlHelper.getBaseUrl(serverUrl.href) + this.tokenConfigKey = `http.${baseURL}/.extraheader` // "origin" is SCHEME://HOSTNAME[:PORT] const basicCredential = Buffer.from( `x-access-token:${this.settings.authToken}`, 'utf8' @@ -63,7 +64,7 @@ class GitAuthHelper { this.tokenConfigValue = `AUTHORIZATION: basic ${basicCredential}` // Instead of SSH URL - this.insteadOfKey = `url.${serverUrl.origin}/.insteadOf` // "origin" is SCHEME://HOSTNAME[:PORT] + this.insteadOfKey = `url.${baseURL}/.insteadOf` // "origin" is SCHEME://HOSTNAME[:PORT] this.insteadOfValues.push(`git@${serverUrl.hostname}:`) if (this.settings.workflowOrganizationId) { this.insteadOfValues.push( diff --git a/src/url-helper.ts b/src/url-helper.ts index 64ecbf3..07774bf 100644 --- a/src/url-helper.ts +++ b/src/url-helper.ts @@ -17,17 +17,23 @@ export function getFetchUrl(settings: IGitSourceSettings): string { } // "origin" is SCHEME://HOSTNAME[:PORT] - return `${serviceUrl.origin}/${encodedOwner}/${encodedName}` + const baseURL = getBaseUrl(serviceUrl.href) + return `${baseURL}/${encodedOwner}/${encodedName}` } export function getServerUrl(url?: string): URL { - let urlValue = + const urlValue = url && url.trim().length > 0 ? url : process.env['GITHUB_SERVER_URL'] || 'https://github.com' return new URL(urlValue) } +export function getBaseUrl(url: string): string { + const matcher = url.match(/^[^?]+/) + return (matcher && matcher[0].replace(/\/+$/g, '')) || '' +} + export function getServerApiUrl(url?: string): string { let apiUrl = 'https://api.github.com'