Skip to content

Commit f7ad5a5

Browse files
committed
Update github3.git to remove get_attribute
This also created a CommitTree object as the tree is different via the git object API vs the repository object API. New example data is added to account for this difference as well.
1 parent 6f733b1 commit f7ad5a5

File tree

3 files changed

+91
-32
lines changed

3 files changed

+91
-32
lines changed

github3/git.py

+59-31
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,25 @@ class Blob(GitHubCore):
2424
"""
2525

2626
def _update_attributes(self, blob):
27-
self._api = self._get_attribute(blob, 'url')
27+
self._api = blob['url']
2828

2929
#: Raw content of the blob.
30-
self.content = self._get_attribute(blob, 'content')
30+
self.content = blob['content']
3131
if self.content is not None:
3232
self.content = self.content.encode()
3333

3434
#: Encoding of the raw content.
35-
self.encoding = self._get_attribute(blob, 'encoding')
35+
self.encoding = blob['encoding']
3636

3737
#: Decoded content of the blob.
3838
self.decoded = self.content
3939
if self.encoding == 'base64':
4040
self.decoded = b64decode(self.content)
4141

4242
#: Size of the blob in bytes
43-
self.size = self._get_attribute(blob, 'size')
43+
self.size = blob['size']
4444
#: SHA1 of the blob
45-
self.sha = self._get_attribute(blob, 'sha')
45+
self.sha = blob['sha']
4646

4747
def _repr(self):
4848
return '<Blob [{0:.10}]>'.format(self.sha)
@@ -58,8 +58,8 @@ class GitData(GitHubCore):
5858

5959
def _update_attributes(self, data):
6060
#: SHA of the object
61-
self.sha = self._get_attribute(data, 'sha')
62-
self._api = self._get_attribute(data, 'url')
61+
self.sha = data['sha']
62+
self._api = data['url']
6363

6464

6565
class Commit(BaseCommit):
@@ -75,21 +75,25 @@ def _update_attributes(self, commit):
7575
super(Commit, self)._update_attributes(commit)
7676
#: dict containing at least the name, email and date the commit was
7777
#: created
78-
self.author = self._get_attribute(commit, 'author', {})
79-
# If GH returns nil/None then make sure author is a dict
80-
self._author_name = self._get_attribute(self.author, 'name')
78+
self.author = commit['author']
79+
# GitHub may not provide a name for the author
80+
if self.author.get('name'):
81+
self._author_name = self.author['name']
8182

8283
#: dict containing similar information to the author attribute
83-
self.committer = self._get_attribute(commit, 'committer', {})
84-
# blank the data if GH returns no data
84+
# If the committer is not different from the author, we may not get
85+
# a committer key
86+
if commit.get('committer'):
87+
self.committer = commit['committer']
8588

86-
self._commit_name = self._get_attribute(self.committer, 'name')
89+
if self.committer.get('name'):
90+
self._commit_name = self.committer['name']
8791

88-
#: :class:`Tree <Tree>` the commit belongs to.
89-
self.tree = self._class_attribute(commit, 'tree', Tree, self)
92+
#: :class:`CommitTree <CommitTree>` the commit belongs to.
93+
self.tree = CommitTree(commit['tree'], self)
9094

9195
def _repr(self):
92-
return '<Commit [{0}:{1}]>'.format(self._author_name, self.sha)
96+
return '<Commit [{0}]>'.format(self.sha)
9397

9498

9599
class Reference(GitHubCore):
@@ -102,13 +106,13 @@ class Reference(GitHubCore):
102106
"""
103107

104108
def _update_attributes(self, ref):
105-
self._api = self._get_attribute(ref, 'url')
109+
self._api = ref['url']
106110

107111
#: The reference path, e.g., refs/heads/sc/featureA
108-
self.ref = self._get_attribute(ref, 'ref')
112+
self.ref = ref['ref']
109113

110114
#: :class:`GitObject <GitObject>` the reference points to
111-
self.object = self._class_attribute(ref, 'object', GitObject, self)
115+
self.object = GitObject(ref['object'], self)
112116

113117
def _repr(self):
114118
return '<Reference [{0}]>'.format(self.ref)
@@ -146,7 +150,7 @@ class GitObject(GitData):
146150
def _update_attributes(self, obj):
147151
super(GitObject, self)._update_attributes(obj)
148152
#: The type of object.
149-
self.type = self._get_attribute(obj, 'type')
153+
self.type = obj['type']
150154

151155
def _repr(self):
152156
return '<Git Object [{0}]>'.format(self.sha)
@@ -164,21 +168,43 @@ def _update_attributes(self, tag):
164168
super(Tag, self)._update_attributes(tag)
165169

166170
#: String of the tag
167-
self.tag = self._get_attribute(tag, 'tag')
171+
self.tag = tag['tag']
168172

169173
#: Commit message for the tag
170-
self.message = self._get_attribute(tag, 'message')
174+
self.message = tag['message']
171175

172176
#: dict containing the name and email of the person
173-
self.tagger = self._get_attribute(tag, 'tagger')
177+
self.tagger = tag['tagger']
174178

175179
#: :class:`GitObject <GitObject>` for the tag
176-
self.object = self._class_attribute(tag, 'object', GitObject, self)
180+
self.object = GitObject(tag['object'], self)
177181

178182
def _repr(self):
179183
return '<Tag [{0}]>'.format(self.tag)
180184

181185

186+
class CommitTree(GitData):
187+
188+
"""The :class:`CommitTree <CommitTree>` object.
189+
190+
Represents the tree data found in a commit object
191+
192+
"""
193+
194+
def _update_attributes(self, tree):
195+
super(CommitTree, self)._update_attributes(tree)
196+
197+
def _repr(self):
198+
return '<CommitTree [{0}]>'.format(self.sha)
199+
200+
def to_tree(self):
201+
"""Retrieve a full Tree object for this CommitTree."""
202+
json = self._json(self._get(self._api), 200)
203+
return self._instance_or_null(Tree, json)
204+
205+
refresh = to_tree
206+
207+
182208
class Tree(GitData):
183209

184210
"""The :class:`Tree <Tree>` object.
@@ -191,7 +217,7 @@ def _update_attributes(self, tree):
191217
super(Tree, self)._update_attributes(tree)
192218

193219
#: list of :class:`Hash <Hash>` objects
194-
self.tree = self._get_attribute(tree, 'tree', [])
220+
self.tree = tree['tree']
195221
if self.tree:
196222
self.tree = [Hash(t, self) for t in self.tree]
197223

@@ -224,22 +250,24 @@ class Hash(GitHubCore):
224250

225251
def _update_attributes(self, info):
226252
#: Path to file
227-
self.path = self._get_attribute(info, 'path')
253+
self.path = info['path']
228254

229255
#: File mode
230-
self.mode = self._get_attribute(info, 'mode')
256+
self.mode = info['mode']
231257

232258
#: Type of hash, e.g., blob
233-
self.type = self._get_attribute(info, 'type')
259+
self.type = info['type']
234260

235261
#: Size of hash
236-
self.size = self._get_attribute(info, 'size')
262+
# Size is not set if the type is a tree
263+
if self.type != 'tree':
264+
self.size = info['size']
237265

238266
#: SHA of the hash
239-
self.sha = self._get_attribute(info, 'sha')
267+
self.sha = info['sha']
240268

241269
#: URL of this object in the GitHub API
242-
self.url = self._get_attribute(info, 'url')
270+
self.url = info['url']
243271

244272
def _repr(self):
245273
return '<Hash [{0}]>'.format(self.sha)

tests/unit/json/git_commit_example

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"sha": "7638417db6d59f3c431d3e1f261cc637155684cd",
3+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/7638417db6d59f3c431d3e1f261cc637155684cd",
4+
"author": {
5+
"date": "2014-11-07T22:01:45Z",
6+
"name": "Scott Chacon",
7+
"email": "schacon@gmail.com"
8+
},
9+
"committer": {
10+
"date": "2014-11-07T22:01:45Z",
11+
"name": "Scott Chacon",
12+
"email": "schacon@gmail.com"
13+
},
14+
"message": "added readme, because im a good github citizen",
15+
"tree": {
16+
"url": "https://api.github.com/repos/octocat/Hello-World/git/trees/691272480426f78a0138979dd3ce63b77f706feb",
17+
"sha": "691272480426f78a0138979dd3ce63b77f706feb"
18+
},
19+
"parents": [
20+
{
21+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/1acc419d4d6a9ce985db7be48c6349a0475975b5",
22+
"sha": "1acc419d4d6a9ce985db7be48c6349a0475975b5"
23+
}
24+
],
25+
"verification": {
26+
"verified": false,
27+
"reason": "unsigned",
28+
"signature": null,
29+
"payload": null
30+
}
31+
}

tests/unit/test_git.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'octocat/Hello-World/'
1111
'git/refs/heads/featureA')
1212

13-
get_commit_example_data = create_example_data_helper('commit_example')
13+
get_commit_example_data = create_example_data_helper('git_commit_example')
1414
get_git_tag_example_data = create_example_data_helper('git_tag_example')
1515
get_reference_example_data = create_example_data_helper('reference_example')
1616

0 commit comments

Comments
 (0)