From e321c0bddd39957e1cff91049da849781a272e0b Mon Sep 17 00:00:00 2001 From: Langenfeld Date: Wed, 25 Jan 2023 17:51:54 +0100 Subject: [PATCH] (feat) adding file to repo by api (closes #16) --- gitea/apiobject.py | 14 ++++++++------ tests/test_api.py | 6 +++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/gitea/apiobject.py b/gitea/apiobject.py index fdb6c99..23fc606 100644 --- a/gitea/apiobject.py +++ b/gitea/apiobject.py @@ -535,18 +535,20 @@ class Repository(ApiObject): else: return [Content.parse_response(self.gitea, f) for f in self.gitea.requests_get(url, data)] - def create_file(self, file_path: str, data: "data" = dict): + def create_file(self, file_path: str, content: str, data: dict = None): """https://try.gitea.io/api/swagger#/repository/repoCreateFile""" + if not data: + data = {} url = f"/repos/{self.owner.username}/{self.name}/contents/{file_path}" - if "content" not in data: - raise Exception("No Data to upload is supplied. Please give 'content' Field with data") + data.update({"content": content}) return self.gitea.requests_post(url, data) - def change_file(self, file_path: str, data: "data" = dict): + def change_file(self, file_path: str, file_sha: str, content: str, data: dict = None): """https://try.gitea.io/api/swagger#/repository/repoCreateFile""" + if not data: + data = {} url = f"/repos/{self.owner.username}/{self.name}/contents/{file_path}" - if "content" not in data: - raise Exception("No Data to upload is supplied. Please give 'content' Field with data") + data.update({"sha": file_sha, "data": content}) return self.gitea.requests_put(url, data) def delete(self): diff --git a/tests/test_api.py b/tests/test_api.py index bdc9a40..a06fbe7 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -153,7 +153,7 @@ def test_create_file(instance): org = Organization.request(instance, test_org) repo = org.get_repository(test_repo) repo.create_file("testfile.md", - {"content": TESTFILE_CONENTE_B64.decode("ascii")}) + content = TESTFILE_CONENTE_B64.decode("ascii")) # test if putting was successful content = repo.get_git_content() readmes = [c for c in content if c.name == "testfile.md"] @@ -171,8 +171,8 @@ def test_change_file(instance): content = repo.get_git_content() readmes = [c for c in content if c.name == "testfile.md"] # change - repo.change_file("testfile.md", - {"content": TESTFILE_CONENTE_B64.decode("ascii"), "sha": readmes[0].sha}) + repo.change_file("testfile.md", readmes[0].sha, + content = TESTFILE_CONENTE_B64.decode("ascii")) # test if putting was successful content = repo.get_git_content() readmes = [c for c in content if c.name == "testfile.md"]