diff --git a/fedeproxy/common/gitlab.py b/fedeproxy/common/gitlab.py index 4f227af..c594f14 100644 --- a/fedeproxy/common/gitlab.py +++ b/fedeproxy/common/gitlab.py @@ -101,6 +101,27 @@ class GitLab(object): else: return info + def project_export(self, namespace, project, filename): + url = f'{self.s.api}/projects/{namespace}%2F{project}/export' + r = self.s.post(url) + assert r.status_code == 202, f'{r.status_code} {r.text}' + + while True: + r = self.s.get(url) + r.raise_for_status() + status = r.json()["export_status"] + logger.info(f"waiting {namespace}/{project} export: status is {status}") + if status == "finished": + break + time.sleep(1) + logger.info(f"download {namespace}/{project} into {filename}") + with self.s.get(f'{url}/download', stream=True) as r: + r.raise_for_status() + with open(filename, 'wb') as f: + for chunk in r.iter_content(chunk_size=8192): + f.write(chunk) + return True + def create_api_application(self, domain): callbacks = [ f'https://api.{domain}/accounts/gitlab/login/callback/', diff --git a/tests/fedeproxy/common/test_gitlab.py b/tests/fedeproxy/common/test_gitlab.py index df83d2a..0b7aec3 100644 --- a/tests/fedeproxy/common/test_gitlab.py +++ b/tests/fedeproxy/common/test_gitlab.py @@ -1,11 +1,12 @@ import os import pytest +import gzip from fedeproxy.common.gitlab import GitLab @pytest.mark.gitlab -def test_create_project(tmpdir): +def test_project_create(): ip = os.environ.get('FEDEPROXY_IP', '0.0.0.0') gitlab = GitLab(f'http://{ip}:8181') gitlab.login('root', 'Wrobyak4') @@ -13,3 +14,15 @@ def test_create_project(tmpdir): assert gitlab.project_get('root', 'testproject') is None p = gitlab.project_create('root', 'testproject') assert p['id'] == gitlab.project_create('root', 'testproject')['id'] + + +@pytest.mark.gitlab +def test_project_export(tmpdir): + ip = os.environ.get('FEDEPROXY_IP', '0.0.0.0') + gitlab = GitLab(f'http://{ip}:8181') + gitlab.login('root', 'Wrobyak4') + gitlab.project_delete('root', 'testproject') + gitlab.project_create('root', 'testproject') + exported = f'{tmpdir}/testproject.zip' + gitlab.project_export('root', 'testproject', exported) + assert gzip.open(exported)