kopia lustrzana https://github.com/Langenfeld/py-gitea
commit dirty fields of organization back to api
rodzic
ef93d185d1
commit
c5e213e7c6
|
@ -4,7 +4,7 @@ import logging
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
logging = logging.getLogger("gitea")
|
logging = logging.getLogger("gitea")
|
||||||
version = "0.4.4"
|
version = "0.4.6"
|
||||||
|
|
||||||
class AlreadyExistsException(Exception):
|
class AlreadyExistsException(Exception):
|
||||||
pass
|
pass
|
||||||
|
@ -19,11 +19,13 @@ class ObjectIsInvalid(Exception):
|
||||||
class GiteaApiObject:
|
class GiteaApiObject:
|
||||||
|
|
||||||
GET_API_OBJECT = "FORMAT/STINING/{argument}"
|
GET_API_OBJECT = "FORMAT/STINING/{argument}"
|
||||||
|
PATCH_API_OBJECT = "FORMAT/STINING/{argument}"
|
||||||
|
|
||||||
def __init__(self, gitea, id: int):
|
def __init__(self, gitea, id: int):
|
||||||
self.__id = id
|
self.__id = id
|
||||||
self.gitea = gitea
|
self.gitea = gitea
|
||||||
self.deleted = False # set if .delete was called, so that an exception is risen
|
self.deleted = False # set if .delete was called, so that an exception is risen
|
||||||
|
self.dirty_fields = set()
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
return other.id == self.id if isinstance(other, type(self)) else False
|
return other.id == self.id if isinstance(other, type(self)) else False
|
||||||
|
@ -36,6 +38,12 @@ class GiteaApiObject:
|
||||||
|
|
||||||
fields_to_parsers = {}
|
fields_to_parsers = {}
|
||||||
|
|
||||||
|
def commit(self):
|
||||||
|
raise NotImplemented()
|
||||||
|
|
||||||
|
def get_dirty_fields(self):
|
||||||
|
return {name: getattr(self,name) for name in self.dirty_fields}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def request(cls, gitea, id):
|
def request(cls, gitea, id):
|
||||||
"""Use for ginving a nice e.g. 'request(gita, orgname, repo, ticket)'.
|
"""Use for ginving a nice e.g. 'request(gita, orgname, repo, ticket)'.
|
||||||
|
@ -64,7 +72,7 @@ class GiteaApiObject:
|
||||||
"""Retrieving an object always as GET_API_OBJECT """
|
"""Retrieving an object always as GET_API_OBJECT """
|
||||||
return gitea.requests_get(cls.GET_API_OBJECT.format(**args))
|
return gitea.requests_get(cls.GET_API_OBJECT.format(**args))
|
||||||
|
|
||||||
editable_fields = []
|
patchable_fields = set()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _initialize(cls, gitea, api_object, result):
|
def _initialize(cls, gitea, api_object, result):
|
||||||
|
@ -72,7 +80,7 @@ class GiteaApiObject:
|
||||||
if name in cls.fields_to_parsers and value is not None:
|
if name in cls.fields_to_parsers and value is not None:
|
||||||
parse_func = cls.fields_to_parsers[name]
|
parse_func = cls.fields_to_parsers[name]
|
||||||
value = parse_func(gitea, value)
|
value = parse_func(gitea, value)
|
||||||
if name in cls.editable_fields:
|
if name in cls.patchable_fields:
|
||||||
prop = property(
|
prop = property(
|
||||||
(lambda name: lambda self: self.__get_var(name))(name),
|
(lambda name: lambda self: self.__get_var(name))(name),
|
||||||
(lambda name: lambda self, v: self.__set_var(name, v))(name))
|
(lambda name: lambda self, v: self.__set_var(name, v))(name))
|
||||||
|
@ -83,6 +91,7 @@ class GiteaApiObject:
|
||||||
setattr(api_object, "__"+name, value)
|
setattr(api_object, "__"+name, value)
|
||||||
|
|
||||||
def __set_var(self,name,i):
|
def __set_var(self,name,i):
|
||||||
|
self.dirty_fields.add(name)
|
||||||
setattr(self,"__"+name,i)
|
setattr(self,"__"+name,i)
|
||||||
|
|
||||||
def __get_var(self,name):
|
def __get_var(self,name):
|
||||||
|
@ -90,13 +99,14 @@ class GiteaApiObject:
|
||||||
raise ObjectIsInvalid()
|
raise ObjectIsInvalid()
|
||||||
return getattr(self,"__"+name)
|
return getattr(self,"__"+name)
|
||||||
|
|
||||||
|
|
||||||
class Organization(GiteaApiObject):
|
class Organization(GiteaApiObject):
|
||||||
|
|
||||||
GET_API_OBJECT = """/orgs/{name}""" # <org>
|
GET_API_OBJECT = """/orgs/{name}""" # <org>
|
||||||
|
PATCH_API_OBJECT = """/orgs/{name}""" # <org>
|
||||||
ORG_REPOS_REQUEST = """/orgs/%s/repos""" # <org>
|
ORG_REPOS_REQUEST = """/orgs/%s/repos""" # <org>
|
||||||
ORG_TEAMS_REQUEST = """/orgs/%s/teams""" # <org>
|
ORG_TEAMS_REQUEST = """/orgs/%s/teams""" # <org>
|
||||||
ORG_TEAMS_CREATE = """/orgs/%s/teams""" # <org>
|
ORG_TEAMS_CREATE = """/orgs/%s/teams""" # <org>
|
||||||
ORG_PATCH = """/orgs/%s""" # <org>
|
|
||||||
ORG_GET_MEMBERS = """/orgs/%s/members""" # <org>
|
ORG_GET_MEMBERS = """/orgs/%s/members""" # <org>
|
||||||
ORG_DELETE = """/orgs/%s""" # <org>
|
ORG_DELETE = """/orgs/%s""" # <org>
|
||||||
|
|
||||||
|
@ -107,7 +117,13 @@ class Organization(GiteaApiObject):
|
||||||
def request(cls, gitea, name):
|
def request(cls, gitea, name):
|
||||||
return cls._request(gitea, {"name": name})
|
return cls._request(gitea, {"name": name})
|
||||||
|
|
||||||
editable_fields = ["description", "full_name", "location", "visibility", "website"]
|
patchable_fields = {"description", "full_name", "location", "visibility", "website"}
|
||||||
|
|
||||||
|
def commit(self):
|
||||||
|
values = self.get_dirty_fields()
|
||||||
|
args = {"name": self.name}
|
||||||
|
self.gitea.requests_patch(Organization.PATCH_API_OBJECT.format(**args), data=values)
|
||||||
|
self.dirty_fields = {}
|
||||||
|
|
||||||
# oldstuff
|
# oldstuff
|
||||||
|
|
||||||
|
@ -152,21 +168,6 @@ class Organization(GiteaApiObject):
|
||||||
results = self.gitea.requests_get(Organization.ORG_GET_MEMBERS % self.username)
|
results = self.gitea.requests_get(Organization.ORG_GET_MEMBERS % self.username)
|
||||||
return [User.parse_request(self.gitea, result) for result in results]
|
return [User.parse_request(self.gitea, result) for result in results]
|
||||||
|
|
||||||
def set_value(self, values: dict):
|
|
||||||
""" Setting a certain value for an Organization.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
values (dict): Which values should be changed
|
|
||||||
description: string
|
|
||||||
full_name: string
|
|
||||||
location: string
|
|
||||||
website: string
|
|
||||||
"""
|
|
||||||
result = self.gitea.requests_patch(
|
|
||||||
Organization.ORG_PATCH % self.username, data=values
|
|
||||||
)
|
|
||||||
return Organization.parse_request(self.gitea, result)
|
|
||||||
|
|
||||||
def remove_member(self, username):
|
def remove_member(self, username):
|
||||||
if isinstance(username, User):
|
if isinstance(username, User):
|
||||||
username = username.username
|
username = username.username
|
||||||
|
@ -200,9 +201,9 @@ class User(GiteaApiObject):
|
||||||
def request(cls, gitea, name):
|
def request(cls, gitea, name):
|
||||||
return cls._request(gitea, {"name": name})
|
return cls._request(gitea, {"name": name})
|
||||||
|
|
||||||
editable_fields = ["active", "admin", "allow_create_organization", "allow_git_hook", "allow_import_local",
|
patchable_fields = {"active", "admin", "allow_create_organization", "allow_git_hook", "allow_import_local",
|
||||||
"email", "full_name", "location", "login_name", "max_repo_creation", "must_change_password",
|
"email", "full_name", "location", "login_name", "max_repo_creation", "must_change_password",
|
||||||
"password", "prohibit_login", "source_id", "website"]
|
"password", "prohibit_login", "source_id", "website"}
|
||||||
|
|
||||||
def get_repositories(self):
|
def get_repositories(self):
|
||||||
""" Get all Repositories owned by this User.
|
""" Get all Repositories owned by this User.
|
||||||
|
@ -284,6 +285,10 @@ class Repository(GiteaApiObject):
|
||||||
def request(cls, gitea, owner, name):
|
def request(cls, gitea, owner, name):
|
||||||
return cls._request(gitea, {"owner": owner, "name": name})
|
return cls._request(gitea, {"owner": owner, "name": name})
|
||||||
|
|
||||||
|
patchable_fields = {"allow_merge_commits", "allow_rebase", "allow_rebase_explicit", "allow_squash_merge",
|
||||||
|
"archived","default_branch","description","has_issues","has_pull_requests","has_wiki",
|
||||||
|
"ignore_whitespace_conflicts","name","private","website"}
|
||||||
|
|
||||||
def get_branches(self):
|
def get_branches(self):
|
||||||
"""Get all the Branches of this Repository.
|
"""Get all the Branches of this Repository.
|
||||||
|
|
||||||
|
@ -377,6 +382,10 @@ class Milestone(GiteaApiObject):
|
||||||
"due_on": lambda gitea, t: Util.convert_time(t)
|
"due_on": lambda gitea, t: Util.convert_time(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
patchable_fields = {"allow_merge_commits", "allow_rebase", "allow_rebase_explicit", "allow_squash_merge",
|
||||||
|
"archived", "default_branch","description", "has_issues", "has_pull_requests",
|
||||||
|
"has_wiki", "ignore_whitespace_conflicts", "name", "private", "website"}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def request(cls, gitea, owner, repo, number):
|
def request(cls, gitea, owner, repo, number):
|
||||||
return cls._request(gitea, {"owner":owner, "repo":repo, "number":number})
|
return cls._request(gitea, {"owner":owner, "repo":repo, "number":number})
|
||||||
|
@ -404,6 +413,8 @@ class Issue(GiteaApiObject):
|
||||||
"state": lambda gitea, s: Issue.closed if s == "closed" else Issue.open
|
"state": lambda gitea, s: Issue.closed if s == "closed" else Issue.open
|
||||||
}
|
}
|
||||||
|
|
||||||
|
patchable_fields = {"assignee", "assignees", "body", "due_date", "milestone", "state", "title"}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def request(cls, gitea, owner, repo, number):
|
def request(cls, gitea, owner, repo, number):
|
||||||
api_object = cls._request(gitea, {"owner":owner, "repo":repo, "number":number})
|
api_object = cls._request(gitea, {"owner":owner, "repo":repo, "number":number})
|
||||||
|
@ -461,6 +472,8 @@ class Team(GiteaApiObject):
|
||||||
def request(cls, gitea, id):
|
def request(cls, gitea, id):
|
||||||
return cls._request(gitea, {"id":id})
|
return cls._request(gitea, {"id":id})
|
||||||
|
|
||||||
|
patchable_fields = {"description", "name", "permission", "units"}
|
||||||
|
|
||||||
def add(self, toAdd):
|
def add(self, toAdd):
|
||||||
""" Adding User or Repository to Team.
|
""" Adding User or Repository to Team.
|
||||||
|
|
||||||
|
|
21
test_api.py
21
test_api.py
|
@ -65,6 +65,11 @@ def test_create_org():
|
||||||
assert not org.website
|
assert not org.website
|
||||||
assert not org.full_name
|
assert not org.full_name
|
||||||
|
|
||||||
|
def test_non_changable_field():
|
||||||
|
org = Organization.request(gitea, test_org)
|
||||||
|
with pytest.raises(AttributeError) as e:
|
||||||
|
org.id = 55
|
||||||
|
|
||||||
def test_create_repo_userowned():
|
def test_create_repo_userowned():
|
||||||
org = User.request(gitea, test_user)
|
org = User.request(gitea, test_user)
|
||||||
repo = gitea.create_repo(org, test_repo, "user owned repo")
|
repo = gitea.create_repo(org, test_repo, "user owned repo")
|
||||||
|
@ -73,6 +78,22 @@ def test_create_repo_userowned():
|
||||||
assert repo.name == test_repo
|
assert repo.name == test_repo
|
||||||
assert not repo.private
|
assert not repo.private
|
||||||
|
|
||||||
|
def test_edit_org_fields_and_commit():
|
||||||
|
org = Organization.request(gitea, test_org)
|
||||||
|
org.description = "some thing other man"
|
||||||
|
org.location = "somewehre new"
|
||||||
|
org.visibility = "public"
|
||||||
|
org.website = "http:\\\\testurl.com"
|
||||||
|
org.commit()
|
||||||
|
org2 = Organization.request(gitea, test_org)
|
||||||
|
assert org2.name == test_org
|
||||||
|
assert org2.description == "some thing other man"
|
||||||
|
assert org2.location == "somewehre new"
|
||||||
|
#assert org2.visibility == "private" # after commiting, this field just vanishes (Bug?)
|
||||||
|
assert org2.website == "http:\\\\testurl.com"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test_create_repo_orgowned():
|
def test_create_repo_orgowned():
|
||||||
org = Organization.request(gitea, test_org)
|
org = Organization.request(gitea, test_org)
|
||||||
repo = gitea.create_repo(org, test_repo, "descr")
|
repo = gitea.create_repo(org, test_repo, "descr")
|
||||||
|
|
Ładowanie…
Reference in New Issue