From 4af854836668f5cfea9a176b006f416b965ee84c Mon Sep 17 00:00:00 2001 From: Pierre Vanduynslager Date: Fri, 18 Oct 2019 14:26:13 -0400 Subject: [PATCH] fix: ignore custom port when converting ssh repo URL to https --- lib/get-git-auth-url.js | 7 ++++++- test/get-git-auth-url.test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/get-git-auth-url.js b/lib/get-git-auth-url.js index cc7a354842..c0374f7c46 100644 --- a/lib/get-git-auth-url.js +++ b/lib/get-git-auth-url.js @@ -48,9 +48,14 @@ module.exports = async ({cwd, env, options: {repositoryUrl, branch}}) => { if (gitCredentials) { // If credentials are set via environment variables, convert the URL to http/https and add basic auth, otherwise return `repositoryUrl` as is const [match, auth, host, path] = /^(?!.+:\/\/)(?:(.*)@)?(.*?):(.*)$/.exec(repositoryUrl) || []; + const {port, hostname, ...parsed} = parse( + match ? `ssh://${auth ? `${auth}@` : ''}${host}/${path}` : repositoryUrl + ); + return format({ - ...parse(match ? `ssh://${auth ? `${auth}@` : ''}${host}/${path}` : repositoryUrl), + ...parsed, auth: gitCredentials, + host: `${hostname}${protocol === 'ssh:' ? '' : port ? `:${port}` : ''}`, protocol: protocol && /http[^s]/.test(protocol) ? 'http' : 'https', }); } diff --git a/test/get-git-auth-url.test.js b/test/get-git-auth-url.test.js index 8312d2a083..b0db1cd137 100644 --- a/test/get-git-auth-url.test.js +++ b/test/get-git-auth-url.test.js @@ -140,6 +140,19 @@ test('Return the "http" formatted URL if "gitCredentials" is defined and reposit ); }); +test('Return the "http" formatted URL if "gitCredentials" is defined and repositoryUrl is a "http" URL with custom port', async t => { + const {cwd} = await gitRepo(); + + t.is( + await getAuthUrl({ + cwd, + env: {...env, GIT_CREDENTIALS: 'user:pass'}, + options: {branch: 'master', repositoryUrl: 'http://host.null:8080/owner/repo.git'}, + }), + 'http://user:pass@host.null:8080/owner/repo.git' + ); +}); + test('Return the "https" formatted URL if "gitCredentials" is defined and repositoryUrl is a "git+https" URL', async t => { const {cwd} = await gitRepo(); @@ -166,6 +179,19 @@ test('Return the "http" formatted URL if "gitCredentials" is defined and reposit ); }); +test('Return the "http" formatted URL if "gitCredentials" is defined and repositoryUrl is a "ssh" URL', async t => { + const {cwd} = await gitRepo(); + + t.is( + await getAuthUrl({ + cwd, + env: {...env, GIT_CREDENTIALS: 'user:pass'}, + options: {branch: 'master', repositoryUrl: 'ssh://git@host.null:2222/owner/repo.git'}, + }), + 'https://user:pass@host.null/owner/repo.git' + ); +}); + test('Return the "https" formatted URL if "gitCredentials" is defined with "GH_TOKEN"', async t => { const {cwd} = await gitRepo();