Skip to content

Commit 37698db

Browse files
committed
Add GistHistory to cover gist histories
1 parent 490994e commit 37698db

3 files changed

Lines changed: 94 additions & 42 deletions

File tree

HISTORY.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ History/Changelog
2424

2525
- is_unread() and unread are now the same
2626

27-
- In github3.gists.GistFile
27+
- In github3.gists
2828

29-
- filename and name return the same information
29+
- GistFile.filename and GistFile.name return the same information
30+
- Gist.history now lists the history of the gist
31+
- GistHistory is an object representing one commit or version of the history
32+
- You can retrieve gists at a specific version with GistHistory.get_gist()
3033

3134
- github3.orgs.Organization.iter_repos now accepts all types_
3235

docs/gists.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ Gist Objects
2929

3030
.. autoclass:: GistFile
3131
:inherited-members:
32+
33+
------
34+
35+
.. autoclass:: GistHistory
36+
:inherited-members:

github3/gists.py

Lines changed: 84 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,6 @@
1111
from github3.decorators import requires_auth
1212

1313

14-
class GistFile(GitHubObject):
15-
"""The :class:`GistFile <GistFile>` object. This is used to represent a
16-
file object returned by GitHub while interacting with gists.
17-
"""
18-
def __init__(self, attributes):
19-
super(GistFile, self).__init__(attributes)
20-
21-
#: The raw URL for the file at GitHub.
22-
self.raw_url = attributes.get('raw_url')
23-
#: The name of the file.
24-
self.filename = attributes.get('filename')
25-
#: The name of the file.
26-
self.name = attributes.get('filename')
27-
#: The language associated with the file.
28-
self.language = attributes.get('language')
29-
#: The size of the file.
30-
self.size = attributes.get('size')
31-
#: The content of the file.
32-
self.content = attributes.get('content')
33-
34-
def __repr__(self):
35-
return '<Gist File [{0}]>'.format(self.name)
36-
37-
38-
class GistComment(BaseComment):
39-
"""The :class:`GistComment <GistComment>` object. This represents a comment
40-
on a gist.
41-
"""
42-
def __init__(self, comment, session=None):
43-
super(GistComment, self).__init__(comment, session)
44-
45-
#: :class:`User <github3.users.User>` who made the comment
46-
self.user = None
47-
if comment.get('user'):
48-
self.user = User(comment.get('user'), self)
49-
50-
def __repr__(self):
51-
return '<Gist Comment [{0}]>'.format(self.user.login)
52-
53-
5414
class Gist(GitHubCore):
5515
"""The :class:`Gist <Gist>` object. This object holds all the information
5616
returned by Github about a gist. With it you can comment on or fork the
@@ -100,6 +60,9 @@ def __init__(self, data, session=None):
10060
#: Number of files in this gist.
10161
self.files = len(self._files)
10262

63+
#: History of this gist, list of :class:`GistHistory <GistHistory>`
64+
self.history = [GistHistory(h, self) for h in data.get('history', [])]
65+
10366
def __repr__(self):
10467
return '<Gist [{0}]>'.format(self.id)
10568

@@ -220,3 +183,84 @@ def unstar(self):
220183
"""
221184
url = self._build_url('star', base_url=self._api)
222185
return self._boolean(self._delete(url), 204, 404)
186+
187+
188+
class GistComment(BaseComment):
189+
"""The :class:`GistComment <GistComment>` object. This represents a comment
190+
on a gist.
191+
"""
192+
def __init__(self, comment, session=None):
193+
super(GistComment, self).__init__(comment, session)
194+
195+
#: :class:`User <github3.users.User>` who made the comment
196+
self.user = None
197+
if comment.get('user'):
198+
self.user = User(comment.get('user'), self)
199+
200+
def __repr__(self):
201+
return '<Gist Comment [{0}]>'.format(self.user.login)
202+
203+
204+
class GistFile(GitHubObject):
205+
"""The :class:`GistFile <GistFile>` object. This is used to represent a
206+
file object returned by GitHub while interacting with gists.
207+
"""
208+
def __init__(self, attributes):
209+
super(GistFile, self).__init__(attributes)
210+
211+
#: The raw URL for the file at GitHub.
212+
self.raw_url = attributes.get('raw_url')
213+
#: The name of the file.
214+
self.filename = attributes.get('filename')
215+
#: The name of the file.
216+
self.name = attributes.get('filename')
217+
#: The language associated with the file.
218+
self.language = attributes.get('language')
219+
#: The size of the file.
220+
self.size = attributes.get('size')
221+
#: The content of the file.
222+
self.content = attributes.get('content')
223+
224+
def __repr__(self):
225+
return '<Gist File [{0}]>'.format(self.name)
226+
227+
228+
class GistHistory(GitHubCore):
229+
"""The :class:`GistHistory <GistHistory>` object represents one version
230+
(or revision) of a gist."""
231+
def __init__(self, history, session=None):
232+
super(GistHistory, self).__init__(history, session)
233+
self._api = history.get('url', '')
234+
235+
#: SHA of the commit associated with this version
236+
self.version = history.get('version', '')
237+
238+
#: user who made these changes
239+
self.user = User(history.get('user') or {}, session)
240+
241+
#: dict containing the change status; see also: deletions, additions,
242+
#: total
243+
self.change_status = history.get('change_status', {})
244+
245+
#: number of additions made
246+
self.additions = self.change_status.get('additions', 0)
247+
248+
#: number of deletions made
249+
self.deletions = self.change_status.get('deletions', 0)
250+
251+
#: total number of changes made
252+
self.total = self.change_status.get('total', 0)
253+
254+
#: datetime representation of when the commit was made
255+
self.committed_at = self._strptime(history.get('committed_at'))
256+
257+
def __repr__(self):
258+
return '<Gist History [{0}]>'.format(self.version)
259+
260+
def get_gist(self):
261+
"""Retrieves the gist at this version.
262+
263+
:returns: :class:`Gist <Gist>`
264+
"""
265+
json = self._json(self._get(self._api), 200)
266+
return Gist(json, self)

0 commit comments

Comments
 (0)