8000 Fix PullFile.contents for private repos · justfortherec/github3.py@8a0ef20 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8a0ef20

Browse files
committed
Fix PullFile.contents for private repos
1 parent 270f6d9 commit 8a0ef20

File tree

5 files changed

+15
-74
lines changed

5 files changed

+15
-74
lines changed

AUTHORS.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,5 @@ Contributors
9292
- Jürgen Hermann (@jhermann)
9393

9494
- Antoine Giraudmaillet (@antoine-g)
95+
96+
- Paulus Schoutsen (@balloob)

github3/pulls.py

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from json import dumps
1313

1414
from . import models
15-
from . import utils
15+
from .repos.contents import Contents
1616
from .repos.commit import RepoCommit
1717
from .users import User
1818
from .decorators import requires_auth
@@ -78,39 +78,19 @@ def _update_attributes(self, pfile):
7878
self.raw_url = pfile.get('raw_url')
7979
#: Patch generated by this pull request
8080
self.patch = pfile.get('patch')
81+
#: URL to JSON object with content and metadata
82+
self.contents_url = pfile.get('contents_url')
8183

8284
def _repr(self):
8385
return '<Pull Request File [{0}]>'.format(self.filename)
8486

8587
def contents(self):
86-
"""Return the contents of the file as bytes.
88+
"""Return the contents of the file.
8789
88-
:param stream: When true, the resulting object can be iterated over via
89-
``iter_contents``.
90+
:returns: :class:`Contents <github3.repos.contents.Contents>`
9091
"""
91-
headers = {'Accept': 'application/octet-stream'}
92-
resp = self._get(self.raw_url, headers=headers)
93-
if self._boolean(resp, 200, 404):
94-
return resp.content
95-
return b''
96-
97-
def download(self, path=None):
98-
"""Download the contents for this file to disk.
99-
100-
:param path: (optional), path where the file should be saved
101-
to, default is the filename provided in the headers and will be
102-
written in the current directory.
103-
it can take a file-like object as well
104-
:type path: str, file-like object
105-
:returns: bool -- True if successful, False otherwise
106-
"""
107-
headers = {'Accept': 'application/octet-stream'}
108-
resp = self._get(self.raw_url, stream=True, headers=headers)
109-
if path is None:
110-
path = self.filename
111-
if self._boolean(resp, 200, 404):
112-
return utils.stream_response_to_file(resp, path)
113-
return None
92+
json = self._json(self._get(self.contents_url), 200)
93+
return self._instance_or_null(Contents, json)
11494

11595

11696
class PullRequest(models.GitHubCore):

github3/repos/contents.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def _update_attributes(self, content):
6262
#: with the character set you wish to use, e.g.,
6363
#: ``content.decoded.decode('utf-8')``.
6464
#: .. versionchanged:: 0.5.2
65-
self.decoded = ''
65+
self.decoded = b''
6666
if self.encoding == 'base64' and self.content:
6767
self.decoded = b64decode(self.content.encode())
6868

tests/integration/test_pulls.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import tempfile
44

55
import github3
6+
from github3 import repos
67

78
from .helper import IntegrationHelper
89

@@ -151,18 +152,6 @@ def test_contents(self):
151152
owner='sigmavirus24', repo='github3.py', pull_number=286,
152153
filename='github3/pulls.py'
153154
)
154-
155-
assert isinstance(pull_file.contents(), bytes)
156-
157-
def test_download(self):
158-
"""Show that a user can download a file in a pull request."""
159-
cassette_name = self.cassette_name('download')
160-
with self.recorder.use_cassette(cassette_name):
161-
pull_file = self.get_pull_request_file(
162-
owner='sigmavirus24', repo='github3.py', pull_number=286,
163-
filename='github3/pulls.py'
164-
)
165-
166-
with tempfile.NamedTemporaryFile() as fd:
167-
filename = pull_file.download(fd)
168-
assert filename is not None
155+
contents = pull_file.contents()
156+
assert isinstance(contents, repos.contents.Contents)
157+
assert contents.decoded != b''

tests/unit/test_pulls.py

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -298,40 +298,10 @@ class TestPullFile(UnitHelper):
298298
F438 " module Test")
299299
}
300300

301-
@mock.patch('github3.utils.stream_response_to_file')
302-
def test_download(self, stream_response_to_file):
303-
"""Verify the proper request is made to download file contents."""
304-
response_mock = mock.Mock()
305-
response_mock.status_code = 200
306-
self.session.get.return_value = response_mock
307-
308-
self.instance.download()
309-
310-
self.session.get.assert_called_once_with(
311-
self.example_data['raw_url'], stream=True,
312-
headers={'Accept': 'application/octet-stream'}
313-
)
314-
stream_response_to_file.assert_called_once_with(response_mock,
315-
'file1.txt')
316-
317-
@mock.patch('github3.utils.stream_response_to_file')
318-
def test_download_does_not_stream(self, stream_response_to_file):
319-
"""Verify the proper request is made to download file contents."""
320-
# Since the default return value for self.session.get is None we do
321-
# not need to mock out the response object in this test.
322-
self.instance.download()
323-
324-
self.session.get.assert_called_once_with(
325-
self.example_data['raw_url'], stream=True,
326-
headers={'Accept': 'application/octet-stream'}
327-
)
328-
assert stream_response_to_file.called is False
329-
330301
def test_contents(self):
331302
"""Verify the request made to fetch a pull request file contents."""
332303
self.instance.contents()
333304

334305
self.session.get.assert_called_once_with(
335-
self.example_data['raw_url'],
336-
headers={'Accept': 'application/octet-stream'}
306+
self.example_data['contents_url']
337307
)

0 commit comments

Comments
 (0)
0