kopia lustrzana https://github.com/Langenfeld/py-gitea
cont.
rodzic
79c10de569
commit
4753641296
183
gitea/gitea.py
183
gitea/gitea.py
|
@ -15,6 +15,8 @@ class NotFoundException(Exception):
|
||||||
|
|
||||||
class GiteaApiObject:
|
class GiteaApiObject:
|
||||||
|
|
||||||
|
GET_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
|
||||||
|
@ -25,18 +27,25 @@ class GiteaApiObject:
|
||||||
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
|
||||||
|
|
||||||
|
def __hash__(self):
|
||||||
|
return self.id
|
||||||
|
|
||||||
fields_to_parsers = {}
|
fields_to_parsers = {}
|
||||||
|
|
||||||
@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)'.
|
||||||
All args are put into an args tuple for passing around"""
|
All args are put into an args tuple for passing around"""
|
||||||
return cls._request(gitea, (id))
|
return cls._request(gitea, {"id":id})
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _request(cls, gitea, args):
|
def _request(cls, gitea, args):
|
||||||
result = cls._get_gitea_api_object(gitea, args)
|
result = cls._get_gitea_api_object(gitea, args)
|
||||||
return cls.parse_request(gitea, result)
|
api_object = cls.parse_request(gitea, result)
|
||||||
|
api_object = cls.parse_request(gitea, result)
|
||||||
|
for key, value in args.items(): # hack: not all necessary request args in api result (e.g. repo name in issue)
|
||||||
|
setattr(api_object, key, value)
|
||||||
|
return api_object
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def parse_request(cls, gitea, result):
|
def parse_request(cls, gitea, result):
|
||||||
|
@ -48,22 +57,23 @@ class GiteaApiObject:
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _get_gitea_api_object(cls, gitea, args):
|
def _get_gitea_api_object(cls, gitea, args):
|
||||||
"""Make the conctrete request to gitea-api"""
|
"""Retrieving an object always as GET_API_OBJECT """
|
||||||
return {"id":args[0]}
|
return gitea.requests_get(cls.GET_API_OBJECT.format(**args))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _initialize(cls, gitea, api_object, result):
|
def _initialize(cls, gitea, api_object, result):
|
||||||
for i, v in result.items():
|
for i, v in result.items():
|
||||||
if i in cls.fields_to_parsers:
|
if i in cls.fields_to_parsers and v is not None:
|
||||||
parse_func = cls.fields_to_parsers[i]
|
parse_func = cls.fields_to_parsers[i]
|
||||||
setattr(api_object, i, parse_func(gitea, v))
|
setattr(api_object, i, parse_func(gitea, v))
|
||||||
else:
|
else:
|
||||||
setattr(api_object, i, v)
|
setattr(api_object, i, v)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Organization(GiteaApiObject):
|
class Organization(GiteaApiObject):
|
||||||
|
|
||||||
ORG_REQUEST = """/orgs/%s""" # <org>
|
GET_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>
|
||||||
|
@ -76,11 +86,8 @@ class Organization(GiteaApiObject):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def request(cls, gitea, name):
|
def request(cls, gitea, name):
|
||||||
return cls._request(gitea, (name))
|
return cls._request(gitea, {"name": name})
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _get_gitea_api_object(cls, gitea, args):
|
|
||||||
return gitea.requests_get(cls.ORG_REQUEST % args)
|
|
||||||
|
|
||||||
# oldstuff
|
# oldstuff
|
||||||
|
|
||||||
|
@ -152,8 +159,8 @@ class Organization(GiteaApiObject):
|
||||||
|
|
||||||
class User(GiteaApiObject):
|
class User(GiteaApiObject):
|
||||||
|
|
||||||
|
GET_API_OBJECT = """/users/{name}""" # <org>
|
||||||
USER_MAIL = """/user/emails?sudo=%s""" # <name>
|
USER_MAIL = """/user/emails?sudo=%s""" # <name>
|
||||||
USER_REQUEST = """/users/%s""" # <org>
|
|
||||||
USER_REPOS_REQUEST = """/users/%s/repos""" # <org>
|
USER_REPOS_REQUEST = """/users/%s/repos""" # <org>
|
||||||
USER_PATCH = """/admin/users/%s""" # <username>
|
USER_PATCH = """/admin/users/%s""" # <username>
|
||||||
ADMIN_DELETE_USER = """/admin/users/%s""" # <username>
|
ADMIN_DELETE_USER = """/admin/users/%s""" # <username>
|
||||||
|
@ -164,11 +171,7 @@ class User(GiteaApiObject):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def request(cls, gitea, name):
|
def request(cls, gitea, name):
|
||||||
return cls._request(gitea, (name))
|
return cls._request(gitea, {"name": name})
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _get_gitea_api_object(cls, gitea, args):
|
|
||||||
return gitea.requests_get(cls.USER_REQUEST % args)
|
|
||||||
|
|
||||||
|
|
||||||
def get_repositories(self):
|
def get_repositories(self):
|
||||||
|
@ -231,7 +234,7 @@ class User(GiteaApiObject):
|
||||||
|
|
||||||
class Repository(GiteaApiObject):
|
class Repository(GiteaApiObject):
|
||||||
|
|
||||||
REPO_REQUEST = """/repos/%s/%s""" # <owner>, <reponame>
|
GET_API_OBJECT = """/repos/{owner}/{name}""" # <owner>, <reponame>
|
||||||
REPO_SEARCH = """/repos/search/%s""" # <reponame>
|
REPO_SEARCH = """/repos/search/%s""" # <reponame>
|
||||||
REPO_BRANCHES = """/repos/%s/%s/branches""" # <owner>, <reponame>
|
REPO_BRANCHES = """/repos/%s/%s/branches""" # <owner>, <reponame>
|
||||||
REPO_ISSUES = """/repos/%s/%s/issues""" # <owner, reponame>
|
REPO_ISSUES = """/repos/%s/%s/issues""" # <owner, reponame>
|
||||||
|
@ -247,12 +250,8 @@ class Repository(GiteaApiObject):
|
||||||
}
|
}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def request(cls, gitea, name):
|
def request(cls, gitea, owner, name):
|
||||||
return cls._request(gitea, (name))
|
return cls._request(gitea, {"owner": owner, "name": name})
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _get_gitea_api_object(cls, gitea, args):
|
|
||||||
return gitea.requests_get(cls.REPO_REQUEST % args)
|
|
||||||
|
|
||||||
def get_branches(self):
|
def get_branches(self):
|
||||||
"""Get all the Branches of this Repository.
|
"""Get all the Branches of this Repository.
|
||||||
|
@ -291,7 +290,12 @@ class Repository(GiteaApiObject):
|
||||||
if len(results) <= 0:
|
if len(results) <= 0:
|
||||||
break
|
break
|
||||||
index += 1
|
index += 1
|
||||||
issues += [Issue(self, result["id"], result) for result in results]
|
for result in results:
|
||||||
|
issue = Issue.parse_request(self.gitea, result)
|
||||||
|
#again a hack because this infomation gets lost after the api call
|
||||||
|
setattr(issue, "repo", self.name)
|
||||||
|
setattr(issue, "owner", self.owner.username)
|
||||||
|
issues.append(issue)
|
||||||
return issues
|
return issues
|
||||||
|
|
||||||
def get_user_time(self, username, ignore_above=8):
|
def get_user_time(self, username, ignore_above=8):
|
||||||
|
@ -330,128 +334,47 @@ class Repository(GiteaApiObject):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class Milestone:
|
class Milestone(GiteaApiObject):
|
||||||
"""Reperesents a Milestone in Gitea.
|
"""Reperesents a Milestone in Gitea.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
GET = """/repos/%s/%s/milestones/%s""" # <owner, repo, id>
|
GET_API_OBJECT = """/repos/{owner}/{repo}/milestones/{number}""" # <owner, repo, id>
|
||||||
|
|
||||||
def __init__(self, repo: Repository, id: int, initJson: json = None):
|
def __init__(self, gitea, id: int):
|
||||||
""" Initializes a Milestone.
|
super(Milestone, self).__init__(gitea, id=id)
|
||||||
|
|
||||||
Args:
|
@classmethod
|
||||||
repo (Repository): The Repository of this Milestone.
|
def request(cls, gitea, owner, repo, number):
|
||||||
id (int): The id of the Milestone.
|
return cls._request(gitea, {"owner":owner, "repo":repo, "number":number})
|
||||||
initJson (dict): Optional, init information for Milestone.
|
|
||||||
|
|
||||||
Warning:
|
|
||||||
This does not create a Milestone. <sth> does.
|
|
||||||
|
|
||||||
Throws:
|
|
||||||
NotFoundException, if the Milestone could not be found.
|
|
||||||
"""
|
|
||||||
self.gitea = repo.gitea
|
|
||||||
self.__initialize_milestone(repo, id, initJson)
|
|
||||||
|
|
||||||
def __initialize_milestone(self, repository, id, result):
|
|
||||||
""" Initializes a Milestone.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
repo (Repository): The Repository of this Milestone.
|
|
||||||
id (int): The id of the Milestone.
|
|
||||||
initJson (dict): Optional, init information for Milestone.
|
|
||||||
|
|
||||||
Throws:
|
|
||||||
NotFoundException, if the Milestone could not be found.
|
|
||||||
"""
|
|
||||||
if not result:
|
|
||||||
result = self.gitea.requests_get(
|
|
||||||
Milestone.GET % (repository.owner.username, repository.name, id)
|
|
||||||
)
|
|
||||||
logging.debug(
|
|
||||||
"Milestone found: %s/%s/%s: %s"
|
|
||||||
% (repository.owner.username, repository.name, id, result["title"])
|
|
||||||
)
|
|
||||||
for i, v in result.items():
|
|
||||||
setattr(self, i, v)
|
|
||||||
self.repository = repository
|
|
||||||
|
|
||||||
def __eq__(self, other):
|
|
||||||
if other is not None:
|
|
||||||
if isinstance(other, Milestone):
|
|
||||||
return other.id == self.id
|
|
||||||
return False
|
|
||||||
|
|
||||||
def __hash__(self):
|
|
||||||
return self.id
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return "Milestone: '%s'" % self.title
|
|
||||||
|
|
||||||
def full_print(self):
|
def full_print(self):
|
||||||
return str(vars(self))
|
return str(vars(self))
|
||||||
|
|
||||||
|
|
||||||
class Issue:
|
class Issue(GiteaApiObject):
|
||||||
"""Reperestents an Issue in Gitea.
|
|
||||||
"""
|
|
||||||
|
|
||||||
GET = """/repos/%s/%s/issues/%s""" # <owner, repo, index>
|
GET_API_OBJECT = """/repos/{owner}/{repo}/issues/{number}""" # <owner, repo, index>
|
||||||
GET_TIME = """/repos/%s/%s/issues/%s/times""" # <owner, repo, index>
|
GET_TIME = """/repos/%s/%s/issues/%s/times""" # <owner, repo, index>
|
||||||
|
|
||||||
def __init__(self, repo: Repository, id: int, initJson: json = None):
|
closed = "closed"
|
||||||
""" Initializes a Issue.
|
open = "open"
|
||||||
|
|
||||||
Args:
|
def __init__(self, gitea, id: int):
|
||||||
repo (Repository): The Repository of this Issue.
|
super(Issue, self).__init__(gitea, id=id)
|
||||||
id (int): The id of the Issue.
|
|
||||||
initJson (dict): Optional, init information for Issue.
|
|
||||||
|
|
||||||
Warning:
|
fields_to_parsers = {
|
||||||
This does not create an Issue. <sth> does.
|
"milestone": lambda gitea, m: Milestone.parse_request(gitea, m),
|
||||||
|
"user": lambda gitea, u: User.parse_request(gitea, u),
|
||||||
|
"assignee": lambda gitea, u: User.parse_request(gitea, u),
|
||||||
|
"assignees": lambda gitea, us: [User.parse_request(gitea, u) for u in us],
|
||||||
|
"state": lambda gitea, s: Issue.closed if s == "closed" else Issue.open
|
||||||
|
}
|
||||||
|
|
||||||
Throws:
|
@classmethod
|
||||||
NotFoundException, if the Issue could not be found.
|
def request(cls, gitea, owner, repo, number):
|
||||||
"""
|
api_object = cls._request(gitea, {"owner":owner, "repo":repo, "number":number})
|
||||||
self.gitea = repo.gitea
|
return api_object
|
||||||
self.__initialize_issue(repo, id, initJson)
|
|
||||||
|
|
||||||
def __initialize_issue(self, repository, id, result):
|
|
||||||
""" Initializing an Issue.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
repo (Repository): The Repository of this Issue.
|
|
||||||
id (int): The id of the Issue.
|
|
||||||
initJson (dict): Optional, init information for Issue.
|
|
||||||
|
|
||||||
Throws:
|
|
||||||
NotFoundException, if the Issue could not be found.
|
|
||||||
"""
|
|
||||||
if not result:
|
|
||||||
result = self.gitea.requests_get(
|
|
||||||
Issue.GET % (repository.owner.username, repository.name, id)
|
|
||||||
)
|
|
||||||
logging.debug(
|
|
||||||
"Issue found: %s/%s/%s: %s"
|
|
||||||
% (repository.owner.username, repository.name, id, result["title"])
|
|
||||||
)
|
|
||||||
for i, v in result.items():
|
|
||||||
setattr(self, i, v)
|
|
||||||
self.repository = repository
|
|
||||||
self.milestone = (
|
|
||||||
Milestone(repository, self.milestone["id"], self.milestone)
|
|
||||||
if self.milestone
|
|
||||||
else self.milestone
|
|
||||||
)
|
|
||||||
|
|
||||||
def __eq__(self, other):
|
|
||||||
if other is not None:
|
|
||||||
if isinstance(other, Milestone):
|
|
||||||
return other.id == self.id
|
|
||||||
return False
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return "#%i %s" % (self.id, self.title)
|
|
||||||
|
|
||||||
def get_estimate_sum(self):
|
def get_estimate_sum(self):
|
||||||
"""Returns the summed estimate-labeled values"""
|
"""Returns the summed estimate-labeled values"""
|
||||||
|
@ -467,7 +390,7 @@ class Issue:
|
||||||
return sum(
|
return sum(
|
||||||
(t["time"] // 60) / 60
|
(t["time"] // 60) / 60
|
||||||
for t in self.gitea.requests_get(
|
for t in self.gitea.requests_get(
|
||||||
Issue.GET_TIME % (self.repository.owner.username, self.repository.name, self.number)
|
Issue.GET_TIME % (self.owner, self.repo, self.number)
|
||||||
)
|
)
|
||||||
if user_id and t["user_id"] == user_id
|
if user_id and t["user_id"] == user_id
|
||||||
)
|
)
|
||||||
|
|
Ładowanie…
Reference in New Issue