Skip to content

Commit fe9571e

Browse files
feat: add remote import s3
1 parent e5dc72d commit fe9571e

File tree

4 files changed

+135
-1
lines changed

4 files changed

+135
-1
lines changed

docs/gl_objects/projects.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,20 @@ Import the project using file stored on a remote URL::
352352
override_params={'visibility': 'private'},
353353
)
354354

355+
Import the project using file stored on AWS S3::
356+
357+
output = gl.projects.remote_import_s3(
358+
region="aws-region",
359+
bucket_name="aws-bucket-name",
360+
file_key="aws-file-key",
361+
access_key_id="aws-access-key-id",
362+
secret_access_key="secret-access-key",
363+
path="my_new_remote_project",
364+
name="My New Remote Project",
365+
namespace="my-group",
366+
override_params={'visibility': 'private'},
367+
)
368+
355369
Project custom attributes
356370
=========================
357371

gitlab/v4/objects/projects.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,65 @@ def remote_import(
894894
"/projects/remote-import", post_data=data, **kwargs
895895
)
896896

897+
def remote_import_s3(
898+
self,
899+
region: str,
900+
bucket_name: str,
901+
file_key: str,
902+
access_key_id: str,
903+
secret_access_key: str,
904+
path: str,
905+
name: Optional[str] = None,
906+
namespace: Optional[str] = None,
907+
overwrite: bool = False,
908+
override_params: Optional[Dict[str, Any]] = None,
909+
**kwargs: Any,
910+
) -> Union[Dict[str, Any], requests.Response]:
911+
"""Import a project from an archive file stored on AWS S3.
912+
913+
Args:
914+
region: AWS S3 region name where the file is stored
915+
bucket_name: AWS S3 bucket name where the file is stored
916+
file_key: AWS S3 file key to identify the file.
917+
access_key_id: AWS S3 access key ID.
918+
secret_access_key: AWS S3 secret access key.
919+
path: Name and path for the new project
920+
name: The name of the project to import. If not provided,
921+
defaults to the path of the project.
922+
namespace: The ID or path of the namespace that the project
923+
will be imported to
924+
overwrite: If True overwrite an existing project with the
925+
same path
926+
override_params: Set the specific settings for the project
927+
**kwargs: Extra options to send to the server (e.g. sudo)
928+
929+
Raises:
930+
GitlabAuthenticationError: If authentication is not correct
931+
GitlabListError: If the server failed to perform the request
932+
933+
Returns:
934+
A representation of the import status.
935+
"""
936+
data = {
937+
"region": region,
938+
"bucket_name": bucket_name,
939+
"file_key": file_key,
940+
"access_key_id": access_key_id,
941+
"secret_access_key": secret_access_key,
942+
"path": path,
943+
"overwrite": str(overwrite),
944+
}
945+
if override_params:
946+
for k, v in override_params.items():
947+
data[f"override_params[{k}]"] = v
948+
if name is not None:
949+
data["name"] = name
950+
if namespace:
951+
data["namespace"] = namespace
952+
return self.gitlab.http_post(
953+
"/projects/remote-import-s3", post_data=data, **kwargs
954+
)
955+
897956
def import_bitbucket_server(
898957
self,
899958
bitbucket_server_url: str,

tests/functional/api/test_import_export.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,23 @@ def test_project_remote_import(gl):
7878
"File url is blocked: Only allowed schemes are https"
7979
in err_info.value.error_message
8080
)
81+
82+
83+
def test_project_remote_import_s3(gl):
84+
gl.features.set("import_project_from_remote_file_s3", True)
85+
with pytest.raises(gitlab.exceptions.GitlabHttpError) as err_info:
86+
gl.projects.remote_import_s3(
87+
"aws-region",
88+
"aws-bucket-name",
89+
"aws-file-key",
90+
"aws-access-key-id",
91+
"secret-access-key",
92+
"remote-project",
93+
"remote-project",
94+
"root",
95+
)
96+
assert err_info.value.response_code == 400
97+
assert (
98+
"Failed to open 'aws-file-key' in 'aws-bucket-name'"
99+
in err_info.value.error_message
100+
)

tests/unit/objects/test_project_import_export.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,30 @@ def resp_remote_import():
5353
yield rsps
5454

5555

56+
@pytest.fixture
57+
def resp_remote_import_s3():
58+
content = {
59+
"id": 1,
60+
"description": None,
61+
"name": "remote-project-s3",
62+
"name_with_namespace": "Administrator / remote-project-s3",
63+
"path": "remote-project-s3",
64+
"path_with_namespace": "root/remote-project-s3",
65+
"created_at": "2018-02-13T09:05:58.023Z",
66+
"import_status": "scheduled",
67+
}
68+
69+
with responses.RequestsMock() as rsps:
70+
rsps.add(
71+
method=responses.POST,
72+
url="http://localhost/api/v4/projects/remote-import-s3",
73+
json=content,
74+
content_type="application/json",
75+
status=200,
76+
)
77+
yield rsps
78+
79+
5680
@pytest.fixture
5781
def resp_import_status():
5882
content = {
@@ -125,7 +149,24 @@ def test_import_project(gl, resp_import_project):
125149

126150
def test_remote_import(gl, resp_remote_import):
127151
project_import = gl.projects.remote_import(
128-
"https://whatever.com/url", "remote-project", "remote-project", "root"
152+
"https://whatever.com/url/file.tar.gz",
153+
"remote-project",
154+
"remote-project",
155+
"root",
156+
)
157+
assert project_import["import_status"] == "scheduled"
158+
159+
160+
def test_remote_import_s3(gl, resp_remote_import_s3):
161+
project_import = gl.projects.remote_import_s3(
162+
"aws-region",
163+
"aws-bucket-name",
164+
"aws-file-key",
165+
"aws-access-key-id",
166+
"secret-access-key",
167+
"remote-project",
168+
"remote-project",
169+
"root",
129170
)
130171
assert project_import["import_status"] == "scheduled"
131172

0 commit comments

Comments
 (0)