11import re
22from dataclasses import dataclass
3+ from typing import List
34from urllib .error import HTTPError , URLError
45from retry import retry
56from cloudshell .iac .terraform .downloaders .base_git_downloader import GitScriptDownloaderBase
67from cloudshell .iac .terraform .services .gitlab_api_handler import GitlabApiHandler
8+ from urllib .parse import unquote
79
810
911@dataclass
@@ -16,7 +18,7 @@ class CommonGitLabUrlData:
1618
1719
1820@dataclass
19- class GitLabRawUrlData (CommonGitLabUrlData ):
21+ class GitLabBrowserUrlData (CommonGitLabUrlData ):
2022 gitlab_user : str
2123 project_name : str
2224
@@ -28,7 +30,7 @@ class GitLabApiUrlData(CommonGitLabUrlData):
2830 api_endpoint : str
2931
3032
31- def extract_data_from_raw_url (url ) -> GitLabRawUrlData :
33+ def extract_data_from_browser_url (url ) -> GitLabBrowserUrlData :
3234 """
3335 Take api style url and extract data
3436 Sample Raw Browser url: "http://192.168.85.26/quali_natti/terraformstuff/-/tree/test-branch/rds/project1"
@@ -42,13 +44,24 @@ def extract_data_from_raw_url(url) -> GitLabRawUrlData:
4244 raise ValueError (f"No GitLab URL Data found in RAW url '{ url } '" )
4345
4446 groups = match .groupdict ()
45- return GitLabRawUrlData (protocol = groups ['protocol' ],
46- domain = groups ['domain' ],
47- gitlab_user = groups ['user' ],
48- project_name = groups ['project' ],
49- sha = groups ['sha' ],
50- path = groups ['path' ],
51- full_url = url )
47+ return GitLabBrowserUrlData (protocol = groups ['protocol' ],
48+ domain = groups ['domain' ],
49+ gitlab_user = groups ['user' ],
50+ project_name = groups ['project' ],
51+ sha = groups ['sha' ],
52+ path = groups ['path' ],
53+ full_url = url )
54+
55+
56+ def get_query_param_val (param_key : str , params_list : List [List [str ]]) -> str :
57+ """
58+ look for target param in 2D list of key pair values
59+ [[k1,v1],[k2,v2]]
60+ if not found return empty string
61+ """
62+ target_param_search = [x for x in params_list if x [0 ] == param_key ]
63+ param_val = target_param_search [0 ][1 ] if target_param_search else ""
64+ return param_val
5265
5366
5467def extract_data_from_api_url (url ) -> GitLabApiUrlData :
@@ -68,26 +81,23 @@ def extract_data_from_api_url(url) -> GitLabApiUrlData:
6881 groups = match .groupdict ()
6982 query_params = groups ['params' ]
7083
71- # remove the leading '?'
84+ # remove the leading '?' of the query param string
7285 query_params = query_params .split ("?" )[- 1 ]
7386
7487 # split into 2D list [[k1,v1],[k2,v2]]
7588 params_list = [x .split ("=" ) for x in query_params .split ("&" )]
7689
7790 # search for target params
78- path_param_search = [x for x in params_list if x [0 ] == "path" ]
79- sha_param_search = [x for x in params_list if x [0 ] == "sha" ]
80- ref_param_search = [x for x in params_list if x [0 ] == "ref" ]
81- sha = sha_param_search [0 ][1 ] if sha_param_search else ""
82- path = path_param_search [0 ][1 ] if path_param_search else ""
83- ref = ref_param_search [0 ][1 ] if ref_param_search else ""
91+ path = get_query_param_val ("path" , params_list )
92+ sha = get_query_param_val ("sha" , params_list )
93+ ref = get_query_param_val ("ref" , params_list )
8494
8595 # take sha param if passed, otherwise use the ref
8696 sha = sha if sha else ref
8797
88- # url encoded path not necessary for archive api
89- path = path . replace ( "%2F" , "/" ). replace ( "%2D" , "-" )
90- sha = sha . replace ( "%2D" , "-" )
98+ # url encoded path not necessary
99+ path = unquote ( path )
100+ sha = unquote ( sha )
91101 return GitLabApiUrlData (protocol = groups ['protocol' ],
92102 domain = groups ['domain' ],
93103 api_version = groups ['api_version' ],
@@ -127,7 +137,7 @@ def download_repo(self, url: str, token: str, branch: str = "") -> str:
127137 if is_api_url :
128138 url_data = extract_data_from_api_url (url )
129139 else :
130- url_data = extract_data_from_raw_url (url )
140+ url_data = extract_data_from_browser_url (url )
131141
132142 # allow service branch attr to override the url defined sha
133143 sha = branch if branch else url_data .sha
0 commit comments