Skip to content

feat: add import project from remote support #2348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 1, 2022
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
9 changes: 9 additions & 0 deletions docs/gl_objects/projects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,15 @@ Import the project into a namespace and override parameters::
override_params={'visibility': 'private'},
)

Import the project using file stored on a remote URL::

output = gl.projects.remote_import(
url="https://whatever.com/url/file.tar.gz",
path="my_new_remote_project",
name="My New Remote Project",
namespace="my-group",
override_params={'visibility': 'private'},
)

Project custom attributes
=========================
Expand Down
45 changes: 45 additions & 0 deletions gitlab/v4/objects/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,8 @@ def import_project(
Args:
file: Data or file object containing the project
path: Name and path for the new project
name: The name of the project to import. If not provided,
defaults to the path of the project.
namespace: The ID or path of the namespace that the project
will be imported to
overwrite: If True overwrite an existing project with the
Expand Down Expand Up @@ -849,6 +851,49 @@ def import_project(
"/projects/import", post_data=data, files=files, **kwargs
)

def remote_import(
self,
url: str,
path: str,
name: Optional[str] = None,
namespace: Optional[str] = None,
overwrite: bool = False,
override_params: Optional[Dict[str, Any]] = None,
**kwargs: Any,
) -> Union[Dict[str, Any], requests.Response]:
"""Import a project from an archive file stored on a remote URL.

Args:
url: URL for the file containing the project data to import
path: Name and path for the new project
name: The name of the project to import. If not provided,
defaults to the path of the project.
namespace: The ID or path of the namespace that the project
will be imported to
overwrite: If True overwrite an existing project with the
same path
override_params: Set the specific settings for the project
**kwargs: Extra options to send to the server (e.g. sudo)

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabListError: If the server failed to perform the request

Returns:
A representation of the import status.
"""
data = {"path": path, "overwrite": str(overwrite), "url": url}
if override_params:
for k, v in override_params.items():
data[f"override_params[{k}]"] = v
if name is not None:
data["name"] = name
if namespace:
data["namespace"] = namespace
return self.gitlab.http_post(
"/projects/remote-import", post_data=data, **kwargs
)

def import_bitbucket_server(
self,
bitbucket_server_url: str,
Expand Down
14 changes: 14 additions & 0 deletions tests/functional/api/test_import_export.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import time

import pytest

import gitlab


Expand Down Expand Up @@ -64,3 +66,15 @@ def test_project_import_export(gl, project, temp_dir):
count += 1
if count == 15:
raise Exception("Project import taking too much time")


def test_project_remote_import(gl):
with pytest.raises(gitlab.exceptions.GitlabHttpError) as err_info:
gl.projects.remote_import(
"ftp://whatever.com/url", "remote-project", "remote-project", "root"
)
assert err_info.value.response_code == 400
assert (
"File url is blocked: Only allowed schemes are https"
in err_info.value.error_message
)
31 changes: 31 additions & 0 deletions tests/unit/objects/test_project_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ def resp_import_project():
yield rsps


@pytest.fixture
def resp_remote_import():
content = {
"id": 1,
"description": None,
"name": "remote-project",
"name_with_namespace": "Administrator / remote-project",
"path": "remote-project",
"path_with_namespace": "root/remote-project",
"created_at": "2018-02-13T09:05:58.023Z",
"import_status": "scheduled",
}

with responses.RequestsMock() as rsps:
rsps.add(
method=responses.POST,
url="http://localhost/api/v4/projects/remote-import",
json=content,
content_type="application/json",
status=200,
)
yield rsps


@pytest.fixture
def resp_import_status():
content = {
Expand Down Expand Up @@ -99,6 +123,13 @@ def test_import_project(gl, resp_import_project):
assert project_import["import_status"] == "scheduled"


def test_remote_import(gl, resp_remote_import):
project_import = gl.projects.remote_import(
"https://whatever.com/url", "remote-project", "remote-project", "root"
)
assert project_import["import_status"] == "scheduled"


def test_import_project_with_override_params(gl, resp_import_project):
project_import = gl.projects.import_project(
"file", "api-project", override_params={"visibility": "private"}
Expand Down