kopia lustrzana https://github.com/Langenfeld/py-gitea
added ability to create branches
rodzic
57034a6756
commit
cee702f6f9
|
@ -32,7 +32,10 @@ class BasicGiteaApiObject:
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def parse_response(cls, gitea, result):
|
def parse_response(cls, gitea, result):
|
||||||
id = int(result["id"])
|
if "id" in result:
|
||||||
|
id = int(result["id"])
|
||||||
|
else:
|
||||||
|
id = hash(result.items)
|
||||||
# gitea.logger.debug("Found api object of type %s (id: %s)" % (type(cls), id))
|
# gitea.logger.debug("Found api object of type %s (id: %s)" % (type(cls), id))
|
||||||
api_object = cls(gitea, id=id)
|
api_object = cls(gitea, id=id)
|
||||||
cls._initialize(gitea, api_object, result)
|
cls._initialize(gitea, api_object, result)
|
||||||
|
|
|
@ -38,22 +38,29 @@ class Organization(GiteaApiObject):
|
||||||
self.gitea.requests_patch(Organization.PATCH_API_OBJECT.format(**args), data=values)
|
self.gitea.requests_patch(Organization.PATCH_API_OBJECT.format(**args), data=values)
|
||||||
self.dirty_fields = {}
|
self.dirty_fields = {}
|
||||||
|
|
||||||
def get_repositories(self) -> List[GiteaApiObject]:
|
def get_repositories(self) -> List['Repository']:
|
||||||
results = self.gitea.requests_get(Organization.ORG_REPOS_REQUEST % self.username)
|
results = self.gitea.requests_get(Organization.ORG_REPOS_REQUEST % self.username)
|
||||||
return [Repository.parse_response(self.gitea, result) for result in results]
|
return [Repository.parse_response(self.gitea, result) for result in results]
|
||||||
|
|
||||||
def get_teams(self) -> List[GiteaApiObject]:
|
def get_repository(self, name) -> 'Repository':
|
||||||
|
repos = self.get_repositories()
|
||||||
|
for repo in repos:
|
||||||
|
if repo.name == name:
|
||||||
|
return repo
|
||||||
|
raise NotFoundException("Repository %s not existent in organization." % name)
|
||||||
|
|
||||||
|
def get_teams(self) -> List['Team']:
|
||||||
results = self.gitea.requests_get(Organization.ORG_TEAMS_REQUEST % self.username)
|
results = self.gitea.requests_get(Organization.ORG_TEAMS_REQUEST % self.username)
|
||||||
return [Team.parse_response(self.gitea, result) for result in results]
|
return [Team.parse_response(self.gitea, result) for result in results]
|
||||||
|
|
||||||
def get_team(self, name):
|
def get_team(self, name) -> 'Team':
|
||||||
teams = self.get_teams()
|
teams = self.get_teams()
|
||||||
for team in teams:
|
for team in teams:
|
||||||
if team.name == name:
|
if team.name == name:
|
||||||
return team
|
return team
|
||||||
raise NotFoundException("Team not existent in organization.")
|
raise NotFoundException("Team not existent in organization.")
|
||||||
|
|
||||||
def get_members(self) -> List[GiteaApiObject]:
|
def get_members(self) -> List['User']:
|
||||||
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_response(self.gitea, result) for result in results]
|
return [User.parse_response(self.gitea, result) for result in results]
|
||||||
|
|
||||||
|
@ -147,6 +154,17 @@ class User(GiteaApiObject):
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
class Branch(GiteaApiObject):
|
||||||
|
GET_API_OBJECT = """/repos/%s/%s/branches/%s""" # <owner>, <repo>, <ref>
|
||||||
|
|
||||||
|
def __init__(self, gitea, id: int):
|
||||||
|
super(Branch, self).__init__(gitea, id=id)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def request(cls, gitea, owner, repo, ref):
|
||||||
|
return cls._request(gitea, {"owner": owner, "repo": repo, "ref": ref})
|
||||||
|
|
||||||
|
|
||||||
class Repository(GiteaApiObject):
|
class Repository(GiteaApiObject):
|
||||||
REPO_IS_COLLABORATOR = """/repos/%s/%s/collaborators/%s""" # <owner>, <reponame>, <username>
|
REPO_IS_COLLABORATOR = """/repos/%s/%s/collaborators/%s""" # <owner>, <reponame>, <username>
|
||||||
GET_API_OBJECT = """/repos/{owner}/{name}""" # <owner>, <reponame>
|
GET_API_OBJECT = """/repos/{owner}/{name}""" # <owner>, <reponame>
|
||||||
|
@ -182,11 +200,21 @@ class Repository(GiteaApiObject):
|
||||||
results = self.gitea.requests_get(Repository.REPO_BRANCHES % (self.owner.username, self.name))
|
results = self.gitea.requests_get(Repository.REPO_BRANCHES % (self.owner.username, self.name))
|
||||||
return [Branch.parse_response(self.gitea, result) for result in results]
|
return [Branch.parse_response(self.gitea, result) for result in results]
|
||||||
|
|
||||||
def get_issues(self) -> List[GiteaApiObject]:
|
def add_branch(self, create_from: Branch, newname: str) -> 'Commit':
|
||||||
|
"""Add a branch to the repository"""
|
||||||
|
# Note: will only work with gitea 1.13 or higher!
|
||||||
|
data = {
|
||||||
|
"new_branch_name": newname,
|
||||||
|
"old_branch_name": create_from.name
|
||||||
|
}
|
||||||
|
result = self.gitea.requests_post(Repository.REPO_BRANCHES % (self.owner.username, self.name), data=data)
|
||||||
|
return Commit.parse_response(self.gitea, result)
|
||||||
|
|
||||||
|
def get_issues(self) -> List['Issue']:
|
||||||
"""Get all Issues of this Repository (open and closed)"""
|
"""Get all Issues of this Repository (open and closed)"""
|
||||||
return self.get_issues_state(Issue.open) + self.get_issues_state(Issue.closed)
|
return self.get_issues_state(Issue.open) + self.get_issues_state(Issue.closed)
|
||||||
|
|
||||||
def get_commits(self) -> List[GiteaApiObject]:
|
def get_commits(self) -> List['Commit']:
|
||||||
"""Get all the Commits of this Repository."""
|
"""Get all the Commits of this Repository."""
|
||||||
try:
|
try:
|
||||||
results = self.gitea.requests_get_commits(Repository.REPO_COMMITS % (self.owner.username, self.name))
|
results = self.gitea.requests_get_commits(Repository.REPO_COMMITS % (self.owner.username, self.name))
|
||||||
|
@ -196,7 +224,7 @@ class Repository(GiteaApiObject):
|
||||||
results = []
|
results = []
|
||||||
return [Commit.parse_response(self.gitea, result) for result in results]
|
return [Commit.parse_response(self.gitea, result) for result in results]
|
||||||
|
|
||||||
def get_issues_state(self, state) -> List[GiteaApiObject]:
|
def get_issues_state(self, state) -> List['Issue']:
|
||||||
"""Get issues of state Issue.open or Issue.closed of a repository."""
|
"""Get issues of state Issue.open or Issue.closed of a repository."""
|
||||||
assert state in [Issue.OPENED, Issue.CLOSED]
|
assert state in [Issue.OPENED, Issue.CLOSED]
|
||||||
issues = []
|
issues = []
|
||||||
|
@ -370,17 +398,6 @@ class Issue(GiteaApiObject):
|
||||||
return [comment for comment in allProjectComments if comment.issue_url.endswith("/" + str(self.number))]
|
return [comment for comment in allProjectComments if comment.issue_url.endswith("/" + str(self.number))]
|
||||||
|
|
||||||
|
|
||||||
class Branch(GiteaApiObject):
|
|
||||||
GET_API_OBJECT = """/repos/%s/%s/branches/%s""" # <owner>, <repo>, <ref>
|
|
||||||
|
|
||||||
def __init__(self, gitea, id: int):
|
|
||||||
super(Branch, self).__init__(gitea, id=id)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def request(cls, gitea, owner, repo, ref):
|
|
||||||
return cls._request(gitea, {"owner": owner, "repo": repo, "ref": ref})
|
|
||||||
|
|
||||||
|
|
||||||
class Team(GiteaApiObject):
|
class Team(GiteaApiObject):
|
||||||
GET_API_OBJECT = """/teams/{id}""" # <id>
|
GET_API_OBJECT = """/teams/{id}""" # <id>
|
||||||
ADD_USER = """/teams/%s/members/%s""" # <id, username to add>
|
ADD_USER = """/teams/%s/members/%s""" # <id, username to add>
|
||||||
|
|
|
@ -111,6 +111,21 @@ def test_create_repo_orgowned(instance):
|
||||||
assert repo.name == test_repo
|
assert repo.name == test_repo
|
||||||
assert not repo.private
|
assert not repo.private
|
||||||
|
|
||||||
|
def test_list_branches(instance):
|
||||||
|
org = Organization.request(instance, test_org)
|
||||||
|
repo = org.get_repository(test_repo)
|
||||||
|
branches = repo.get_branches()
|
||||||
|
assert len(branches) > 0
|
||||||
|
master = [b for b in branches if b.name == "master"]
|
||||||
|
assert len(master) > 0
|
||||||
|
|
||||||
|
def test_create_branch(instance):
|
||||||
|
org = Organization.request(instance, test_org)
|
||||||
|
repo = org.get_repository(test_repo)
|
||||||
|
branches = repo.get_branches()
|
||||||
|
master = [b for b in branches if b.name == "master"]
|
||||||
|
assert len(master) > 0
|
||||||
|
repo.add_branch(master[0], "test123")
|
||||||
|
|
||||||
def test_create_team(instance):
|
def test_create_team(instance):
|
||||||
org = Organization.request(instance, test_org)
|
org = Organization.request(instance, test_org)
|
||||||
|
|
Ładowanie…
Reference in New Issue