Skip to content

Commit 657243a

Browse files
authored
chore: handle git@github.com url format (#153)
1 parent ccb29fb commit 657243a

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

pytest_mergify/utils.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,20 @@ def get_ci_provider() -> typing.Optional[CIProviderT]:
3636

3737

3838
def get_repository_name_from_url(repository_url: str) -> typing.Optional[str]:
39+
# Handle SSH Git URLs like git@github.com:owner/repo.git
3940
if match := re.match(
40-
r"(https?://[\w.-]+/)?(?P<full_name>[\w.-]+/[\w.-]+)/?$",
41+
r"git@[\w.-]+:(?P<full_name>[\w.-]+/[\w.-]+)(?:\.git)?/?$",
42+
repository_url,
43+
):
44+
full_name = match.group("full_name")
45+
# Remove .git suffix if present
46+
if full_name.endswith(".git"):
47+
full_name = full_name[:-4]
48+
return full_name
49+
50+
# Handle HTTPS/HTTP URLs like https://github.com/owner/repo (with optional port)
51+
if match := re.match(
52+
r"(https?://[\w.-]+(?::\d+)?/)?(?P<full_name>[\w.-]+/[\w.-]+)/?$",
4153
repository_url,
4254
):
4355
return match.group("full_name")

tests/test_utils.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import pytest
2+
3+
from pytest_mergify.utils import get_repository_name_from_url
4+
5+
6+
@pytest.mark.parametrize(
7+
"url,expected",
8+
[
9+
("https://github.com/owner/repo", "owner/repo"),
10+
("https://github.com/owner/repo/", "owner/repo"),
11+
("http://github.com/owner/repo", "owner/repo"),
12+
("https://gitlab.com/owner/repo", "owner/repo"),
13+
("https://git.example.com/owner/repo", "owner/repo"),
14+
("owner/repo", "owner/repo"),
15+
("https://github.com/my-org.name/my-repo.name", "my-org.name/my-repo.name"),
16+
("https://git.example.com:8080/owner/repo", "owner/repo"),
17+
("https://github.com/owner123/repo456", "owner123/repo456"),
18+
("git@github.com:owner/repo.git", "owner/repo"),
19+
("git@github.com:owner/repo", "owner/repo"),
20+
("git@gitlab.com:owner/repo.git", "owner/repo"),
21+
(
22+
"git@git.example.com:my-org.name/my-repo.name.git",
23+
"my-org.name/my-repo.name",
24+
),
25+
("git@bitbucket.org:owner123/repo456.git", "owner123/repo456"),
26+
],
27+
)
28+
def test_get_repository_name_from_url_valid(url: str, expected: str) -> None:
29+
"""Test valid URL formats that should extract repository names."""
30+
result = get_repository_name_from_url(url)
31+
assert result == expected
32+
33+
34+
@pytest.mark.parametrize(
35+
"url",
36+
[
37+
"https://github.com/owner/repo/issues",
38+
"https://github.com/owner",
39+
"",
40+
"not-a-url",
41+
"https://github.com/owner/repo?tab=readme",
42+
],
43+
)
44+
def test_get_repository_name_from_url_invalid(url: str) -> None:
45+
"""Test invalid URL formats that should return None."""
46+
result = get_repository_name_from_url(url)
47+
assert result is None

0 commit comments

Comments
 (0)