Skip to content

Commit 1c832e9

Browse files
committed
Repos commits added
1 parent 37a9c5d commit 1c832e9

File tree

4 files changed

+234
-8
lines changed

4 files changed

+234
-8
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
4+
from . import Request
5+
from pygithub3.resources.repos import Commit, GitCommit, Comment, Diff
6+
7+
8+
class List(Request):
9+
10+
uri = 'repos/{user}/{repo}/commits'
11+
resource = GitCommit
12+
13+
14+
class Get(Request):
15+
16+
uri = 'repos/{user}/{repo}/commits/{sha}'
17+
resource = Commit
18+
19+
20+
class List_comments(Request):
21+
22+
uri = 'repos/{user}/{repo}/comments'
23+
resource = Comment
24+
25+
def clean_uri(self):
26+
if self.sha:
27+
return 'repos/{user}/{repo}/commits/{sha}/comments'
28+
29+
30+
class Create_comment(Request):
31+
32+
uri = 'repos/{user}/{repo}/commits/{sha}/comments'
33+
resource = Comment
34+
body_schema = {
35+
'schema': ('body', 'commit_id', 'line', 'path', 'position'),
36+
'required': ('body', 'commit_id', 'line', 'path', 'position'),
37+
}
38+
39+
40+
class Get_comment(Request):
41+
42+
uri = 'repos/{user}/{repo}/comments/{comment_id}'
43+
resource = Comment
44+
45+
46+
class Update_comment(Request):
47+
48+
uri = 'repos/{user}/{repo}/comments/{comment_id}'
49+
resource = Comment
50+
body_schema = {
51+
'schema': ('body', ),
52+
'required': ('body', ),
53+
}
54+
55+
56+
class Compare(Request):
57+
58+
uri = 'repos/{user}/{repo}/compare/{base}...{head}'
59+
resource = Diff
60+
61+
62+
class Delete_comment(Request):
63+
64+
uri = 'repos/{user}/{repo}/comments/{comment_id}'

pygithub3/resources/repos.py

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
from .users import User
66
from .orgs import Org
77

8-
__all__ = ('Repo', )
9-
108

119
class Repo(Resource):
1210

@@ -24,25 +22,81 @@ def __str__(self):
2422
return '<Team (%s)>' % getattr(self, 'name', '')
2523

2624

25+
class Author(Resource):
26+
27+
_dates = ('date')
28+
29+
def __str__(self):
30+
return '<Author (%s)>' % getattr(self, 'name', '')
31+
32+
33+
class Committer(Resource):
34+
35+
_dates = ('date')
36+
37+
def __str__(self):
38+
return '<Committer (%s)>' % getattr(self, 'name', '')
39+
40+
41+
class GitCommit(Resource):
42+
43+
_maps = {'author': Author, 'committer': Committer, 'tree': 'self'}
44+
_collection_maps = {'parents': 'self'}
45+
46+
def __str__(self):
47+
return '<GitCommit (%s:%s)>' % (getattr(self, 'sha', ''),
48+
getattr(self, 'message', ''))
49+
50+
51+
class Stats(Resource):
52+
pass
53+
54+
55+
class File(Resource):
56+
57+
def __str__(self):
58+
return '<File (%s)>' % getattr(self, 'filename', '')
59+
60+
2761
class Commit(Resource):
2862

63+
_maps = {'commit': GitCommit, 'author': User, 'committer': User,
64+
'stats': Stats}
65+
_collection_maps = {'parents': GitCommit, 'files': File}
66+
67+
def __str__(self):
68+
return '<Commit (%s)>' % getattr(self, 'author', '')
69+
70+
71+
class Comment(Resource):
72+
73+
_dates = ('created_at', 'updated_at')
74+
_maps = {'user': User}
75+
76+
def __str__(self):
77+
return '<Comment (%s)>' % getattr(self, 'user', '')
78+
79+
80+
class Diff(Resource):
81+
82+
_maps = {'base_commit': Commit}
83+
_collection_maps = {'commits': Commit, 'files': File}
84+
2985
def __str__(self):
30-
return '<Commit (%s:%s)>' % (
31-
getattr(self, 'sha', ''),
32-
getattr(self, 'message', ''))
86+
return '<Diff (%s)>' % getattr(self, 'status', '')
3387

3488

3589
class Tag(Resource):
3690

37-
_maps = {'commit': Commit}
91+
_maps = {'commit': GitCommit}
3892

3993
def __str__(self):
4094
return '<Tag (%s)>' % getattr(self, 'name', '')
4195

4296

4397
class Branch(Resource):
4498

45-
_maps = {'commit': Commit}
99+
_maps = {'commit': GitCommit}
46100

47101
def __str__(self):
48102
return '<Branch (%s)>' % getattr(self, 'name', '')

pygithub3/services/repos.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,49 @@
44
from .base import Service
55

66

7+
class Commits(Service):
8+
9+
""" TODO: Pagination structure differs from usual
10+
def list(self, user=None, repo=None, sha='', path=''):
11+
request = self.make_request('repos.commits.list', user=user, repo=repo)
12+
return self._get_result(request, sha=sha, path=path)
13+
"""
14+
15+
def get(self, sha, user=None, repo=None):
16+
request = self.make_request('repos.commits.get',
17+
sha=sha, user=user, repo=repo)
18+
return self._get(request)
19+
20+
def list_comments(self, sha=None, user=None, repo=None):
21+
request = self.make_request('repos.commits.list_comments',
22+
sha=sha, user=user, repo=repo)
23+
return self._get_result(request)
24+
25+
def create_comment(self, data, sha, user=None, repo=None):
26+
request = self.make_request('repos.commits.create_comment',
27+
sha=sha, user=user, repo=repo, body=data)
28+
return self._post(request)
29+
30+
def get_comment(self, cid, user=None, repo=None):
31+
request = self.make_request('repos.commits.get_comment',
32+
comment_id=cid, user=user, repo=repo)
33+
return self._get(request)
34+
35+
def update_comment(self, data, cid, user=None, repo=None):
36+
request = self.make_request('repos.commits.update_comment',
37+
comment_id=cid, user=user, repo=repo, body=data)
38+
return self._patch(request)
39+
40+
def compare(self, base, head, user=None, repo=None):
41+
request = self.make_request('repos.commits.compare',
42+
base=base, head=head, user=user, repo=repo)
43+
return self._get(request)
44+
45+
def delete_comment(self, cid, user=None, repo=None):
46+
request = self.make_request('repos.commits.delete_comment',
47+
comment_id=cid, user=user, repo=repo)
48+
self._delete(request)
49+
750
class Collaborator(Service):
851

952
def list(self, user=None, repo=None):

pygithub3/tests/services/test_repos.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import requests
77
from mock import patch, Mock
88

9-
from pygithub3.services.repos import Repo, Collaborator
9+
from pygithub3.services.repos import Repo, Collaborator, Commits
1010
from pygithub3.resources.base import json
1111
from pygithub3.tests.utils.base import mock_response, mock_response_result
1212
from pygithub3.tests.utils.services import _, mock_json
@@ -170,3 +170,68 @@ def test_DELETE(self, request_method):
170170
self.cs.delete('user')
171171
self.assertEqual(request_method.call_args[0],
172172
('delete', _('repos/octocat/oc_repo/collaborators/user')))
173+
174+
175+
@patch.object(requests.sessions.Session, 'request')
176+
class TestCommitsService(TestCase):
177+
178+
def setUp(self):
179+
self.cs = Commits(user='oct', repo='re_oct')
180+
181+
"""
182+
def test_LIST(self, request_method):
183+
request_method.return_value = mock_response_result()
184+
self.cs.list().all()
185+
self.assertEqual(request_method.call_args[0],
186+
('get', _('repos/oct/re_oct/commits')))
187+
"""
188+
189+
def test_GET(self, request_method):
190+
request_method.return_value = mock_response()
191+
self.cs.get('e3bc')
192+
self.assertEqual(request_method.call_args[0],
193+
('get', _('repos/oct/re_oct/commits/e3bc')))
194+
195+
def test_LIST_comments(self, request_method):
196+
request_method.return_value = mock_response_result()
197+
self.cs.list_comments().all()
198+
self.assertEqual(request_method.call_args[0],
199+
('get', _('repos/oct/re_oct/comments')))
200+
201+
def test_LIST_comments_for_commit(self, request_method):
202+
request_method.return_value = mock_response_result()
203+
self.cs.list_comments(sha='e3bc').all()
204+
self.assertEqual(request_method.call_args[0],
205+
('get', _('repos/oct/re_oct/commits/e3bc/comments')))
206+
207+
def test_CREATE_comment(self, request_method):
208+
request_method.return_value = mock_response('post')
209+
data = dict(body='some', commit_id='e2bc',
210+
line=1, path='some.txt', position=1)
211+
self.cs.create_comment(data, 'e3bc')
212+
self.assertEqual(request_method.call_args[0],
213+
('post', _('repos/oct/re_oct/commits/e3bc/comments')))
214+
215+
def test_GET_comment(self, request_method):
216+
request_method.return_value = mock_response()
217+
self.cs.get_comment(1)
218+
self.assertEqual(request_method.call_args[0],
219+
('get', _('repos/oct/re_oct/comments/1')))
220+
221+
def test_UPDATE_comment(self, request_method):
222+
request_method.return_value = mock_response('patch')
223+
self.cs.update_comment({'body': 'changed'}, 1)
224+
self.assertEqual(request_method.call_args[0],
225+
('patch', _('repos/oct/re_oct/comments/1')))
226+
227+
def test_COMPARE(self, request_method):
228+
request_method.return_value = mock_response()
229+
self.cs.compare('develop', 'master')
230+
self.assertEqual(request_method.call_args[0],
231+
('get', _('repos/oct/re_oct/compare/develop...master')))
232+
233+
def test_DELETE_comment(self, request_method):
234+
request_method.return_value = mock_response('delete')
235+
self.cs.delete_comment(1)
236+
self.assertEqual(request_method.call_args[0],
237+
('delete', _('repos/oct/re_oct/comments/1')))

0 commit comments

Comments
 (0)