From 16f01d0e0ac5785b19dbacaf0250eed972385efe Mon Sep 17 00:00:00 2001 From: Langenfeld Date: Fri, 12 Nov 2021 14:21:54 +0100 Subject: [PATCH] fixing some of the hash functions --- gitea/gitea.py | 20 ++++++++++++++------ tests/test_api.py | 13 +++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/gitea/gitea.py b/gitea/gitea.py index 37a9267..ee91aa6 100644 --- a/gitea/gitea.py +++ b/gitea/gitea.py @@ -234,10 +234,10 @@ class Branch(GiteaApiObject): def __eq__(self, other): if not isinstance(other, Branch): return False - return self.repo == other.repo and self.name == other.name + return self.commit == other.commit and self.name == other.name def __hash__(self): - return hash(self.repo) ^ hash(self.name) + return hash(self.commit) ^ hash(self.name) fields_to_parsers = { "commit": lambda gitea, c: Commit.parse_response(gitea, c) @@ -263,6 +263,7 @@ class Repository(GiteaApiObject): REPO_TRANSFER = "/repos/{owner}/{repo}/transfer" REPO_CONTENTS = "/repos/{owner}/{repo}/contents" REPO_CONTENT = """/repos/{owner}/{repo}/contents/{filepath}""" + REPO_MILESTONES = """/repos/{owner}/{repo}/milestones""" def __init__(self, gitea): super(Repository, self).__init__(gitea) @@ -384,6 +385,13 @@ class Repository(GiteaApiObject): ) return Issue.parse_response(self.gitea, result) + def create_milestone(self, title: str, description: str, due_date: str = None, state:str = "open") -> "Milestone": + url = Repository.REPO_MILESTONES.format(owner=self.owner.username, repo=self.name) + data= {"title": title, "description": description, "state": state} + if due_date: data["due_date"] = due_date + result = self.gitea.requests_post(url, data=data) + return Milestone.parse_response(self.gitea, result) + def create_gitea_hook(self, hook_url: str, events: List[str]): url = f"/repos/{self.owner.username}/{self.name}/hooks" data = { @@ -476,10 +484,10 @@ class Milestone(GiteaApiObject): def __eq__(self, other): if not isinstance(other, Milestone): return False - return self.repo == other.repo and self.id == other.id + return self.gitea == other.gitea and self.id == other.id def __hash__(self): - return hash(self.repo) ^ hash(self.id) + return hash(self.gitea) ^ hash(self.id) fields_to_parsers = { "closed_at": lambda gitea, t: Util.convert_time(t), @@ -541,10 +549,10 @@ class Commit(GiteaApiObject): def __eq__(self, other): if not isinstance(other, Commit): return False - return self.repo == other.repo and self.ref == other.ref + return self.sha == other.sha def __hash__(self): - return hash(self.repo) ^ hash(self.ref) + return hash(self.sha) @classmethod def request(cls, gitea, owner, repo): diff --git a/tests/test_api.py b/tests/test_api.py index f185709..4030386 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -193,6 +193,19 @@ def test_create_issue(instance): assert issue.title == "TestIssue" assert issue.body == "Body text with this issue" +def test_hashing(instance): + #just call the hash function of each object to see if something bad happens + org = Organization.request(instance, test_org) + team = org.get_team(test_team) + user = instance.get_user_by_name(test_user) + #TODO test for milestones (Todo: add milestone adding) + repo = org.get_repositories()[0] + milestone = repo.create_milestone("mystone", "this is only a teststone") + issue = repo.get_issues()[0] + branch = repo.get_branches()[0] + commit = repo.get_commits()[0] + assert len(set([org, team, user, repo, issue, branch, commit, milestone])) + def test_team_get_org(instance): org = Organization.request(instance, test_org) user = instance.get_user_by_name(test_user)