Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/codegen/git/utils/clone_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("[email protected]:", "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(
Expand All @@ -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}"
18 changes: 18 additions & 0 deletions tests/unit/codegen/git/utils/test_clone_url.py
Original file line number Diff line number Diff line change
@@ -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:[email protected]/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:[email protected]/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
Loading