Skip to content

Commit e820d06

Browse files
committed
allow user branch attr to override
1 parent 83b6d62 commit e820d06

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

package/cloudshell/iac/terraform/downloaders/gitlab_downloader.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,30 @@ class CommonGitLabUrlData:
1212
domain: str
1313
path: str
1414
full_url: str
15+
sha: str
1516

1617

1718
@dataclass
1819
class GitLabRawUrlData(CommonGitLabUrlData):
1920
gitlab_user: str
2021
project_name: str
21-
branch: str
2222

2323

2424
@dataclass
2525
class GitLabApiUrlData(CommonGitLabUrlData):
2626
api_version: str
2727
project_id: int
2828
api_endpoint: str
29-
sha: str
3029

3130

3231
def extract_data_from_raw_url(url) -> GitLabRawUrlData:
3332
"""
3433
Take api style url and extract data
3534
Sample Raw Browser url: "http://192.168.85.26/quali_natti/terraformstuff/-/tree/test-branch/rds/project1"
35+
'sha' can be branch or commit id
3636
"""
3737
pattern = (r'^(?P<protocol>https?)://(?P<domain>[^/]+)/(?P<user>[^/]+)/(?P<project>[^/]+)/-/tree/'
38-
r'(?P<branch>[^/]+)/(?P<path>.*)?$')
38+
r'(?P<sha>[^/]+)/(?P<path>.*)?$')
3939

4040
match = re.match(pattern, url)
4141
if not match:
@@ -46,7 +46,7 @@ def extract_data_from_raw_url(url) -> GitLabRawUrlData:
4646
domain=groups['domain'],
4747
gitlab_user=groups['user'],
4848
project_name=groups['project'],
49-
branch=groups['branch'],
49+
sha=groups['branch'],
5050
path=groups['path'],
5151
full_url=url)
5252

@@ -121,14 +121,21 @@ class GitLabScriptDownloader(GitScriptDownloaderBase):
121121

122122
@retry((HTTPError, URLError), delay=1, backoff=2, tries=5)
123123
def download_repo(self, url: str, token: str, branch: str = "") -> str:
124+
125+
# extract data from browser "raw style url" or "gitlab api" style
124126
is_api_url = is_gitlab_api_url(url)
125127
if is_api_url:
126128
url_data = extract_data_from_api_url(url)
127129
else:
128130
url_data = extract_data_from_raw_url(url)
131+
132+
# allow service branch attr to override the url defined sha
133+
sha = branch if branch else url_data.sha
129134
is_https = True if url_data.protocol == "https" else False
130135
api_handler = GitlabApiHandler(host=url_data.domain, token=token, is_https=is_https)
136+
137+
# if using raw style url, do lookup for project id from project name
131138
project_id = url_data.project_id if is_api_url else api_handler.get_project_id_from_name(url_data.project_name)
132-
working_dir = api_handler.download_archive_to_temp_dir(project_id=project_id, path=url_data.path, sha=branch)
139+
working_dir = api_handler.download_archive_to_temp_dir(project_id=project_id, path=url_data.path, sha=sha)
133140
self.logger.info(f"Temp Working Dir: {working_dir}")
134141
return working_dir

0 commit comments

Comments
 (0)