kopia lustrzana https://github.com/Langenfeld/py-gitea
(gitea) extended put method to accept additional data (closes #17)
rodzic
32c8d03f34
commit
0a620188ff
|
@ -535,13 +535,20 @@ class Repository(ApiObject):
|
|||
else:
|
||||
return [Content.parse_response(self.gitea, f) for f in self.gitea.requests_get(url, data)]
|
||||
|
||||
def put_file_content(self, file_path: str, data: "data" = dict):
|
||||
def create_file(self, file_path: str, data: "data" = dict):
|
||||
"""https://try.gitea.io/api/swagger#/repository/repoCreateFile"""
|
||||
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")
|
||||
return self.gitea.requests_post(url, data)
|
||||
|
||||
def change_file(self, file_path: str, data: "data" = dict):
|
||||
"""https://try.gitea.io/api/swagger#/repository/repoCreateFile"""
|
||||
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")
|
||||
return self.gitea.requests_put(url, data)
|
||||
|
||||
def delete(self):
|
||||
self.gitea.requests_delete(
|
||||
Repository.REPO_DELETE % (self.owner.username, self.name)
|
||||
|
@ -738,7 +745,6 @@ class Issue(ApiObject):
|
|||
|
||||
class Team(ApiObject):
|
||||
API_OBJECT = """/teams/{id}""" # <id>
|
||||
ADD_USER = """/teams/%s/members/%s""" # <id, username to add>
|
||||
ADD_REPO = """/teams/%s/repos/%s/%s""" # <id, org, repo>
|
||||
TEAM_DELETE = """/teams/%s""" # <id>
|
||||
GET_MEMBERS = """/teams/%s/members""" # <id>
|
||||
|
@ -765,7 +771,9 @@ class Team(ApiObject):
|
|||
_patchable_fields = {"description", "name", "permission", "units"}
|
||||
|
||||
def add_user(self, user: User):
|
||||
self.gitea.requests_put(Team.ADD_USER % (self.id, user.login))
|
||||
"""https://try.gitea.io/api/swagger#/organization/orgAddTeamMember"""
|
||||
url = f"/teams/{self.id}/members/{user.login}"
|
||||
self.gitea.requests_put(url)
|
||||
|
||||
def add_repo(self, org: Organization, repo: Repository):
|
||||
self.gitea.requests_put(Team.ADD_REPO % (self.id, org, repo.name))
|
||||
|
|
|
@ -103,9 +103,11 @@ class Gitea:
|
|||
aggregated_result.extend(result)
|
||||
page += 1
|
||||
|
||||
def requests_put(self, endpoint: str):
|
||||
request = self.requests.put(self.__get_url(endpoint), headers=self.headers)
|
||||
if request.status_code not in [204]:
|
||||
def requests_put(self, endpoint: str, data: dict = None):
|
||||
if not data:
|
||||
data = {}
|
||||
request = self.requests.put(self.__get_url(endpoint), headers=self.headers, data=json.dumps(data))
|
||||
if request.status_code not in [200, 204]:
|
||||
message = f"Received status code: {request.status_code} ({request.url}) {request.text}"
|
||||
self.logger.error(message)
|
||||
raise Exception(message)
|
||||
|
|
|
@ -147,12 +147,12 @@ def test_list_files_and_content(instance):
|
|||
assert len(readme_content) > 0
|
||||
assert "descr" in str(base64.b64decode(readme_content))
|
||||
|
||||
def test_put_files_and_content(instance):
|
||||
def test_create_file(instance):
|
||||
TESTFILE_CONENTE = "TestStringFileContent"
|
||||
TESTFILE_CONENTE_B64 = base64.b64encode(bytes(TESTFILE_CONENTE, 'utf-8'))
|
||||
org = Organization.request(instance, test_org)
|
||||
repo = org.get_repository(test_repo)
|
||||
repo.put_file_content("testfile.md",
|
||||
repo.create_file("testfile.md",
|
||||
{"content": TESTFILE_CONENTE_B64.decode("ascii")})
|
||||
# test if putting was successful
|
||||
content = repo.get_git_content()
|
||||
|
@ -162,6 +162,25 @@ def test_put_files_and_content(instance):
|
|||
assert len(readme_content) > 0
|
||||
assert TESTFILE_CONENTE in str(base64.b64decode(readme_content))
|
||||
|
||||
def test_change_file(instance):
|
||||
TESTFILE_CONENTE = "TestStringFileContent with changed content now"
|
||||
TESTFILE_CONENTE_B64 = base64.b64encode(bytes(TESTFILE_CONENTE, 'utf-8'))
|
||||
org = Organization.request(instance, test_org)
|
||||
repo = org.get_repository(test_repo)
|
||||
#figure out the sha of the file to change
|
||||
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})
|
||||
# test if putting was successful
|
||||
content = repo.get_git_content()
|
||||
readmes = [c for c in content if c.name == "testfile.md"]
|
||||
assert len(readmes) > 0
|
||||
readme_content = repo.get_file_content(readmes[0])
|
||||
assert len(readme_content) > 0
|
||||
assert TESTFILE_CONENTE in str(base64.b64decode(readme_content))
|
||||
|
||||
def test_create_branch(instance):
|
||||
org = Organization.request(instance, test_org)
|
||||
repo = org.get_repository(test_repo)
|
||||
|
|
Ładowanie…
Reference in New Issue