From 75a9591b985b122ce002a82372df68f5b636b964 Mon Sep 17 00:00:00 2001 From: laromicas Date: Wed, 22 Mar 2023 20:47:43 -0500 Subject: [PATCH] Support for migration of repositories --- gitea/__init__.py | 3 +- gitea/apiobject.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++ tests/test_api.py | 12 +++++++- 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/gitea/__init__.py b/gitea/__init__.py index 97bbedd..4a9b7a3 100644 --- a/gitea/__init__.py +++ b/gitea/__init__.py @@ -15,7 +15,8 @@ from .apiobject import ( Milestone, Commit, Comment, - Content + Content, + MigrationServices ) __all__ = [ diff --git a/gitea/apiobject.py b/gitea/apiobject.py index 79f0002..3b2869f 100644 --- a/gitea/apiobject.py +++ b/gitea/apiobject.py @@ -324,6 +324,7 @@ class Branch(ReadonlyApiObject): class Repository(ApiObject): API_OBJECT = """/repos/{owner}/{name}""" # , + REPO_MIGRATE = """/repos/migrate""" REPO_IS_COLLABORATOR = """/repos/%s/%s/collaborators/%s""" # , , REPO_SEARCH = """/repos/search/%s""" # REPO_BRANCHES = """/repos/%s/%s/branches""" # , @@ -575,6 +576,65 @@ class Repository(ApiObject): ) self.deleted = True + def migrate_repo( + self, + service: str, + clone_addr: str, + repo_name: str, + description: str = "", + private: bool = False, + auth_token: str = None, + auth_username: str = None, + auth_password: str = None, + mirror: bool = False, + mirror_interval: str = None, + lfs: bool = False, + lfs_endpoint: str = "", + wiki: bool = True, + labels: bool = False, + issues: bool = False, + pull_requests: bool = True, + releases: bool = True, + milestones: bool = True, + repo_owner: str = None + ): + """ Migrate a Repository from another service. + + Throws: + AlreadyExistsException: If the Repository exists already. + Exception: If something else went wrong. + """ + result = self.gitea.requests_post( + Repository.REPO_MIGRATE, + data={ + "auth_password": auth_password, + "auth_token": auth_token, + "auth_username": auth_username, + "clone_addr": clone_addr, + "description": description, + "issues": issues, + "labels": labels, + "lfs": lfs, + "lfs_endpoint": lfs_endpoint, + "milestones": milestones, + "mirror": mirror, + "mirror_interval": mirror_interval, + "private": private, + "pull_requests": pull_requests, + "releases": releases, + "repo_name": repo_name, + "repo_owner": repo_owner, + "service": service, + "wiki": wiki + } + ) + if "id" in result: + self.gitea.logger.info("Successfully created Job to Migrate Repository %s " % result["name"]) + else: + self.gitea.logger.error(result["message"]) + raise Exception("Repository not Migrated... (gitea: %s)" % result["message"]) + return Repository.parse_response(self, result) + class Milestone(ApiObject): API_OBJECT = """/repos/{owner}/{repo}/milestones/{number}""" # @@ -853,3 +913,13 @@ class Util: return datetime.strptime(time[:-3] + "00", "%Y-%m-%dT%H:%M:%S%z") except ValueError: return datetime.strptime(time[:-3] + "00", "%Y-%m-%dT%H:%M:%S") + +class MigrationServices: + GIT = "1" + GITHUB = "2" + GITEA = "3" + GITLAB = "4" + GOGS = "5" + ONEDEV = "6" + BITBUCKET = "7" + CODEBASE = "8" diff --git a/tests/test_api.py b/tests/test_api.py index 46aa82e..3ce6060 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -3,7 +3,7 @@ import base64 import pytest import uuid -from gitea import Gitea, User, Organization, Team, Repository, Issue, Milestone +from gitea import Gitea, User, Organization, Team, Repository, Issue, Milestone, MigrationServices from gitea import NotFoundException, AlreadyExistsException # put a ".token" file into your directory containg only the token for gitea @@ -386,3 +386,13 @@ def test_delete_user(instance): user.delete() with pytest.raises(NotFoundException) as e: User.request(instance, user_name) + +def test_migrate_repo(instance): + service = Repository(instance) + repo = service.migrate_repo(MigrationServices.GITEA, "https://gitea.com/gitea/awesome-gitea.git", test_repo, "user owned repo") + assert repo.name == test_repo + assert repo.description == "user owned repo" + assert not repo.private + assert repo.owner.username == "test" + repo = Repository.request(instance, "test", test_repo) + repo.delete()