Skip to content

Commit 5453e93

Browse files
da1nerdethantkoenig
authored andcommitted
Branches (#16)
* added branch interface * added get_branches * added name and description fields to repo entity
1 parent 9bae026 commit 5453e93

File tree

4 files changed

+193
-2
lines changed

4 files changed

+193
-2
lines changed

gogs_client/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from gogs_client.auth import Authentication, Token, UsernamePassword
2-
from gogs_client.entities import GogsUser, GogsRepo, GogsOrg, GogsTeam
2+
from gogs_client.entities import GogsUser, GogsRepo, GogsBranch, GogsCommit, GogsOrg, GogsTeam
33
from gogs_client.interface import GogsApi, ApiFailure, NetworkFailure
44
from gogs_client.updates import GogsUserUpdate, GogsHookUpdate

gogs_client/entities.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ class GogsRepo(GogsEntity):
104104
#: :type: entities.GogsUser
105105
owner = attr.ib(convert=lambda parsed_json: GogsUser.from_json(parsed_json))
106106

107+
#: The name of the repository
108+
#:
109+
#: :type: str
110+
name = attr.ib()
111+
107112
#: The full name of the repository
108113
#:
109114
#: :type: str
@@ -147,6 +152,11 @@ def urls(self):
147152
#: :type: GogsRepo
148153
parent = attr.ib(convert=lambda data: GogsRepo.from_json(data) if data else None, default=None)
149154

155+
#: The description of the repository
156+
#:
157+
#: :type: str
158+
description = attr.ib(default=None)
159+
150160
#: Whether the repository is empty
151161
#:
152162
#: :type: bool
@@ -274,6 +284,48 @@ class DeployKey(GogsEntity):
274284
#: :type: bool
275285
read_only = attr.ib()
276286

287+
@attr.s(frozen=True)
288+
class GogsBranch(GogsEntity):
289+
"""
290+
An immutable representation of a Gogs branch
291+
"""
292+
293+
#: The branch's name
294+
#:
295+
#: :type: str
296+
name = attr.ib()
297+
298+
#: The HEAD commit of the branch
299+
#:
300+
#: :type: entities.GogsCommit
301+
commit = attr.ib(convert=lambda parsed_json: GogsCommit.from_json(parsed_json))
302+
303+
@attr.s(frozen=True)
304+
class GogsCommit(GogsEntity):
305+
"""
306+
An immutable representation of a Gogs commit
307+
"""
308+
309+
#: The commit's id
310+
#:
311+
#: :type: str
312+
id = attr.ib()
313+
314+
#: The commit's message
315+
#:
316+
#: :type: str
317+
message = attr.ib()
318+
319+
#: The commit's url
320+
#:
321+
#: :type: str
322+
url = attr.ib()
323+
324+
#: The commit's timestamp
325+
#:
326+
#: :type: str
327+
timestamp = attr.ib()
328+
277329

278330
@attr.s(frozen=True)
279331
class GogsOrg(GogsEntity):

gogs_client/interface.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from gogs_client._implementation.http_utils import RelativeHttpRequestor, append_url
44
from gogs_client.auth import Token
5-
from gogs_client.entities import GogsUser, GogsRepo, GogsOrg, GogsTeam
5+
from gogs_client.entities import GogsUser, GogsRepo, GogsBranch, GogsOrg, GogsTeam
66

77

88
class GogsApi(object):
@@ -194,6 +194,40 @@ def get_user_repos(self, auth, username):
194194
response = self._check_ok(self._get(path, auth=auth))
195195
return [GogsRepo.from_json(repo_json) for repo_json in response.json()]
196196

197+
def get_branch(self, auth, username, repo_name, branch_name):
198+
"""
199+
Returns the branch with name ``branch_name`` in the repository with name ``repo_name``
200+
owned by the user with username ``username``.
201+
202+
:param auth.Authentication auth: authentication object
203+
:param str username: username of owner of repository containing the branch
204+
:param str repo_name: name of the repository with the branch
205+
:param str branch_name: name of the branch to return
206+
:return a branch
207+
:rtype: GogsBranch
208+
:raises NetworkFailure: if there is an error communicating with the server
209+
:raises ApiFailure: if the request cannot be serviced
210+
"""
211+
path = "/repos/{u}/{r}/branches/{b}".format(u=username, r=repo_name, b=branch_name)
212+
response = self._check_ok(self._get(path, auth=auth))
213+
return GogsBranch.from_json(response.json())
214+
215+
def get_branches(self, auth, username, repo_name):
216+
"""
217+
Returns the branches in the repository with name ``repo_name`` owned by the user with username ``username``.
218+
219+
:param auth.Authentication auth: authentication object
220+
:param str username: username of owner of repository containing the branch
221+
:param str repo_name: name of the repository with the branch
222+
:return a list of branches
223+
:rtype: List[GogsBranch]
224+
:raises NetworkFailure: if there is an error communicating with the server
225+
:raises ApiFailure: if the request cannot be serviced
226+
"""
227+
path = "/repos/{u}/{r}/branches".format(u=username, r=repo_name)
228+
response = self._check_ok(self._get(path, auth=auth))
229+
return [GogsBranch.from_json(branch_json) for branch_json in response.json()]
230+
197231
def delete_repo(self, auth, username, repo_name):
198232
"""
199233
Deletes the repository with name ``repo_name`` owned by the user with username ``username``.

tests/interface_test.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ def setUp(self):
2323
"email": "u@gogs.io",
2424
"avatar_url": "/avatars/1"
2525
},
26+
"name": "Hello-World",
2627
"full_name": "unknwon/Hello-World",
28+
"description": "Some description",
2729
"private": false,
2830
"fork": false,
2931
"parent": null,
@@ -48,7 +50,9 @@ def setUp(self):
4850
"email": "u@gogs.io",
4951
"avatar_url": "/avatars/1"
5052
},
53+
"name": "Hello-World",
5154
"full_name": "unknwon/Hello-World",
55+
"description": "Some description",
5256
"private": false,
5357
"fork": false,
5458
"parent": null,
@@ -72,6 +76,7 @@ def setUp(self):
7276
"email": "u@gogs.io",
7377
"avatar_url": "/avatars/1"
7478
},
79+
"name": "Hello-World-Again",
7580
"full_name": "unknwon/Hello-World-Again",
7681
"private": false,
7782
"fork": false,
@@ -88,6 +93,80 @@ def setUp(self):
8893
"pull": true
8994
}
9095
}]"""
96+
self.branch_json_str = """{
97+
"name": "master",
98+
"commit": {
99+
"id": "c17825309a0d52201e78a19f49948bcc89e52488",
100+
"message": "migrated to RC0.2",
101+
"url": "Not implemented",
102+
"author": {
103+
"name": "Joel Lonbeck",
104+
"email": "joel@neutrinographics.com",
105+
"username": "joel"
106+
},
107+
"committer": {
108+
"name": "Joel Lonbeck",
109+
"email": "joel@neutrinographics.com",
110+
"username": "joel"
111+
},
112+
"verification": {
113+
"verified": false,
114+
"reason": "gpg.error.not_signed_commit",
115+
"signature": "",
116+
"payload": ""
117+
},
118+
"timestamp": "2017-05-17T21:11:25Z"
119+
}
120+
}"""
121+
self.branches_list_json_str = """[{
122+
"name": "master",
123+
"commit": {
124+
"id": "c17825309a0d52201e78a19f49948bcc89e52488",
125+
"message": "migrated to RC0.2",
126+
"url": "Not implemented",
127+
"author": {
128+
"name": "Joel Lonbeck",
129+
"email": "joel@neutrinographics.com",
130+
"username": "joel"
131+
},
132+
"committer": {
133+
"name": "Joel Lonbeck",
134+
"email": "joel@neutrinographics.com",
135+
"username": "joel"
136+
},
137+
"verification": {
138+
"verified": false,
139+
"reason": "gpg.error.not_signed_commit",
140+
"signature": "",
141+
"payload": ""
142+
},
143+
"timestamp": "2017-05-17T21:11:25Z"
144+
}
145+
},{
146+
"name": "develop",
147+
"commit": {
148+
"id": "r03kd7cjr9a0d52201e78a19f49948bcc89e52488",
149+
"message": "another branch",
150+
"url": "Not implemented",
151+
"author": {
152+
"name": "Joel Lonbeck",
153+
"email": "joel@neutrinographics.com",
154+
"username": "joel"
155+
},
156+
"committer": {
157+
"name": "Joel Lonbeck",
158+
"email": "joel@neutrinographics.com",
159+
"username": "joel"
160+
},
161+
"verification": {
162+
"verified": false,
163+
"reason": "gpg.error.not_signed_commit",
164+
"signature": "",
165+
"payload": ""
166+
},
167+
"timestamp": "2017-05-17T21:11:25Z"
168+
}
169+
}]"""
91170
self.user_json_str = """{
92171
"id": 1,
93172
"username": "unknwon",
@@ -188,6 +267,7 @@ def setUp(self):
188267
"read_only": true
189268
}]"""
190269
self.expected_repo = gogs_client.GogsRepo.from_json(json.loads(self.repo_json_str))
270+
self.expected_branch = gogs_client.GogsBranch.from_json(json.loads(self.branch_json_str))
191271
self.expected_user = gogs_client.GogsUser.from_json(json.loads(self.user_json_str))
192272
self.expected_hook = gogs_client.GogsRepo.Hook.from_json(json.loads(self.hook_json_str))
193273
self.expected_org = gogs_client.GogsOrg.from_json(json.loads(self.org_json_str))
@@ -258,6 +338,21 @@ def test_delete_repo1(self):
258338
last_call = responses.calls[1]
259339
self.assertEqual(last_call.request.url, self.path_with_token(uri2))
260340

341+
@responses.activate
342+
def test_get_branch(self):
343+
uri = self.path("/repos/username/repo/branches/branch")
344+
responses.add(responses.GET, uri, body=self.branch_json_str, status=200)
345+
branch = self.client.get_branch(self.token, "username", "repo", "branch")
346+
self.assert_branches_equal(branch, self.expected_branch)
347+
348+
@responses.activate
349+
def test_get_branches(self):
350+
uri = self.path("/repos/username/repo/branches")
351+
responses.add(responses.GET, uri, body=self.branches_list_json_str, status=200)
352+
branches = self.client.get_branches(self.token, "username", "repo")
353+
self.assertEqual(len(branches), 2)
354+
self.assert_branches_equal(branches[0], self.expected_branch)
355+
261356
@responses.activate
262357
def test_create_user1(self):
263358
uri = self.path("/admin/users")
@@ -601,6 +696,16 @@ def assert_repos_equal(self, repo, expected):
601696
self.assertEqual(repo.permissions.push, expected.permissions.push)
602697
self.assertEqual(repo.permissions.pull, expected.permissions.pull)
603698

699+
def assert_branches_equal(self, branch, expected):
700+
self.assertEqual(branch.name, expected.name)
701+
self.assert_commits_equal(branch.commit, expected.commit)
702+
703+
def assert_commits_equal(self, commit, expected):
704+
self.assertEqual(commit.id, expected.id)
705+
self.assertEqual(commit.message, expected.message)
706+
self.assertEqual(commit.url, expected.url)
707+
self.assertEqual(commit.timestamp, expected.timestamp)
708+
604709
def assert_users_equals(self, user, expected):
605710
self.assertEqual(user.id, expected.id)
606711
self.assertEqual(user.username, expected.username)

0 commit comments

Comments
 (0)