diff --git a/src/codegen/git/utils/clone_url.py b/src/codegen/git/utils/clone_url.py index f53a7e518..d4c446133 100644 --- a/src/codegen/git/utils/clone_url.py +++ b/src/codegen/git/utils/clone_url.py @@ -5,18 +5,17 @@ from codegen.git.schemas.repo_config import RepoConfig +# TODO: move out doesn't belong here def url_to_github(url: str, branch: str) -> str: clone_url = url.removesuffix(".git").replace("git@github.com:", "github.com/") return f"{clone_url}/blob/{branch}" def get_clone_url_for_repo_config(repo_config: RepoConfig, github_type: GithubType = GithubType.GithubEnterprise) -> str: - github_url = f"github.com/{repo_config.full_name}.git" - ghe_url = f"github.codegen.app/{repo_config.full_name}.git" if github_type is GithubType.GithubEnterprise: - return ghe_url + return f"https://github.codegen.app/{repo_config.full_name}.git" elif github_type is GithubType.Github: - return github_url + return f"https://github.com/{repo_config.full_name}.git" def get_authenticated_clone_url_for_repo_config( @@ -25,9 +24,11 @@ def get_authenticated_clone_url_for_repo_config( ) -> str: git_url = get_clone_url_for_repo_config(repo, github_type) token = get_token_for_repo_config(repo_config=repo, github_type=github_type) - return f"https://x-access-token:{token}@{git_url}" + return add_access_token_to_url(git_url, token) -def add_token_to_clone_url(clone_url: str, token: str) -> str: - parsed_clone_url = urlparse(clone_url) - return f"{parsed_clone_url.scheme}://x-access-token:{token}@{parsed_clone_url.netloc}{parsed_clone_url.path}" +def add_access_token_to_url(url: str, token: str | None) -> str: + parsed_url = urlparse(url) + scheme = parsed_url.scheme or "https" + token_prefix = f"x-access-token:{token}@" if token else "" + return f"{scheme}://{token_prefix}{parsed_url.netloc}{parsed_url.path}" diff --git a/tests/unit/codegen/git/utils/test_clone_url.py b/tests/unit/codegen/git/utils/test_clone_url.py new file mode 100644 index 000000000..07f4d174a --- /dev/null +++ b/tests/unit/codegen/git/utils/test_clone_url.py @@ -0,0 +1,18 @@ +import pytest + +from codegen.git.utils.clone_url import add_access_token_to_url + + +@pytest.mark.parametrize( + "url, token, expected", + [ + ("https://github.com/owner/repo.git", "token", "https://x-access-token:token@github.com/owner/repo.git"), # https url with token + ("https://github.com/owner/repo.git", None, "https://github.com/owner/repo.git"), # https url without token + ("https://github.com/owner/repo.git", "", "https://github.com/owner/repo.git"), # https url with empty token + ("github.com/owner/repo", "token", "https://x-access-token:token@github.com/owner/repo"), # scheme missing with token + ("github.com/owner/repo", None, "https://github.com/owner/repo"), # scheme missing without token + ("github.com/owner/repo", "", "https://github.com/owner/repo"), # scheme missing with empty token + ], +) +def test_add_access_token_to_url(url, token, expected): + assert add_access_token_to_url(url, token) == expected