From dfafeeb72bf1c91a6640195a66891fcc14475c47 Mon Sep 17 00:00:00 2001 From: Peter Evans <18365890+peter-evans@users.noreply.github.com> Date: Thu, 24 Nov 2022 11:52:04 +0900 Subject: [PATCH] fix: support github server url for pushing to fork (#1318) * feat: support github server url for pushing to fork (#1315) Co-authored-by: Kevin Zhu * fix: code formatting * test: fix tests for getRemoteUrl Co-authored-by: MildC Co-authored-by: Kevin Zhu --- __test__/utils.unit.test.ts | 21 +++++++++++++++++++-- dist/index.js | 15 +++++++++------ src/create-pull-request.ts | 1 + src/utils.ts | 22 ++++++++++++++-------- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/__test__/utils.unit.test.ts b/__test__/utils.unit.test.ts index 2eb0a70f2..19b36609d 100644 --- a/__test__/utils.unit.test.ts +++ b/__test__/utils.unit.test.ts @@ -90,11 +90,28 @@ describe('utils tests', () => { }) test('getRemoteUrl successfully returns remote URLs', async () => { - const url1 = utils.getRemoteUrl('HTTPS', 'peter-evans/create-pull-request') + const url1 = utils.getRemoteUrl( + 'HTTPS', + 'github.com', + 'peter-evans/create-pull-request' + ) expect(url1).toEqual('https://github.com/peter-evans/create-pull-request') - const url2 = utils.getRemoteUrl('SSH', 'peter-evans/create-pull-request') + const url2 = utils.getRemoteUrl( + 'SSH', + 'github.com', + 'peter-evans/create-pull-request' + ) expect(url2).toEqual('git@github.com:peter-evans/create-pull-request.git') + + const url3 = utils.getRemoteUrl( + 'HTTPS', + 'mygithubserver.com', + 'peter-evans/create-pull-request' + ) + expect(url3).toEqual( + 'https://mygithubserver.com/peter-evans/create-pull-request' + ) }) test('secondsSinceEpoch returns the number of seconds since the Epoch', async () => { diff --git a/dist/index.js b/dist/index.js index 81ec93919..aff221f87 100644 --- a/dist/index.js +++ b/dist/index.js @@ -339,7 +339,7 @@ function createPullRequest(inputs) { throw new Error(`Repository '${branchRepository}' is not a fork of '${baseRemote.repository}'. Unable to continue.`); } // Add a remote for the fork - const remoteUrl = utils.getRemoteUrl(baseRemote.protocol, branchRepository); + const remoteUrl = utils.getRemoteUrl(baseRemote.protocol, baseRemote.hostname, branchRepository); yield git.exec(['remote', 'add', 'fork', remoteUrl]); } core.endGroup(); @@ -1247,11 +1247,13 @@ function getRemoteDetail(remoteUrl) { if (!githubServerMatch) { throw new Error('Could not parse GitHub Server name'); } - const httpsUrlPattern = new RegExp('^https?://.*@?' + githubServerMatch[1] + '/(.+/.+?)(\\.git)?$', 'i'); - const sshUrlPattern = new RegExp('^git@' + githubServerMatch[1] + ':(.+/.+)\\.git$', 'i'); + const hostname = githubServerMatch[1]; + const httpsUrlPattern = new RegExp('^https?://.*@?' + hostname + '/(.+/.+?)(\\.git)?$', 'i'); + const sshUrlPattern = new RegExp('^git@' + hostname + ':(.+/.+)\\.git$', 'i'); const httpsMatch = remoteUrl.match(httpsUrlPattern); if (httpsMatch) { return { + hostname, protocol: 'HTTPS', repository: httpsMatch[1] }; @@ -1259,6 +1261,7 @@ function getRemoteDetail(remoteUrl) { const sshMatch = remoteUrl.match(sshUrlPattern); if (sshMatch) { return { + hostname, protocol: 'SSH', repository: sshMatch[1] }; @@ -1266,10 +1269,10 @@ function getRemoteDetail(remoteUrl) { throw new Error(`The format of '${remoteUrl}' is not a valid GitHub repository URL`); } exports.getRemoteDetail = getRemoteDetail; -function getRemoteUrl(protocol, repository) { +function getRemoteUrl(protocol, hostname, repository) { return protocol == 'HTTPS' - ? `https://github.com/${repository}` - : `git@github.com:${repository}.git`; + ? `https://${hostname}/${repository}` + : `git@${hostname}:${repository}.git`; } exports.getRemoteUrl = getRemoteUrl; function secondsSinceEpoch() { diff --git a/src/create-pull-request.ts b/src/create-pull-request.ts index b5c59d812..1e42c1f5a 100644 --- a/src/create-pull-request.ts +++ b/src/create-pull-request.ts @@ -74,6 +74,7 @@ export async function createPullRequest(inputs: Inputs): Promise { // Add a remote for the fork const remoteUrl = utils.getRemoteUrl( baseRemote.protocol, + baseRemote.hostname, branchRepository ) await git.exec(['remote', 'add', 'fork', remoteUrl]) diff --git a/src/utils.ts b/src/utils.ts index 433c0beb2..b98f8fa96 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -32,6 +32,7 @@ export function getRepoPath(relativePath?: string): string { } interface RemoteDetail { + hostname: string protocol: string repository: string } @@ -46,18 +47,18 @@ export function getRemoteDetail(remoteUrl: string): RemoteDetail { throw new Error('Could not parse GitHub Server name') } + const hostname = githubServerMatch[1] + const httpsUrlPattern = new RegExp( - '^https?://.*@?' + githubServerMatch[1] + '/(.+/.+?)(\\.git)?$', - 'i' - ) - const sshUrlPattern = new RegExp( - '^git@' + githubServerMatch[1] + ':(.+/.+)\\.git$', + '^https?://.*@?' + hostname + '/(.+/.+?)(\\.git)?$', 'i' ) + const sshUrlPattern = new RegExp('^git@' + hostname + ':(.+/.+)\\.git$', 'i') const httpsMatch = remoteUrl.match(httpsUrlPattern) if (httpsMatch) { return { + hostname, protocol: 'HTTPS', repository: httpsMatch[1] } @@ -66,6 +67,7 @@ export function getRemoteDetail(remoteUrl: string): RemoteDetail { const sshMatch = remoteUrl.match(sshUrlPattern) if (sshMatch) { return { + hostname, protocol: 'SSH', repository: sshMatch[1] } @@ -76,10 +78,14 @@ export function getRemoteDetail(remoteUrl: string): RemoteDetail { ) } -export function getRemoteUrl(protocol: string, repository: string): string { +export function getRemoteUrl( + protocol: string, + hostname: string, + repository: string +): string { return protocol == 'HTTPS' - ? `https://github.com/${repository}` - : `git@github.com:${repository}.git` + ? `https://${hostname}/${repository}` + : `git@${hostname}:${repository}.git` } export function secondsSinceEpoch(): number {