Skip to content

Commit

Permalink
fix for get filename from gitlab url structure
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamSharon committed May 2, 2021
1 parent e74a747 commit 8f8f152
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ def _get_filename(self, response):
if matching:
file_name = matching.group('filename')

# fallback, couldn't find file name regular URL, check gitlab structure (filename in [-2] position)
if not file_name:
file_name_from_url = urllib.parse.unquote(response.url.split('/')[-2])
matching = re.match(self.filename_pattern, file_name_from_url)
if matching:
file_name = matching.group('filename')

if not file_name:
raise Exception("Script file of supported types: '.sh', '.bash', '.ps1' was not found")
return file_name.strip()
10 changes: 10 additions & 0 deletions package/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def mocked_requests_get(*args, **kwargs):
"private_token": 'https://raw.repocontentservice.com/SomeUser/SomePrivateTokenRepo/master/bashScript.sh',
"private_token_auth_pattern": 'https://gitlab.mock.com/api/v4/SomeUser/SomePrivateTokenRepo/master/bashScript.sh',
"private_cred": 'https://raw.repocontentservice.com/SomeUser/SomePrivateCredRepo/master/bashScript.sh',
"private_cred_gitlab_struct": 'https://gitlab.mock.com/api/v4/SomeUser/SomePrivateTokenRepo/master/bashScript%2Esh/raw?ref=master',
"content": 'SomeBashScriptContent'
}

Expand Down Expand Up @@ -48,6 +49,15 @@ def iter_content(self, chunk):
elif kwargs["headers"]["Private-Token"] == 'Bearer 551e48b030e1a9f334a330121863e48e43f58c55':
response = MockResponse(repo_dict['content'], 200, {"Content-Type": "text/plain"}, repo_dict['private_token_auth_pattern'])
return response

if args[0] == repo_dict['private_cred_gitlab_struct']:
if 'headers' in kwargs:
if 'Authorization' in kwargs["headers"]:
response = MockResponse("error", 404, {"Content-Type": "text/plain"}, "error content")
return response
elif kwargs["headers"]["Private-Token"] == 'Bearer 551e48b030e1a9f334a330121863e48e43f58c55':
response = MockResponse(repo_dict['content'], 200, {"Content-Type": "text/plain"}, repo_dict['private_cred_gitlab_struct'])
return response

if args[0] == repo_dict['private_cred']:
if 'auth' in kwargs and kwargs["auth"] is not None:
Expand Down
15 changes: 15 additions & 0 deletions package/tests/test_script_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ def test_download_as_private_with_token_with_private_token_pattern(self, mocked_
self.assertEqual(script_file.name, "bashScript.sh")
self.assertEqual(script_file.text, "SomeBashScriptContent")

@mock.patch('cloudshell.cm.customscript.domain.script_downloader.requests.get', side_effect=mocked_requests_get)
def test_download_as_private_with_token_with_gitlab_url_structure(self, mocked_requests_get):
# private - url, with token
private_repo_url = 'https://gitlab.mock.com/api/v4/SomeUser/SomePrivateTokenRepo/master/bashScript%2Esh/raw?ref=master'
self.auth = HttpAuth('','','551e48b030e1a9f334a330121863e48e43f58c55')

# set downloaded and downaload
self.logger.info = print_logs
script_downloader = ScriptDownloader(self.logger, self.cancel_sampler)
script_file = script_downloader.download(private_repo_url, self.auth, True)

# assert name and content
self.assertEqual(script_file.name, "bashScript.sh")
self.assertEqual(script_file.text, "SomeBashScriptContent")

@mock.patch('cloudshell.cm.customscript.domain.script_downloader.requests.get', side_effect=mocked_requests_get)
def test_download_as_private_with_credentials_and_failed_token(self, mocked_requests_get):
# private - url, with token that fails and user\password. note - this is will not work on GitHub repo, they require token
Expand Down

0 comments on commit 8f8f152

Please sign in to comment.