Skip to content

Commit

Permalink
fix: support github server url for pushing to fork (peter-evans#1318)
Browse files Browse the repository at this point in the history
* feat: support github server url for pushing to fork (peter-evans#1315)

Co-authored-by: Kevin Zhu <[email protected]>

* fix: code formatting

* test: fix tests for getRemoteUrl

Co-authored-by: MildC <[email protected]>
Co-authored-by: Kevin Zhu <[email protected]>
  • Loading branch information
3 people authored Nov 24, 2022
1 parent d7db273 commit 331d02c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
21 changes: 19 additions & 2 deletions __test__/utils.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('[email protected]: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 () => {
Expand Down
15 changes: 9 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -1247,29 +1247,32 @@ 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]
};
}
const sshMatch = remoteUrl.match(sshUrlPattern);
if (sshMatch) {
return {
hostname,
protocol: 'SSH',
repository: sshMatch[1]
};
}
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() {
Expand Down
1 change: 1 addition & 0 deletions src/create-pull-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
// Add a remote for the fork
const remoteUrl = utils.getRemoteUrl(
baseRemote.protocol,
baseRemote.hostname,
branchRepository
)
await git.exec(['remote', 'add', 'fork', remoteUrl])
Expand Down
22 changes: 14 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function getRepoPath(relativePath?: string): string {
}

interface RemoteDetail {
hostname: string
protocol: string
repository: string
}
Expand All @@ -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]
}
Expand All @@ -66,6 +67,7 @@ export function getRemoteDetail(remoteUrl: string): RemoteDetail {
const sshMatch = remoteUrl.match(sshUrlPattern)
if (sshMatch) {
return {
hostname,
protocol: 'SSH',
repository: sshMatch[1]
}
Expand All @@ -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 {
Expand Down

0 comments on commit 331d02c

Please sign in to comment.