8000 Generate the _api attribute for ShortBranch objects · pythonthings/github3.py@eb7a44d · GitHub
[go: up one dir, main page]

Skip to content

Commit eb7a44d

Browse files
committed
Generate the _api attribute for ShortBranch objects
Parse the commit's URL to generate the Branch URL. Closes sigmavirus24gh-801
1 parent dcb72b3 commit eb7a44d

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

github3/repos/branch.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ class _Branch(models.GitHubCore):
2525
def _update_attributes(self, branch):
2626
self.commit = commit.MiniCommit(branch['commit'], self)
2727
self.name = branch['name']
28+
base = self.commit.url.split('/commit', 1)[0]
29+
self._api = self._build_url('branches', self.name, base_url=base)
2830

2931
def _repr(self):
3032
return '<{0} [{1}]>'.format(self.class_name, self.name)
3133

3234
def latest_sha(self, differs_from=''):
33-
"""Check if SHA-1 is the same as remote branch.
35+
"""Check if SHA-1 is the same as the remote branch.
3436
3537
See: https://git.io/vaqIw
3638
@@ -139,10 +141,6 @@ def _update_attributes(self, branch):
139141
self.protection_url = branch['protection_url']
140142
if self.links and 'self' in self.links:
141143
self._api = self.links['self']
142-
elif isinstance(self.commit, commit.ShortCommit):
143-
# Branches obtained via `repo.branches` don't have links.
144-
base = self.commit.url.split('/commit', 1)[0]
145-
self._api = self._build_url('branches', self.name, base_url=base)
146144

147145

148146
class ShortBranch(_Branch):

tests/unit/helper.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,18 @@ def put_called_with(self, *args, **kwargs):
154154
def setUp(self):
155155
"""Use to set up attributes on self before each test."""
156156
self.session = self.create_session_mock()
157-
self.instance = self.create_instance_of_described_class()
158157
# Proxy the build_url method to the class so it can build the URL and
159158
# we can assert things about the call that will be attempted to the
160159
# internet
160+
self.old_build_url = self.described_class._build_url
161161
self.described_class._build_url = build_url
162+
self.instance = self.create_instance_of_described_class()
162163
self.after_setup()
163164

165+
def tearDown(self):
166+
"""Reset attributes on items under test."""
167+
self.described_class._build_url = self.old_build_url
168+
164169
def after_setup(self):
165170
"""No-op method to avoid people having to override setUp."""
166171
pass

tests/unit/json/repos_branch_example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"commit": {
44
"sha": "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
55
"commit": {
6+
"url": "https://api.github.com/repos/octocat/Hello-World/commits/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
67
"author": {
78
"name": "The Octocat",
89
"date": "2012-03-06T15:06:50-08:00",

0 commit comments

Comments
 (0)
0