1+ import logging
12import re
3+ from enum import Enum
24from urllib .parse import urlparse
35
46slug_regex = re .compile (r"[^/\s]+\/[^/\s]+$" )
57
8+ logger = logging .getLogger ("codecovcli" )
9+
10+
11+ class GitService (Enum ):
12+ GITHUB = "github"
13+ GITLAB = "gitlab"
14+ BITBUCKET = "bitbucket"
15+ GITHUB_ENTERPRISE = "github_enterprise"
16+ GITLAB_ENTERPRISE = "gitlab_enterprise"
17+ BITBUCKET_SERVER = "bitbucket_server"
18+
619
720def parse_slug (remote_repo_url : str ):
821 """
@@ -29,3 +42,43 @@ def parse_slug(remote_repo_url: str):
2942 return None
3043
3144 return path_to_parse
45+
46+
47+ def parse_git_service (remote_repo_url : str ):
48+ """
49+ Extracts git service from git remote urls. returns None if the url is invalid
50+
51+ Possible cases we're considering:
52+ - https://github.com/codecov/codecov-cli.git returns github
53+ - [email protected] :codecov/codecov-cli.git returns github 54+ - https://[email protected] /namespace-codecov/first_repo.git returns bitbucket 55+ """
56+ services = [service .value for service in GitService ]
57+ parsed_url = urlparse (remote_repo_url )
58+ service = None
59+
60+ if remote_repo_url .startswith ("https://" ):
61+ netloc = parsed_url .netloc
62+ if "@" in netloc :
63+ netloc = netloc .split ("@" , 1 )[1 ]
64+ if "." in netloc :
65+ netloc = netloc .split ("." , 1 )[0 ]
66+ service = netloc
67+ elif remote_repo_url .startswith ("git@" ):
68+ path = parsed_url .path
69+ if "@" in path :
70+ path = path .split ("@" , 1 )[1 ]
71+ if ":" in path :
72+ path = path .split (":" , 1 )[0 ]
73+ if "." in path :
74+ path = path .split ("." , 1 )[0 ]
75+ service = path
76+
77+ if service in services :
78+ return service
79+ else :
80+ logger .warning (
81+ f"Service not found: { service } . Possible services are { services } " ,
82+ extra = dict (remote_repo_url = remote_repo_url ),
83+ )
84+ return None
0 commit comments