summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gitlab/tests/test_gitlab.py27
-rw-r--r--gitlab/v4/objects.py63
2 files changed, 90 insertions, 0 deletions
diff --git a/gitlab/tests/test_gitlab.py b/gitlab/tests/test_gitlab.py
index 5bf373a..3eccf6e 100644
--- a/gitlab/tests/test_gitlab.py
+++ b/gitlab/tests/test_gitlab.py
@@ -844,6 +844,33 @@ class TestGitlab(unittest.TestCase):
self.assertEqual(ret["message"], "Message")
self.assertEqual(ret["id"], "ed899a2f4b50b4370feeea94676502b42383c746")
+ def test_import_github(self):
+ @urlmatch(
+ scheme="http",
+ netloc="localhost",
+ path="/api/v4/import/github",
+ method="post",
+ )
+ def resp_import_github(url, request):
+ headers = {"content-type": "application/json"}
+ content = """{
+ "id": 27,
+ "name": "my-repo",
+ "full_path": "/root/my-repo",
+ "full_name": "Administrator / my-repo"
+ }"""
+ content = content.encode("utf-8")
+ return response(200, content, headers, None, 25, request)
+
+ with HTTMock(resp_import_github):
+ base_path = "/root"
+ name = "my-repo"
+ ret = self.gl.projects.import_github("githubkey", 1234, base_path, name)
+ self.assertIsInstance(ret, dict)
+ self.assertEqual(ret["name"], name)
+ self.assertEqual(ret["full_path"], "/".join((base_path, name)))
+ self.assertTrue(ret["full_name"].endswith(name))
+
def _default_config(self):
fd, temp_path = tempfile.mkstemp()
os.write(fd, valid_config)
diff --git a/gitlab/v4/objects.py b/gitlab/v4/objects.py
index 2b5ed1d..7a1e7b8 100644
--- a/gitlab/v4/objects.py
+++ b/gitlab/v4/objects.py
@@ -4744,6 +4744,69 @@ class ProjectManager(CRUDMixin, RESTManager):
"/projects/import", post_data=data, files=files, **kwargs
)
+ def import_github(
+ self,
+ personal_access_token,
+ repo_id,
+ target_namespace,
+ new_name=None,
+ timeout_override=60.0,
+ **kwargs
+ ):
+ """Import a project from Github to Gitlab (schedule the import)
+
+ This method will return when an import operation has been safely queued,
+ or an error has occurred. After triggering an import, check the
+ `import_status` of the newly created project to detect when the import
+ operation has completed.
+
+ NOTE: this request may take longer than most other API requests.
+ So this method will override the session timeout with <timeout_override>,
+ which defaults to 60 seconds.
+
+ Args:
+ personal_access_token (str): GitHub personal access token
+ repo_id (int): Github repository ID
+ target_namespace (str): Namespace to import repo into
+ new_name (str): New repo name (Optional)
+ timeout_override (int or float): Timeout to use for this request
+ **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:
+ dict: A representation of the import status.
+
+ Example:
+ ```
+ gl = gitlab.Gitlab_from_config()
+ print "Triggering import"
+ result = gl.projects.import_github(ACCESS_TOKEN,
+ 123456,
+ "my-group/my-subgroup")
+ project = gl.projects.get(ret['id'])
+ print "Waiting for import to complete"
+ while project.import_status == u'started':
+ time.sleep(1.0)
+ project = gl.projects.get(project.id)
+ print "Github import complete"
+ ```
+ """
+ data = {
+ "personal_access_token": personal_access_token,
+ "repo_id": repo_id,
+ "target_namespace": target_namespace,
+ }
+ if new_name:
+ data["new_name"] = new_name
+ prev_timeout = self.gitlab.timeout
+ self.gitlab.timeout = timeout_override
+ result = self.gitlab.http_post("/import/github", post_data=data, **kwargs)
+ self.gitlab.timeout = prev_timeout
+ return result
+
class RunnerJob(RESTObject):
pass