Skip to content

Commit 768df54

Browse files
author
Nicolas Maître
committed
feat: support SSH remote for AWS CodeCommit repositories
1 parent e650593 commit 768df54

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

pull_request_codecommit/git/remote.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
11
import re
22
from typing import Optional
3+
from enum import Enum
34

45

56
class Remote:
67
"""
78
Understands remote CodeCommit URLs
89
"""
910

11+
class Protocol(Enum):
12+
SSH = 1
13+
HTTPS = 2
14+
1015
def __init__(self, url: str):
1116
self.__url: str = url
1217
self.__region: Optional[str] = ""
1318
self.__profile: Optional[str] = ""
1419
self.__name: str = ""
20+
self.__protocol: Remote.Protocol = (
21+
Remote.Protocol.SSH if url.startswith("ssh://") else Remote.Protocol.HTTPS
22+
)
1523

1624
def __regex(self, pattern: str, index: int = 1) -> Optional[str]:
1725
match = re.search(pattern, self.__url)
1826
return match.group(index) if match else None
1927

2028
@property
2129
def supported(self) -> bool:
22-
return self.__url.startswith("codecommit:") and self.name != ""
30+
return (
31+
self.__protocol == Remote.Protocol.HTTPS
32+
and self.__url.startswith("codecommit:")
33+
or self.__protocol == Remote.Protocol.SSH
34+
and self.__url.startswith("ssh://")
35+
) and self.name != ""
2336

2437
@property
2538
def url(self) -> str:
@@ -28,7 +41,10 @@ def url(self) -> str:
2841
@property
2942
def region(self) -> Optional[str]:
3043
if not self.__region:
31-
self.__region = self.__regex(r"^codecommit::(.*)://")
44+
if self.__protocol == Remote.Protocol.HTTPS:
45+
self.__region = self.__regex(r"^codecommit::(.*)://")
46+
elif self.__protocol == Remote.Protocol.SSH:
47+
self.__region = self.__regex(r"ssh://git-codecommit.(.*).amazonaws.com")
3248

3349
return self.__region
3450

@@ -42,7 +58,10 @@ def profile(self) -> Optional[str]:
4258
@property
4359
def name(self) -> str:
4460
if not self.__name:
45-
name = self.__regex(r"(\/\/|.*@)(.*)$", 2)
61+
if self.__protocol == Remote.Protocol.HTTPS:
62+
name = self.__regex(r"(\/\/|.*@)(.*)$", 2)
63+
elif self.__protocol == Remote.Protocol.SSH:
64+
name = self.__regex(r"/([^/]*)$", 1)
4665

4766
if name:
4867
self.__name = name

0 commit comments

Comments
 (0)