Skip to content

Commit 4df1c58

Browse files
chore: add_access_token_to_url (#270)
1 parent 8bdb6b7 commit 4df1c58

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/codegen/git/utils/clone_url.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55
from codegen.git.schemas.repo_config import RepoConfig
66

77

8+
# TODO: move out doesn't belong here
89
def url_to_github(url: str, branch: str) -> str:
910
clone_url = url.removesuffix(".git").replace("[email protected]:", "github.com/")
1011
return f"{clone_url}/blob/{branch}"
1112

1213

1314
def get_clone_url_for_repo_config(repo_config: RepoConfig, github_type: GithubType = GithubType.GithubEnterprise) -> str:
14-
github_url = f"github.com/{repo_config.full_name}.git"
15-
ghe_url = f"github.codegen.app/{repo_config.full_name}.git"
1615
if github_type is GithubType.GithubEnterprise:
17-
return ghe_url
16+
return f"https://github.codegen.app/{repo_config.full_name}.git"
1817
elif github_type is GithubType.Github:
19-
return github_url
18+
return f"https://github.com/{repo_config.full_name}.git"
2019

2120

2221
def get_authenticated_clone_url_for_repo_config(
@@ -25,9 +24,11 @@ def get_authenticated_clone_url_for_repo_config(
2524
) -> str:
2625
git_url = get_clone_url_for_repo_config(repo, github_type)
2726
token = get_token_for_repo_config(repo_config=repo, github_type=github_type)
28-
return f"https://x-access-token:{token}@{git_url}"
27+
return add_access_token_to_url(git_url, token)
2928

3029

31-
def add_token_to_clone_url(clone_url: str, token: str) -> str:
32-
parsed_clone_url = urlparse(clone_url)
33-
return f"{parsed_clone_url.scheme}://x-access-token:{token}@{parsed_clone_url.netloc}{parsed_clone_url.path}"
30+
def add_access_token_to_url(url: str, token: str | None) -> str:
31+
parsed_url = urlparse(url)
32+
scheme = parsed_url.scheme or "https"
33+
token_prefix = f"x-access-token:{token}@" if token else ""
34+
return f"{scheme}://{token_prefix}{parsed_url.netloc}{parsed_url.path}"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
3+
from codegen.git.utils.clone_url import add_access_token_to_url
4+
5+
6+
@pytest.mark.parametrize(
7+
"url, token, expected",
8+
[
9+
("https://github.com/owner/repo.git", "token", "https://x-access-token:[email protected]/owner/repo.git"), # https url with token
10+
("https://github.com/owner/repo.git", None, "https://github.com/owner/repo.git"), # https url without token
11+
("https://github.com/owner/repo.git", "", "https://github.com/owner/repo.git"), # https url with empty token
12+
("github.com/owner/repo", "token", "https://x-access-token:[email protected]/owner/repo"), # scheme missing with token
13+
("github.com/owner/repo", None, "https://github.com/owner/repo"), # scheme missing without token
14+
("github.com/owner/repo", "", "https://github.com/owner/repo"), # scheme missing with empty token
15+
],
16+
)
17+
def test_add_access_token_to_url(url, token, expected):
18+
assert add_access_token_to_url(url, token) == expected

0 commit comments

Comments
 (0)