Skip to content

Commit

Permalink
Ensure bitbucket always ends with .git
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriksm committed Nov 19, 2024
1 parent 78495fa commit 24ef42c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
40 changes: 20 additions & 20 deletions src/ToCloneUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,8 @@ public static function fromRepoAndToken(string $repo, string $authToken) : strin
if (!empty($repo_parsed)) {
switch ($repo_parsed['_protocol']) {
case '[email protected]':
$repo_path = sprintf('https://x-token-auth:%[email protected]/%s', $authToken, $repo_parsed['path']);
if (strlen($authToken) < 50 && strpos($authToken, ':') !== false) {
$repo_path = sprintf(
'https://%[email protected]/%s',
$authToken,
$repo_parsed['path']
);
}
$path = sprintf('/%s', $repo_parsed['path']);
$repo_path = self::replaceForBitbucket($authToken, $path);
$has_replaced = true;
break;

Expand Down Expand Up @@ -58,18 +52,7 @@ public static function fromRepoAndToken(string $repo, string $authToken) : strin

case 'www.bitbucket.org':
case 'bitbucket.org':
$repo_path = sprintf(
'https://x-token-auth:%[email protected]%s',
$authToken,
$repo_parsed["path"]
);
if (strlen($authToken) < 50 && strpos($authToken, ':') !== false) {
$repo_path = sprintf(
'https://%[email protected]%s',
$authToken,
$repo_parsed['path']
);
}
$repo_path = self::replaceForBitbucket($authToken, $repo_parsed['path']);
break;

default:
Expand Down Expand Up @@ -114,4 +97,21 @@ public static function fromRepoAndToken(string $repo, string $authToken) : strin
}
return $repo_path;
}

private static function replaceForBitbucket(string $authToken, string $path)
{
$repo_path = sprintf('https://x-token-auth:%[email protected]%s', $authToken, $path);
if (strlen($authToken) < 50 && strpos($authToken, ':') !== false) {
$repo_path = sprintf(
'https://%[email protected]%s',
$authToken,
$path
);
}
// We also want to ensure it ends with .git.
if (substr($repo_path, -4) !== '.git') {
$repo_path .= '.git';
}
return $repo_path;
}
}
10 changes: 10 additions & 0 deletions tests/UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public static function provideAllCases() : array
'mytoken',
'https://x-token-auth:[email protected]/user/repo.git',
],
[
'[email protected]:user/repo',
'mytoken',
'https://x-token-auth:[email protected]/user/repo.git',
],
[
'[email protected]:user/repo.git',
'user:mytoken',
Expand Down Expand Up @@ -69,6 +74,11 @@ public static function provideAllCases() : array
'token123',
'https://x-token-auth:[email protected]/user/repo.git',
],
[
'https://www.bitbucket.org/user/repo',
'token123',
'https://x-token-auth:[email protected]/user/repo.git',
],
[
'https://www.bitbucket.org/user/repo.git',
'user:token123',
Expand Down

0 comments on commit 24ef42c

Please sign in to comment.