Skip to content

Commit

Permalink
fix: ignore custom port when converting ssh repo URL to https
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Oct 18, 2019
1 parent dc19dfa commit 4af8548
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/get-git-auth-url.js
Expand Up @@ -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',
});
}
Expand Down
26 changes: 26 additions & 0 deletions test/get-git-auth-url.test.js
Expand Up @@ -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();

Expand All @@ -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();

Expand Down

0 comments on commit 4af8548

Please sign in to comment.