0
1
Fork 0
mirror of https://github.com/actions/checkout synced 2024-06-26 15:58:20 +02:00

fix: support pathname except ssh

This commit is contained in:
CHANX 2023-04-27 23:17:17 +08:00 committed by ischanx
parent f095bcc56b
commit 06c6114540
5 changed files with 27 additions and 12 deletions

View file

@ -98,8 +98,9 @@ When Git 2.18 or higher is not in your PATH, falls back to the REST API to downl
# The base URL for the GitHub instance that you are trying to clone from, will use # 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 # environment defaults to fetch from the same instance that the workflow is
# running from unless specified. Example URLs are https://github.com or # running from unless specified. Also support URL pathname except SSH (`ssh-key`
# https://my-ghes-server.example.com # 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: '' github-server-url: ''
``` ```
<!-- end usage --> <!-- end usage -->

View file

@ -72,7 +72,7 @@ inputs:
description: Add repository path as safe.directory for Git global config by running `git config --global --add safe.directory <path>` description: Add repository path as safe.directory for Git global config by running `git config --global --add safe.directory <path>`
default: true default: true
github-server-url: 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 required: false
runs: runs:
using: node16 using: node16

17
dist/index.js vendored
View file

@ -163,13 +163,14 @@ class GitAuthHelper {
this.settings = gitSourceSettings || {}; this.settings = gitSourceSettings || {};
// Token auth header // Token auth header
const serverUrl = urlHelper.getServerUrl(this.settings.githubServerUrl); 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'); const basicCredential = Buffer.from(`x-access-token:${this.settings.authToken}`, 'utf8').toString('base64');
core.setSecret(basicCredential); core.setSecret(basicCredential);
this.tokenPlaceholderConfigValue = `AUTHORIZATION: basic ***`; this.tokenPlaceholderConfigValue = `AUTHORIZATION: basic ***`;
this.tokenConfigValue = `AUTHORIZATION: basic ${basicCredential}`; this.tokenConfigValue = `AUTHORIZATION: basic ${basicCredential}`;
// Instead of SSH URL // 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}:`); this.insteadOfValues.push(`git@${serverUrl.hostname}:`);
if (this.settings.workflowOrganizationId) { if (this.settings.workflowOrganizationId) {
this.insteadOfValues.push(`org-${this.settings.workflowOrganizationId}@github.com:`); this.insteadOfValues.push(`org-${this.settings.workflowOrganizationId}@github.com:`);
@ -2294,7 +2295,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result; return result;
}; };
Object.defineProperty(exports, "__esModule", ({ value: true })); 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 assert = __importStar(__nccwpck_require__(9491));
const url_1 = __nccwpck_require__(7310); const url_1 = __nccwpck_require__(7310);
function getFetchUrl(settings) { function getFetchUrl(settings) {
@ -2307,16 +2308,22 @@ function getFetchUrl(settings) {
return `git@${serviceUrl.hostname}:${encodedOwner}/${encodedName}.git`; return `git@${serviceUrl.hostname}:${encodedOwner}/${encodedName}.git`;
} }
// "origin" is SCHEME://HOSTNAME[:PORT] // "origin" is SCHEME://HOSTNAME[:PORT]
return `${serviceUrl.origin}/${encodedOwner}/${encodedName}`; const baseURL = getBaseUrl(serviceUrl.href);
return `${baseURL}/${encodedOwner}/${encodedName}`;
} }
exports.getFetchUrl = getFetchUrl; exports.getFetchUrl = getFetchUrl;
function getServerUrl(url) { function getServerUrl(url) {
let urlValue = url && url.trim().length > 0 const urlValue = url && url.trim().length > 0
? url ? url
: process.env['GITHUB_SERVER_URL'] || 'https://github.com'; : process.env['GITHUB_SERVER_URL'] || 'https://github.com';
return new url_1.URL(urlValue); return new url_1.URL(urlValue);
} }
exports.getServerUrl = getServerUrl; exports.getServerUrl = getServerUrl;
function getBaseUrl(url) {
const matcher = url.match(/^[^?]+/);
return (matcher && matcher[0].replace(/\/+$/g, '')) || '';
}
exports.getBaseUrl = getBaseUrl;
function getServerApiUrl(url) { function getServerApiUrl(url) {
let apiUrl = 'https://api.github.com'; let apiUrl = 'https://api.github.com';
if (isGhes(url)) { if (isGhes(url)) {

View file

@ -53,7 +53,8 @@ class GitAuthHelper {
// Token auth header // Token auth header
const serverUrl = urlHelper.getServerUrl(this.settings.githubServerUrl) 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( const basicCredential = Buffer.from(
`x-access-token:${this.settings.authToken}`, `x-access-token:${this.settings.authToken}`,
'utf8' 'utf8'
@ -63,7 +64,7 @@ class GitAuthHelper {
this.tokenConfigValue = `AUTHORIZATION: basic ${basicCredential}` this.tokenConfigValue = `AUTHORIZATION: basic ${basicCredential}`
// Instead of SSH URL // 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}:`) this.insteadOfValues.push(`git@${serverUrl.hostname}:`)
if (this.settings.workflowOrganizationId) { if (this.settings.workflowOrganizationId) {
this.insteadOfValues.push( this.insteadOfValues.push(

View file

@ -16,17 +16,23 @@ export function getFetchUrl(settings: IGitSourceSettings): string {
} }
// "origin" is SCHEME://HOSTNAME[:PORT] // "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 { export function getServerUrl(url?: string): URL {
let urlValue = const urlValue =
url && url.trim().length > 0 url && url.trim().length > 0
? url ? url
: process.env['GITHUB_SERVER_URL'] || 'https://github.com' : process.env['GITHUB_SERVER_URL'] || 'https://github.com'
return new URL(urlValue) 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 { export function getServerApiUrl(url?: string): string {
let apiUrl = 'https://api.github.com' let apiUrl = 'https://api.github.com'