-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathremote.py
More file actions
79 lines (63 loc) · 2.3 KB
/
remote.py
File metadata and controls
79 lines (63 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import re
from typing import Optional
from enum import Enum
class Remote:
"""
Understands remote CodeCommit URLs
"""
class Protocol(Enum):
SSH = 1
HTTPS = 2
def __init__(self, url: str):
self.__url: str = url
self.__region: Optional[str] = ""
self.__profile: Optional[str] = ""
self.__name: str = ""
self.__protocol: Remote.Protocol = (
Remote.Protocol.SSH if url.startswith("ssh://") else Remote.Protocol.HTTPS
)
def __regex(self, pattern: str, index: int = 1) -> Optional[str]:
match = re.search(pattern, self.__url)
return match.group(index) if match else None
@property
def supported(self) -> bool:
return (
self.__protocol == Remote.Protocol.HTTPS
and self.__url.startswith("codecommit:")
or self.__protocol == Remote.Protocol.SSH
and self.__url.startswith("ssh://")
) and self.name != ""
@property
def url(self) -> str:
return self.__url
@property
def region(self) -> Optional[str]:
if not self.__region:
if self.__protocol == Remote.Protocol.HTTPS:
self.__region = self.__regex(r"^codecommit::(.*)://")
elif self.__protocol == Remote.Protocol.SSH:
self.__region = self.__regex(r"ssh://git-codecommit.(.*).amazonaws.com")
return self.__region
@property
def profile(self) -> Optional[str]:
if self.__profile == "":
self.__profile = self.__regex(r"//(.*)@")
return self.__profile
@property
def name(self) -> str:
if not self.__name:
if self.__protocol == Remote.Protocol.HTTPS:
name = self.__regex(r"(\/\/|.*@)(.*)$", 2)
elif self.__protocol == Remote.Protocol.SSH:
name = self.__regex(r"/([^/]*)$", 1)
if name:
self.__name = name
return self.__name
@property
def codecommit_url(self) -> str:
return f"https://{self.region}.console.aws.amazon.com/codesuite/codecommit/repositories/{self.name}"
def pull_request_url(self, pull_request_id: int) -> str:
return (
self.codecommit_url
+ f"/pull-requests/{pull_request_id}/details?region={self.region}"
)