From 24ef42cb6665eb50149a8fa1d408f3f1b96ab6ea Mon Sep 17 00:00:00 2001 From: "Eirik S. Morland" Date: Tue, 19 Nov 2024 08:57:16 +0100 Subject: [PATCH] Ensure bitbucket always ends with .git --- src/ToCloneUrl.php | 40 ++++++++++++++++++++-------------------- tests/UnitTest.php | 10 ++++++++++ 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/ToCloneUrl.php b/src/ToCloneUrl.php index 0687f04..3d5bd24 100644 --- a/src/ToCloneUrl.php +++ b/src/ToCloneUrl.php @@ -20,14 +20,8 @@ public static function fromRepoAndToken(string $repo, string $authToken) : strin if (!empty($repo_parsed)) { switch ($repo_parsed['_protocol']) { case 'git@bitbucket.org': - $repo_path = sprintf('https://x-token-auth:%s@bitbucket.org/%s', $authToken, $repo_parsed['path']); - if (strlen($authToken) < 50 && strpos($authToken, ':') !== false) { - $repo_path = sprintf( - 'https://%s@bitbucket.org/%s', - $authToken, - $repo_parsed['path'] - ); - } + $path = sprintf('/%s', $repo_parsed['path']); + $repo_path = self::replaceForBitbucket($authToken, $path); $has_replaced = true; break; @@ -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:%s@bitbucket.org%s', - $authToken, - $repo_parsed["path"] - ); - if (strlen($authToken) < 50 && strpos($authToken, ':') !== false) { - $repo_path = sprintf( - 'https://%s@bitbucket.org%s', - $authToken, - $repo_parsed['path'] - ); - } + $repo_path = self::replaceForBitbucket($authToken, $repo_parsed['path']); break; default: @@ -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:%s@bitbucket.org%s', $authToken, $path); + if (strlen($authToken) < 50 && strpos($authToken, ':') !== false) { + $repo_path = sprintf( + 'https://%s@bitbucket.org%s', + $authToken, + $path + ); + } + // We also want to ensure it ends with .git. + if (substr($repo_path, -4) !== '.git') { + $repo_path .= '.git'; + } + return $repo_path; + } } diff --git a/tests/UnitTest.php b/tests/UnitTest.php index c72709f..d9327b3 100644 --- a/tests/UnitTest.php +++ b/tests/UnitTest.php @@ -29,6 +29,11 @@ public static function provideAllCases() : array 'mytoken', 'https://x-token-auth:mytoken@bitbucket.org/user/repo.git', ], + [ + 'git@bitbucket.org:user/repo', + 'mytoken', + 'https://x-token-auth:mytoken@bitbucket.org/user/repo.git', + ], [ 'git@bitbucket.org:user/repo.git', 'user:mytoken', @@ -69,6 +74,11 @@ public static function provideAllCases() : array 'token123', 'https://x-token-auth:token123@bitbucket.org/user/repo.git', ], + [ + 'https://www.bitbucket.org/user/repo', + 'token123', + 'https://x-token-auth:token123@bitbucket.org/user/repo.git', + ], [ 'https://www.bitbucket.org/user/repo.git', 'user:token123',