From 77a75f072bc257b27904408dbea5ae5ccae2b6ab Mon Sep 17 00:00:00 2001 From: Arthur Chaloin Date: Wed, 28 Oct 2020 23:53:03 +0100 Subject: [PATCH] fix: don't parse port as part of the path in repository URLs (#1671) --- lib/get-git-auth-url.js | 6 +++--- test/get-git-auth-url.test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/get-git-auth-url.js b/lib/get-git-auth-url.js index 2ebb84a10b..c6c3e8b8e9 100644 --- a/lib/get-git-auth-url.js +++ b/lib/get-git-auth-url.js @@ -53,10 +53,10 @@ module.exports = async ({cwd, env, branch, options: {repositoryUrl}}) => { 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 [match, auth, host, basePort, path] = + /^(?!.+:\/\/)(?:(?.*)@)?(?.*?):(?\d+)?:?\/?(?.*)$/.exec(repositoryUrl) || []; const {port, hostname, ...parsed} = parse( - match ? `ssh://${auth ? `${auth}@` : ''}${host}/${path}` : repositoryUrl + match ? `ssh://${auth ? `${auth}@` : ''}${host}${basePort ? `:${basePort}` : ''}/${path}` : repositoryUrl ); return format({ diff --git a/test/get-git-auth-url.test.js b/test/get-git-auth-url.test.js index 67ae9352d0..a5711785fb 100644 --- a/test/get-git-auth-url.test.js +++ b/test/get-git-auth-url.test.js @@ -133,6 +133,32 @@ test('Return the "https" formatted URL if "gitCredentials" is defined and reposi ); }); +test('Return the "https" formatted URL if "gitCredentials" is defined and repositoryUrl is a "git" URL without user and with a custom port', async (t) => { + const {cwd} = await gitRepo(); + + t.is( + await getAuthUrl({ + cwd, + env: {...env, GIT_CREDENTIALS: 'user:pass'}, + options: {branch: 'master', repositoryUrl: 'host.null:6666:owner/repo.git'}, + }), + 'https://user:pass@host.null:6666/owner/repo.git' + ); +}); + +test('Return the "https" formatted URL if "gitCredentials" is defined and repositoryUrl is a "git" URL without user and with a custom port followed by a slash', async (t) => { + const {cwd} = await gitRepo(); + + t.is( + await getAuthUrl({ + cwd, + env: {...env, GIT_CREDENTIALS: 'user:pass'}, + options: {branch: 'master', repositoryUrl: 'host.null:6666:/owner/repo.git'}, + }), + 'https://user:pass@host.null:6666/owner/repo.git' + ); +}); + test('Return the "https" formatted URL if "gitCredentials" is defined and repositoryUrl is a "https" URL', async (t) => { const {cwd} = await gitRepo();