From b89523105cf04298f1f44858a02cc76c6c5dd0a5 Mon Sep 17 00:00:00 2001 From: Pierre Vanduynslager Date: Wed, 8 Aug 2018 11:45:08 -0400 Subject: [PATCH] fix: do not convert ssh `repositoryUrl` to https --- lib/get-git-auth-url.js | 12 ++++++++++-- test/get-git-auth-url.test.js | 13 +++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/get-git-auth-url.js b/lib/get-git-auth-url.js index 863879ba..a51ee9ed 100644 --- a/lib/get-git-auth-url.js +++ b/lib/get-git-auth-url.js @@ -47,10 +47,18 @@ module.exports = async ({cwd, env, options: {repositoryUrl, branch}}) => { const envVar = Object.keys(GIT_TOKENS).find(envVar => !isUndefined(env[envVar])); const gitCredentials = `${GIT_TOKENS[envVar] || ''}${env[envVar] || ''}`; const {protocols, ...parsed} = gitUrlParse(repositoryUrl); - const protocol = protocols.includes('https') ? 'https' : protocols.includes('http') ? 'http' : 'https'; + const protocol = protocols.includes('https') + ? 'https' + : protocols.includes('http') + ? 'http' + : protocols.includes('ssh') + ? 'ssh' + : 'https'; // If credentials are set via anvironment variables, convert the URL to http/https and add basic auth, otherwise return `repositoryUrl` as is - return gitCredentials ? {...parsed, protocols: [protocol], user: gitCredentials}.toString(protocol) : repositoryUrl; + return gitCredentials && ['https', 'http'].includes(protocol) + ? {...parsed, protocols: [protocol], user: gitCredentials}.toString(protocol) + : repositoryUrl; } return repositoryUrl; diff --git a/test/get-git-auth-url.test.js b/test/get-git-auth-url.test.js index e0c4828a..f71265e9 100644 --- a/test/get-git-auth-url.test.js +++ b/test/get-git-auth-url.test.js @@ -265,3 +265,16 @@ test('Do not add git credential to repositoryUrl if push is allowed', async t => repositoryUrl ); }); + +test('Do not add git credentials if 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/owner/repo.git'}, + }), + 'ssh://git@host.null/owner/repo.git' + ); +});