Skip to content

Commit b9ad335

Browse files
committed
Get repository's commits supported with new result
1 parent 14ef05b commit b9ad335

File tree

8 files changed

+60
-33
lines changed

8 files changed

+60
-33
lines changed

pygithub3/core/result/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# -*- encoding: utf-8 -*-

pygithub3/core/result/link.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def params(self):
2121
for param, values in parse_qs(self.query).items()])
2222

2323
def __init__(self, object_):
24-
super(Link, self).__init__(object_)
24+
str.__init__(object_)
2525
parsed = parse_link_value(self)
2626
for url in parsed:
2727
setattr(self, parsed[url]['rel'], Link.Url(url))

pygithub3/core/result/normal.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,29 +87,29 @@ class Result(base.Result):
8787

8888
def __init__(self, method):
8989
super(Result, self).__init__(method)
90-
self.counter = 0
91-
self.cached = False
90+
self._counter = 0
91+
self._cached = False
9292

93-
def get_cached(func):
93+
def _get_cached(func):
9494
def wrapper(self):
95-
if self.cached:
96-
if str(self.counter) in self.getter.cache:
97-
page = Page(self.getter, self.counter)
98-
self.counter += 1
95+
if self._cached:
96+
if str(self._counter) in self.getter.cache:
97+
page = Page(self.getter, self._counter)
98+
self._counter += 1
9999
return page
100100
self._reset()
101101
raise StopIteration
102102
return func(self)
103103
return wrapper
104104

105-
@get_cached
105+
@_get_cached
106106
def __next__(self):
107107
if self.getter.next:
108-
self.counter += 1
109-
return Page(self.getter, self.counter)
108+
self._counter += 1
109+
return Page(self.getter, self._counter)
110110
self._reset()
111111
raise StopIteration
112112

113113
def _reset(self):
114-
self.counter = 1
115-
self.cached = True
114+
self._counter = 1
115+
self._cached = True

pygithub3/requests/repos/commits.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- encoding: utf-8 -*-
33

44
from . import Request
5-
from pygithub3.resources.repos import Commit, GitCommit, Comment, Diff
5+
from pygithub3.resources.repos import GitCommit, Comment, Diff
66

77

88
class List(Request):
@@ -14,7 +14,7 @@ class List(Request):
1414
class Get(Request):
1515

1616
uri = 'repos/{user}/{repo}/commits/{sha}'
17-
resource = Commit
17+
resource = GitCommit
1818

1919

2020
class List_comments(Request):

pygithub3/resources/repos.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,13 @@ def __str__(self):
4040
return '<Committer (%s)>' % getattr(self, 'name', '')
4141

4242

43-
class GitCommit(Resource):
43+
class Commit(Resource):
4444

45-
_maps = {'author': Author, 'committer': Committer, 'tree': 'self'}
46-
_collection_maps = {'parents': 'self'}
45+
_maps = {'author': Author, 'committer': Committer}
46+
#_maps.update({'tree': GitCommit})
4747

4848
def __str__(self):
49-
return '<GitCommit (%s:%s)>' % (getattr(self, 'sha', ''),
50-
getattr(self, 'message', ''))
49+
return '<Commit (%s)>' % getattr(self, 'author', '')
5150

5251

5352
class Stats(Resource):
@@ -60,14 +59,14 @@ def __str__(self):
6059
return '<File (%s)>' % getattr(self, 'filename', '')
6160

6261

63-
class Commit(Resource):
62+
class GitCommit(Resource):
6463

65-
_maps = {'commit': GitCommit, 'author': User, 'committer': User,
64+
_maps = {'author': User, 'committer': User, 'commit': Commit,
6665
'stats': Stats}
67-
_collection_maps = {'parents': GitCommit, 'files': File}
66+
_collection_maps = {'parents': 'self', 'files': File}
6867

6968
def __str__(self):
70-
return '<Commit (%s)>' % getattr(self, 'author', '')
69+
return '<GitCommit (%s)>' % getattr(self, 'sha', '')
7170

7271

7372
class Comment(Resource):

pygithub3/services/base.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- encoding: utf-8 -*-
33

44
from pygithub3.core.client import Client
5-
from pygithub3.core import result
5+
from pygithub3.core.result import smart, normal
66
from pygithub3.requests.base import Factory
77
from pygithub3.core.errors import NotFound
88

@@ -138,8 +138,12 @@ def _get(self, request, **kwargs):
138138
return request.resource.loads(response.content)
139139

140140
def _get_result(self, request, **kwargs):
141-
method = result.smart.Method(self._client.get, request, **kwargs)
142-
return result.smart.Result(method)
141+
method = smart.Method(self._client.get, request, **kwargs)
142+
return smart.Result(method)
143+
144+
def _get_normal_result(self, request, **kwargs):
145+
method = normal.Method(self._client.get, request, **kwargs)
146+
return normal.Result(method)
143147

144148

145149
class MimeTypeMixin(object):

pygithub3/services/repos/commits.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,35 @@ class Commits(Service, MimeTypeMixin):
1212
This service support :ref:`mimetypes-section` configuration
1313
"""
1414

15-
#TODO: Pagination structure differs from usual
16-
#def list(self, user=None, repo=None, sha='', path=''):
17-
#request = self.make_request('repos.commits.list', user=user, repo=repo)
18-
#return self._get_result(request, sha=sha, path=path)
15+
def list(self, user=None, repo=None, sha=None, path=None):
16+
""" Get repository's commits
17+
18+
:param str user: Username
19+
:param str repo: Repository
20+
:param str sha: Sha or branch to start listing commits from
21+
:param str path: Only commits containing this file path will be returned
22+
:returns: A :doc:`result`
23+
24+
.. note::
25+
Remember :ref:`config precedence`
26+
27+
.. warning::
28+
Usually a repository has thousand of commits, so be careful when
29+
consume the result. You should filter with ``sha`` or directly
30+
clone the repository
31+
32+
::
33+
34+
commits_service.list(user='octocat', repo='oct_repo')
35+
commits_service.list(user='octocat', repo='oct_repo', sha='dev')
36+
commits_service.list(user='django', repo='django', sha='master',
37+
path='django/db/utils.py')
38+
"""
39+
request = self.make_request('repos.commits.list', user=user, repo=repo)
40+
params = {}
41+
params.update(sha and {'sha': sha} or {})
42+
params.update(path and {'path': path} or {})
43+
return self._get_normal_result(request, **params)
1944

2045
def get(self, sha, user=None, repo=None):
2146
""" Get a single commit

pygithub3/tests/services/test_repos.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,11 @@ class TestCommitsService(TestCase):
179179
def setUp(self):
180180
self.cs = Commits(user='oct', repo='re_oct')
181181

182-
"""
183182
def test_LIST(self, request_method):
184183
request_method.return_value = mock_response_result()
185184
self.cs.list().all()
186185
self.assertEqual(request_method.call_args[0],
187186
('get', _('repos/oct/re_oct/commits')))
188-
"""
189187

190188
def test_GET(self, request_method):
191189
request_method.return_value = mock_response()

0 commit comments

Comments
 (0)