Skip to content

Commit 331d02c

Browse files
peter-evansMildCkevinzhu-sa
authored
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 <kevin.zhu@sap.com> * fix: code formatting * test: fix tests for getRemoteUrl Co-authored-by: MildC <kevin.xizhu@gmail.com> Co-authored-by: Kevin Zhu <kevin.zhu@sap.com>
1 parent d7db273 commit 331d02c

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

__test__/utils.unit.test.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,28 @@ describe('utils tests', () => {
9090
})
9191

9292
test('getRemoteUrl successfully returns remote URLs', async () => {
93-
const url1 = utils.getRemoteUrl('HTTPS', 'peter-evans/create-pull-request')
93+
const url1 = utils.getRemoteUrl(
94+
'HTTPS',
95+
'github.com',
96+
'peter-evans/create-pull-request'
97+
)
9498
expect(url1).toEqual('https://github.com/peter-evans/create-pull-request')
9599

96-
const url2 = utils.getRemoteUrl('SSH', 'peter-evans/create-pull-request')
100+
const url2 = utils.getRemoteUrl(
101+
'SSH',
102+
'github.com',
103+
'peter-evans/create-pull-request'
104+
)
97105
expect(url2).toEqual('git@github.com:peter-evans/create-pull-request.git')
106+
107+
const url3 = utils.getRemoteUrl(
108+
'HTTPS',
109+
'mygithubserver.com',
110+
'peter-evans/create-pull-request'
111+
)
112+
expect(url3).toEqual(
113+
'https://mygithubserver.com/peter-evans/create-pull-request'
114+
)
98115
})
99116

100117
test('secondsSinceEpoch returns the number of seconds since the Epoch', async () => {

dist/index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ function createPullRequest(inputs) {
339339
throw new Error(`Repository '${branchRepository}' is not a fork of '${baseRemote.repository}'. Unable to continue.`);
340340
}
341341
// Add a remote for the fork
342-
const remoteUrl = utils.getRemoteUrl(baseRemote.protocol, branchRepository);
342+
const remoteUrl = utils.getRemoteUrl(baseRemote.protocol, baseRemote.hostname, branchRepository);
343343
yield git.exec(['remote', 'add', 'fork', remoteUrl]);
344344
}
345345
core.endGroup();
@@ -1247,29 +1247,32 @@ function getRemoteDetail(remoteUrl) {
12471247
if (!githubServerMatch) {
12481248
throw new Error('Could not parse GitHub Server name');
12491249
}
1250-
const httpsUrlPattern = new RegExp('^https?://.*@?' + githubServerMatch[1] + '/(.+/.+?)(\\.git)?$', 'i');
1251-
const sshUrlPattern = new RegExp('^git@' + githubServerMatch[1] + ':(.+/.+)\\.git$', 'i');
1250+
const hostname = githubServerMatch[1];
1251+
const httpsUrlPattern = new RegExp('^https?://.*@?' + hostname + '/(.+/.+?)(\\.git)?$', 'i');
1252+
const sshUrlPattern = new RegExp('^git@' + hostname + ':(.+/.+)\\.git$', 'i');
12521253
const httpsMatch = remoteUrl.match(httpsUrlPattern);
12531254
if (httpsMatch) {
12541255
return {
1256+
hostname,
12551257
protocol: 'HTTPS',
12561258
repository: httpsMatch[1]
12571259
};
12581260
}
12591261
const sshMatch = remoteUrl.match(sshUrlPattern);
12601262
if (sshMatch) {
12611263
return {
1264+
hostname,
12621265
protocol: 'SSH',
12631266
repository: sshMatch[1]
12641267
};
12651268
}
12661269
throw new Error(`The format of '${remoteUrl}' is not a valid GitHub repository URL`);
12671270
}
12681271
exports.getRemoteDetail = getRemoteDetail;
1269-
function getRemoteUrl(protocol, repository) {
1272+
function getRemoteUrl(protocol, hostname, repository) {
12701273
return protocol == 'HTTPS'
1271-
? `https://github.com/${repository}`
1272-
: `git@github.com:${repository}.git`;
1274+
? `https://${hostname}/${repository}`
1275+
: `git@${hostname}:${repository}.git`;
12731276
}
12741277
exports.getRemoteUrl = getRemoteUrl;
12751278
function secondsSinceEpoch() {

src/create-pull-request.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
7474
// Add a remote for the fork
7575
const remoteUrl = utils.getRemoteUrl(
7676
baseRemote.protocol,
77+
baseRemote.hostname,
7778
branchRepository
7879
)
7980
await git.exec(['remote', 'add', 'fork', remoteUrl])

src/utils.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function getRepoPath(relativePath?: string): string {
3232
}
3333

3434
interface RemoteDetail {
35+
hostname: string
3536
protocol: string
3637
repository: string
3738
}
@@ -46,18 +47,18 @@ export function getRemoteDetail(remoteUrl: string): RemoteDetail {
4647
throw new Error('Could not parse GitHub Server name')
4748
}
4849

50+
const hostname = githubServerMatch[1]
51+
4952
const httpsUrlPattern = new RegExp(
50-
'^https?://.*@?' + githubServerMatch[1] + '/(.+/.+?)(\\.git)?$',
51-
'i'
52-
)
53-
const sshUrlPattern = new RegExp(
54-
'^git@' + githubServerMatch[1] + ':(.+/.+)\\.git$',
53+
'^https?://.*@?' + hostname + '/(.+/.+?)(\\.git)?$',
5554
'i'
5655
)
56+
const sshUrlPattern = new RegExp('^git@' + hostname + ':(.+/.+)\\.git$', 'i')
5757

5858
const httpsMatch = remoteUrl.match(httpsUrlPattern)
5959
if (httpsMatch) {
6060
return {
61+
hostname,
6162
protocol: 'HTTPS',
6263
repository: httpsMatch[1]
6364
}
@@ -66,6 +67,7 @@ export function getRemoteDetail(remoteUrl: string): RemoteDetail {
6667
const sshMatch = remoteUrl.match(sshUrlPattern)
6768
if (sshMatch) {
6869
return {
70+
hostname,
6971
protocol: 'SSH',
7072
repository: sshMatch[1]
7173
}
@@ -76,10 +78,14 @@ export function getRemoteDetail(remoteUrl: string): RemoteDetail {
7678
)
7779
}
7880

79-
export function getRemoteUrl(protocol: string, repository: string): string {
81+
export function getRemoteUrl(
82+
protocol: string,
83+
hostname: string,
84+
repository: string
85+
): string {
8086
return protocol == 'HTTPS'
81-
? `https://github.com/${repository}`
82-
: `git@github.com:${repository}.git`
87+
? `https://${hostname}/${repository}`
88+
: `git@${hostname}:${repository}.git`
8389
}
8490

8591
export function secondsSinceEpoch(): number {

0 commit comments

Comments
 (0)