|
| 1 | +import re |
| 2 | +from dataclasses import dataclass |
| 3 | +from typing import List |
| 4 | +from urllib.error import HTTPError, URLError |
| 5 | +from retry import retry |
| 6 | +from cloudshell.iac.terraform.downloaders.base_git_downloader import GitScriptDownloaderBase |
| 7 | +from cloudshell.iac.terraform.services.gitlab_api_handler import GitlabApiHandler |
| 8 | +from urllib.parse import unquote |
| 9 | + |
| 10 | + |
| 11 | +@dataclass |
| 12 | +class CommonGitLabUrlData: |
| 13 | + protocol: str |
| 14 | + domain: str |
| 15 | + path: str |
| 16 | + full_url: str |
| 17 | + sha: str |
| 18 | + |
| 19 | + |
| 20 | +@dataclass |
| 21 | +class GitLabBrowserUrlData(CommonGitLabUrlData): |
| 22 | + gitlab_user: str |
| 23 | + project_name: str |
| 24 | + |
| 25 | + |
| 26 | +@dataclass |
| 27 | +class GitLabApiUrlData(CommonGitLabUrlData): |
| 28 | + api_version: str |
| 29 | + project_id: int |
| 30 | + api_endpoint: str |
| 31 | + |
| 32 | + |
| 33 | +def extract_data_from_browser_url(url) -> GitLabBrowserUrlData: |
| 34 | + """ |
| 35 | + Take api style url and extract data |
| 36 | + Sample Raw Browser url: "http://192.168.85.26/quali_natti/terraformstuff/-/tree/test-branch/rds/project1" |
| 37 | + 'sha' can be branch or commit id |
| 38 | + """ |
| 39 | + pattern = (r'^(?P<protocol>https?)://(?P<domain>[^/]+)/(?P<user>[^/]+)/(?P<project>[^/]+)/-/tree/' |
| 40 | + r'(?P<sha>[^/]+)/(?P<path>.*)?$') |
| 41 | + |
| 42 | + match = re.match(pattern, url) |
| 43 | + if not match: |
| 44 | + raise ValueError(f"No GitLab URL Data found in RAW url '{url}'") |
| 45 | + |
| 46 | + groups = match.groupdict() |
| 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 |
| 65 | + |
| 66 | + |
| 67 | +def extract_data_from_api_url(url) -> GitLabApiUrlData: |
| 68 | + """ |
| 69 | + Take user style url and extract data |
| 70 | + supports url-encoded style paths as well |
| 71 | + Sample Api url: "http://192.168.85.26/api/v4/projects/2/repository/archive.zip?path=rds" |
| 72 | + """ |
| 73 | + pattern = (r'^(?P<protocol>https?)://(?P<domain>[^/]+)(?P<api_version>/api/v\d+)?' |
| 74 | + r'(?P<api_endpoint>/projects/(?P<project_id>\d+)/repository/archive\.zip)' |
| 75 | + r'(?P<params>\?([^&]+=[^&]+&)*[^&]+=[^&]+$)') |
| 76 | + |
| 77 | + match = re.match(pattern, url) |
| 78 | + if not match: |
| 79 | + raise ValueError(f"No GitLab url data found in API url '{url}'") |
| 80 | + |
| 81 | + groups = match.groupdict() |
| 82 | + query_params = groups['params'] |
| 83 | + |
| 84 | + # remove the leading '?' of the query param string |
| 85 | + query_params = query_params.split("?")[-1] |
| 86 | + |
| 87 | + # split into 2D list [[k1,v1],[k2,v2]] |
| 88 | + params_list = [x.split("=") for x in query_params.split("&")] |
| 89 | + |
| 90 | + # search for target params |
| 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) |
| 94 | + |
| 95 | + # take sha param if passed, otherwise use the ref |
| 96 | + sha = sha if sha else ref |
| 97 | + |
| 98 | + # url encoded path not necessary |
| 99 | + path = unquote(path) |
| 100 | + sha = unquote(sha) |
| 101 | + return GitLabApiUrlData(protocol=groups['protocol'], |
| 102 | + domain=groups['domain'], |
| 103 | + api_version=groups['api_version'], |
| 104 | + project_id=groups['project_id'], |
| 105 | + api_endpoint=groups['api_endpoint'], |
| 106 | + path=path, |
| 107 | + sha=sha, |
| 108 | + full_url=url) |
| 109 | + |
| 110 | + |
| 111 | +def is_gitlab_api_url(url: str) -> bool: |
| 112 | + """ |
| 113 | + check if is api endpoint |
| 114 | + Sample Api url: "http://192.168.85.26/api/v4/projects/2/repository/archive.zip?path=rds" |
| 115 | + """ |
| 116 | + pattern = r'^(?P<protocol>https?)://(?P<domain>[^/]+)(?P<api_version>/api/v\d+)?(?P<api_endpoint>/[^\s]+)*/?$' |
| 117 | + match = re.match(pattern, url) |
| 118 | + if not match: |
| 119 | + return False |
| 120 | + |
| 121 | + groups = match.groupdict() |
| 122 | + api_version = groups['api_version'] # "/api/v4" |
| 123 | + |
| 124 | + if not api_version: |
| 125 | + return False |
| 126 | + |
| 127 | + return True |
| 128 | + |
| 129 | + |
| 130 | +class GitLabScriptDownloader(GitScriptDownloaderBase): |
| 131 | + |
| 132 | + @retry((HTTPError, URLError), delay=1, backoff=2, tries=5) |
| 133 | + def download_repo(self, url: str, token: str, branch: str = "") -> str: |
| 134 | + |
| 135 | + # extract data from browser "raw style url" or "gitlab api" style |
| 136 | + is_api_url = is_gitlab_api_url(url) |
| 137 | + if is_api_url: |
| 138 | + url_data = extract_data_from_api_url(url) |
| 139 | + else: |
| 140 | + url_data = extract_data_from_browser_url(url) |
| 141 | + |
| 142 | + # allow service branch attr to override the url defined sha |
| 143 | + sha = branch if branch else url_data.sha |
| 144 | + is_https = True if url_data.protocol == "https" else False |
| 145 | + api_handler = GitlabApiHandler(host=url_data.domain, token=token, is_https=is_https) |
| 146 | + |
| 147 | + # if using raw style url, do lookup for project id from project name |
| 148 | + project_id = url_data.project_id if is_api_url else api_handler.get_project_id_from_name(url_data.project_name) |
| 149 | + working_dir = api_handler.download_archive_to_temp_dir(project_id=project_id, path=url_data.path, sha=sha) |
| 150 | + self.logger.info(f"Temp Working Dir: {working_dir}") |
| 151 | + return working_dir |
0 commit comments