diff --git a/.github/workflows/sast.yml b/.github/workflows/sast.yml index f44c1e4..86f2cfa 100644 --- a/.github/workflows/sast.yml +++ b/.github/workflows/sast.yml @@ -19,7 +19,7 @@ jobs: with: output: reports type: python, depscan - - uses: actions/upload-artifact@v3 + - uses: actions/upload-artifact@v4 with: name: reports path: reports diff --git a/tests/test_gitlab.py b/tests/test_gitlab.py index 2bba2e1..fbc8019 100644 --- a/tests/test_gitlab.py +++ b/tests/test_gitlab.py @@ -46,6 +46,41 @@ def test_download_files_from_project(gitlab_instance, mocker): repo_blob_mock.assert_has_calls([mocker.call("file_id_1"), mocker.call("file_id_2")]) +def test_download_files_from_project_with_include_only_files(gitlab_instance, mocker): + gitlab, gitlab_mock = gitlab_instance + + repo_tree_mock = mocker.patch.object( + gitlab_mock.projects.get(1), + "repository_tree", + return_value=[ + {"id": "file_id_1", "name": "hoodi-file.txt", "type": "blob"}, + {"id": "file_id_2", "name": "holesky-file.txt", "type": "blob"}, + {"id": "file_id_3", "name": "not-hoodi.txt", "type": "blob"}, + ], + ) + + repo_blob_mock = mocker.patch.object( + gitlab_mock.projects.get(1), + "repository_blob", + side_effect=[ + {"content": base64.b64encode(b"file1 content").decode("utf-8")}, + {"content": base64.b64encode(b"file2 content").decode("utf-8")}, + ], + ) + + downloaded_files = gitlab.download_files_from_project( + project_id=1, dir_path="test_dir", branch="main", include_only_files=["^hoodi", "^holesky"] + ) + + expected_paths = [ + os.path.join(os.getcwd(), "tmp", "public_keys", "hoodi-file.txt"), + os.path.join(os.getcwd(), "tmp", "public_keys", "holesky-file.txt"), + ] + assert downloaded_files == expected_paths + repo_tree_mock.assert_called_once_with(ref="main", path="test_dir", get_all=True) + repo_blob_mock.assert_has_calls([mocker.call("file_id_1"), mocker.call("file_id_2")]) + + def test_prepare_temp_directory(gitlab_instance, mocker): gitlab, gitlab_mock = gitlab_instance diff --git a/web3_utils/gitlab.py b/web3_utils/gitlab.py index b5dc6cd..8c76642 100644 --- a/web3_utils/gitlab.py +++ b/web3_utils/gitlab.py @@ -1,7 +1,9 @@ import base64 +import re import shutil from os import path, makedirs, getcwd from gitlab import Gitlab +from typing import Optional class GitLab: @@ -9,12 +11,16 @@ def __init__(self, url: str, token: str, tmp_dir: str = path.join(getcwd(), "tmp self.client = Gitlab(private_token=token, url=url) self.tmp_dir = tmp_dir - def download_files_from_project(self, project_id: int, dir_path: str, branch: str = "master"): + def download_files_from_project( + self, project_id: int, dir_path: str, branch: str = "master", include_only_files: Optional[list[str]] = None + ): # ensures that access token doesn't expire self.client.auth() project = self.client.projects.get(project_id) items = project.repository_tree(ref=branch, path=dir_path, get_all=True) + if include_only_files: + items = [item for item in items if any(re.match(pattern, item["name"]) for pattern in include_only_files)] self._prepare_temp_directory()