Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/sast.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions tests/test_gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion web3_utils/gitlab.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import base64
import re
import shutil
from os import path, makedirs, getcwd
from gitlab import Gitlab
from typing import Optional


class GitLab:
def __init__(self, url: str, token: str, tmp_dir: str = path.join(getcwd(), "tmp", "public_keys")):
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()

Expand Down